SHARE
TWEET

List items and their tags

applehelpwriter Nov 21st, 2017 (edited) 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #######################
  3. -->> DESCRIPTION
  4. #######################
  5. (*
  6.  
  7. Return items with tags
  8.  
  9. *)
  10. #######################
  11. -->> USAGE
  12. #######################
  13. (*
  14.  
  15. Run the script and choose a folder when prompted.
  16.  
  17.  
  18. *)
  19. #######################
  20. -->> IMPORT STATEMENTS
  21. #######################
  22.  
  23. use AppleScript version "2.4" -- Yosemite (10.10) or later
  24. use scripting additions
  25. use framework "Foundation"
  26.  
  27.  
  28. #######################
  29. -->> VARIABLES
  30. #######################
  31. set d to current date
  32.  
  33. # WARNING: large folders are going to take forever...
  34. set folderToSearchPath to POSIX path of (choose folder)
  35. set taggedFiles to {}
  36. set displayStr to ""
  37.  
  38. #######################
  39. -->> HANDLERS
  40. #######################
  41.  
  42. on removeWhiteSpace:aString
  43.     set theString to current application's NSString's stringWithString:aString
  44.     set theWhiteSet to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
  45.     set theString to theString's stringByTrimmingCharactersInSet:theWhiteSet
  46.     return theString as text
  47. end removeWhiteSpace:
  48.  
  49.  
  50. on tagsOfFileAtPath:POSIXPath
  51.     set theURL to current application's NSURL's fileURLWithPath:POSIXPath
  52.     set {theResult, theValue, theError} to theURL's getResourceValue:(reference) ¬
  53.         forKey:(current application's NSURLTagNamesKey) |error|:(reference)
  54.     if theResult as integer = 0 then
  55.         return {false, theError's localizedDescription() as text}
  56.     else
  57.         set theValue to theValue as list
  58.         if theValue = {missing value} then set theValue to {}
  59.         return {true, theValue}
  60.     end if
  61. end tagsOfFileAtPath:
  62.  
  63.  
  64. #######################
  65. -->> COMMANDS
  66. #######################
  67. set NSDirectoryEnumerationSkipsHiddenFiles to a reference to 4
  68. set NSFileManager to a reference to current application's NSFileManager
  69. set NSDirectoryEnumerationSkipsPackageDescendants to a reference to 2
  70. set nsPath to current application's NSString's stringWithString:folderToSearchPath
  71. set nsPath to nsPath's stringByResolvingSymlinksInPath()
  72. set folderNSURL to current application's |NSURL|'s fileURLWithPath:nsPath
  73.  
  74. set theURLs to (NSFileManager's defaultManager()'s enumeratorAtURL:folderNSURL includingPropertiesForKeys:{} options:(NSDirectoryEnumerationSkipsPackageDescendants + (get NSDirectoryEnumerationSkipsHiddenFiles)) errorHandler:(missing value))'s allObjects() as list
  75. set cc to count of theURLs
  76. repeat with i from 1 to count of theURLs
  77.     set this_path to item i of theURLs
  78.     set this_path to POSIX path of this_path
  79.     set tagData to (my tagsOfFileAtPath:this_path)
  80.     if tagData's item 1 is true and tagData's item 2's length is greater than 0 then
  81.         set end of my taggedFiles to {path:this_path, tag:tagData's item 2}
  82.     end if
  83. end repeat
  84. if (count of taggedFiles) is greater than 0 then
  85.     repeat with i from 1 to count of taggedFiles
  86.         set this_result to item i of taggedFiles
  87.         set tagList to this_result's tag
  88.         set tagString to ""
  89.         if (count of tagList) is greater than 1 then
  90.             repeat with t in tagList
  91.                 set tagString to tagString & t & ", "
  92.             end repeat
  93.             set tagString to (my removeWhiteSpace:tagString)
  94.             set tagString to text 1 thru -2 of tagString
  95.         else
  96.             set tagString to tagList as text
  97.         end if
  98.        
  99.         set displayStr to displayStr & return
  100.         set displayStr to displayStr & this_result's path & return & "TAG: " & tagString & return
  101.     end repeat
  102. end if
  103. displayStr
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top