Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######################
- -->> DESCRIPTION
- #######################
- (*
- Return items with tags
- *)
- #######################
- -->> USAGE
- #######################
- (*
- Run the script and choose a folder when prompted.
- *)
- #######################
- -->> IMPORT STATEMENTS
- #######################
- use AppleScript version "2.4" -- Yosemite (10.10) or later
- use scripting additions
- use framework "Foundation"
- #######################
- -->> VARIABLES
- #######################
- set d to current date
- # WARNING: large folders are going to take forever...
- set folderToSearchPath to POSIX path of (choose folder)
- set taggedFiles to {}
- set displayStr to ""
- #######################
- -->> HANDLERS
- #######################
- on removeWhiteSpace:aString
- set theString to current application's NSString's stringWithString:aString
- set theWhiteSet to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
- set theString to theString's stringByTrimmingCharactersInSet:theWhiteSet
- return theString as text
- end removeWhiteSpace:
- on tagsOfFileAtPath:POSIXPath
- set theURL to current application's NSURL's fileURLWithPath:POSIXPath
- set {theResult, theValue, theError} to theURL's getResourceValue:(reference) ¬
- forKey:(current application's NSURLTagNamesKey) |error|:(reference)
- if theResult as integer = 0 then
- return {false, theError's localizedDescription() as text}
- else
- set theValue to theValue as list
- if theValue = {missing value} then set theValue to {}
- return {true, theValue}
- end if
- end tagsOfFileAtPath:
- #######################
- -->> COMMANDS
- #######################
- set NSDirectoryEnumerationSkipsHiddenFiles to a reference to 4
- set NSFileManager to a reference to current application's NSFileManager
- set NSDirectoryEnumerationSkipsPackageDescendants to a reference to 2
- set nsPath to current application's NSString's stringWithString:folderToSearchPath
- set nsPath to nsPath's stringByResolvingSymlinksInPath()
- set folderNSURL to current application's |NSURL|'s fileURLWithPath:nsPath
- set theURLs to (NSFileManager's defaultManager()'s enumeratorAtURL:folderNSURL includingPropertiesForKeys:{} options:(NSDirectoryEnumerationSkipsPackageDescendants + (get NSDirectoryEnumerationSkipsHiddenFiles)) errorHandler:(missing value))'s allObjects() as list
- set cc to count of theURLs
- repeat with i from 1 to count of theURLs
- set this_path to item i of theURLs
- set this_path to POSIX path of this_path
- set tagData to (my tagsOfFileAtPath:this_path)
- if tagData's item 1 is true and tagData's item 2's length is greater than 0 then
- set end of my taggedFiles to {path:this_path, tag:tagData's item 2}
- end if
- end repeat
- if (count of taggedFiles) is greater than 0 then
- repeat with i from 1 to count of taggedFiles
- set this_result to item i of taggedFiles
- set tagList to this_result's tag
- set tagString to ""
- if (count of tagList) is greater than 1 then
- repeat with t in tagList
- set tagString to tagString & t & ", "
- end repeat
- set tagString to (my removeWhiteSpace:tagString)
- set tagString to text 1 thru -2 of tagString
- else
- set tagString to tagList as text
- end if
- set displayStr to displayStr & return
- set displayStr to displayStr & this_result's path & return & "TAG: " & tagString & return
- end repeat
- end if
- displayStr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement