Guest User

Untitled

a guest
Jun 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4.  
  5. /**
  6. * This class performs filesystem searches using the Spotlight facility in Mac OS X
  7. * versions 10.4 and higher. The search query is specified using the syntax of the
  8. * mdfind command line tool. This is a powerful syntax which supports wildcards,
  9. * boolean expressions, and specifications of particular metadata attributes to search.
  10. * For details, see the documentation for mdfind.
  11. *
  12. * @author Peter Eastman
  13. */
  14.  
  15.  
  16. public class Spotlight
  17. {
  18. /**
  19. * Perform a Spotlight search.
  20. *
  21. * @param query the query string to search for
  22. * @return a list of all files and folders matching the search
  23. */
  24.  
  25.  
  26. public static List<File> find(String query) throws IOException
  27. {
  28. return doSearch(new String[] {"mdfind", query});
  29. }
  30.  
  31.  
  32. /**
  33. * Perform a Spotlight search.
  34. *
  35. * @param query the query string to search for
  36. * @param folder the search will be restricted to files inside this folder
  37. * @return a list of all files and folders matching the search
  38. */
  39.  
  40.  
  41. public static List<File> find(String query, File folder) throws IOException
  42. {
  43. return doSearch(new String[] {"mdfind", "-onlyin", folder.getAbsolutePath(), query});
  44. }
  45.  
  46.  
  47. private static List<File> doSearch(String command[]) throws IOException
  48. {
  49. Process process = Runtime.getRuntime().exec(command);
  50. BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream()));
  51. ArrayList<File> results = new ArrayList<File>();
  52. String line;
  53. while ((line = out.readLine()) != null)
  54. results.add(new File(line));
  55. return results;
  56. }
  57.  
  58.  
  59. /**
  60. * Get a map containing all searchable metadata attributes for a particular
  61. * file or folder.
  62. *
  63. * @param file the file to report on
  64. * @return a Map containing all metadata for the file
  65. */
  66.  
  67.  
  68. public static Map<String,String> getMetadata(File file) throws IOException
  69. {
  70. Process process = Runtime.getRuntime().exec(new String[] {"mdls", file.getAbsolutePath()});
  71. BufferedReader out = new BufferedReader(new InputStreamReader(process.getInputStream()));
  72. HashMap<String,String> results = new HashMap<String,String>();
  73. String line;
  74. while ((line = out.readLine()) != null)
  75. {
  76. int equals = line.indexOf('=');
  77. if (equals > -1)
  78. {
  79. String key = line.substring(0, equals).trim();
  80. String value = line.substring(equals+1).trim();
  81. results.put(key, value);
  82. }
  83. }
  84. return results;
  85. }
  86. }
Add Comment
Please, Sign In to add comment