从Java查询Mac OS X Spotlight
有关: 但这次是利用OSX的聚光灯 我想使用来自Java的OSX spotlight服务。有API可用吗从Java查询Mac OS X Spotlight,java,macos,spotlight,Java,Macos,Spotlight,有关: 但这次是利用OSX的聚光灯 我想使用来自Java的OSX spotlight服务。有API可用吗 据我所知,Spotlight没有Java API(我不认为它是添加到现在不推荐的Cocoa Java桥中的,桥上的Java桥开发已经停止)。但是,Spotlight有一个过程C,可以用它来包装。I包装mdfind命令行,对我来说很好: import java.io.*; import java.util.*; /** * This class performs filesystem
据我所知,Spotlight没有Java API(我不认为它是添加到现在不推荐的Cocoa Java桥中的,桥上的Java桥开发已经停止)。但是,Spotlight有一个过程C,可以用它来包装。I包装mdfind命令行,对我来说很好:
import java.io.*;
import java.util.*;
/**
* This class performs filesystem searches using the Spotlight facility in Mac OS X
* versions 10.4 and higher. The search query is specified using the syntax of the
* mdfind command line tool. This is a powerful syntax which supports wildcards,
* boolean expressions, and specifications of particular metadata attributes to search.
* For details, see the documentation for mdfind.
*
* @author Peter Eastman
*/
public class Spotlight
{
/**
* Perform a Spotlight search.
*
* @param query the query string to search for
* @return a list of all files and folders matching the search
*/
public static List<File> find(String query) throws IOException
{
return doSearch(new String[] {"mdfind", query});
}
/**
* Perform a Spotlight search.
*
* @param query the query string to search for
* @param folder the search will be restricted to files inside this folder
* @return a list of all files and folders matching the search
*/
public static List<File> find(String query, File folder) throws IOException
{
return doSearch(new String[] {"mdfind", "-onlyin", folder.getAbsolutePath(), query});
}
private static List<File> doSearch(String command[]) throws IOException
{
Process process = Runtime.getRuntime().exec(command);
BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream()));
ArrayList<File> results = new ArrayList<File>();
String line;
while ((line = out.readLine()) != null)
results.add(new File(line));
return results;
}
/**
* Get a map containing all searchable metadata attributes for a particular
* file or folder.
*
* @param file the file to report on
* @return a Map containing all metadata for the file
*/
public static Map<String,String> getMetadata(File file) throws IOException
{
Process process = Runtime.getRuntime().exec(new String[] {"mdls", file.getAbsolutePath()});
BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream()));
HashMap<String,String> results = new HashMap<String,String>();
String line;
while ((line = out.readLine()) != null)
{
int equals = line.indexOf('=');
if (equals > -1)
{
String key = line.substring(0, equals).trim();
String value = line.substring(equals+1).trim();
results.put(key, value);
}
}
return results;
}
}
import java.io.*;
导入java.util.*;
/**
*此类使用MacOSX中的Spotlight工具执行文件系统搜索
*版本10.4及更高版本。搜索查询是使用
*mdfind命令行工具。这是一个支持通配符的强大语法,
*布尔表达式,以及要搜索的特定元数据属性的规范。
*有关详细信息,请参阅mdfind的文档。
*
*@作者彼得·伊斯曼
*/
公共课聚光灯
{
/**
*执行聚光灯搜索。
*
*@param query要搜索的查询字符串
*@返回与搜索匹配的所有文件和文件夹的列表
*/
公共静态列表查找(字符串查询)引发IOException
{
返回doSearch(新字符串[]{“mdfind”,query});
}
/**
*执行聚光灯搜索。
*
*@param query要搜索的查询字符串
*@param folder搜索将限于此文件夹中的文件
*@返回与搜索匹配的所有文件和文件夹的列表
*/
公共静态列表查找(字符串查询、文件文件夹)引发IOException
{
返回doSearch(新字符串[]{“mdfind”,“-onlyin”,folder.getAbsolutePath(),query});
}
私有静态列表doSearch(字符串命令[])引发IOException
{
Process Process=Runtime.getRuntime().exec(命令);
BufferedReader out=新的BufferedReader(新的InputStreamReader(process.getInputStream());
ArrayList结果=新建ArrayList();
弦线;
而((line=out.readLine())!=null)
结果.添加(新文件(行));
返回结果;
}
/**
*获取一个包含特定对象的所有可搜索元数据属性的映射
*文件或文件夹。
*
*@param file要报告的文件
*@返回包含文件所有元数据的映射
*/
公共静态映射getMetadata(文件)引发IOException
{
Process Process=Runtime.getRuntime().exec(新字符串[]{“mdls”,file.getAbsolutePath()});
BufferedReader out=新的BufferedReader(新的InputStreamReader(process.getInputStream());
HashMap结果=新的HashMap();
弦线;
而((line=out.readLine())!=null)
{
int=line.indexOf('=');
如果(等于>-1)
{
String key=line.substring(0,等于).trim();
字符串值=line.substring(等于+1).trim();
结果。输入(键、值);
}
}
返回结果;
}
}