I found code that wraps the mdfind command line, works fine for me:
import java.io.*; import java.util.*; public class Spotlight { public static List<File> find(String query) throws IOException { return doSearch(new String[] {"mdfind", query}); } 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; } 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; } }
Renaud
source share