I’m organizing some old files that were created in the days before OS X allowed you to attach tags to files. In the absence of tags, I would add descriptive info as comma-separated Finder comments (i.e. tree, landscape, sky, clouds).
I’ve come up with this script that iterates through files that I’ve selected in the Finder - for each file, it reads the comma-separated comments into a list and then applies each list item as a tag. Here’s the script:
tell application "Finder"
set filelist to selection as alias list
repeat with i in filelist
set thecomment to (get the comment of i)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set mycomments to every text item of thecomment
set AppleScript's text item delimiters to oldDelimiters
get mycomments
do shell script "xattr -rd com.apple.metadata:_kMDItemUserTags " & quoted form of POSIX path of i --clear existing tags
repeat with x in mycomments
log x
do shell script "/usr/local/bin/tag -a " & quoted form of x & " " & quoted form of POSIX path of i --apply tags derived from comments field
end repeat
end repeat
end tell
Works reliably, but it is sloooooow. Anyone have any ideas on how to speed things up? Any help is appreciated, thanks!
Test this script carefully. Perhaps it will improve the speed:
set pathsString to ""
set quotedPaths to {}
set commentsList to {}
tell application "Finder"
set filelist to selection as alias list
repeat with i in filelist
set quotedPath to quoted form of POSIX path of i
set end of quotedPaths to quotedPath
set end of commentsList to comment of i
set pathsString to pathsString & space & quotedPath
end repeat
end tell
-- clear the tags for all selected items at once
do shell script "xattr -rd com.apple.metadata:_kMDItemUserTags" & pathsString
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with i from 1 to count quotedPaths
set pathItem to item i of quotedPaths
set thecomment to item i of commentsList
set mycomments to every text item of thecomment
repeat with x in mycomments
log x
--apply tags derived from comments field
do shell script "/usr/local/bin/tag -a " & quoted form of x & " " & pathItem
end repeat
end repeat
set AppleScript's text item delimiters to oldDelimiters
I don’t use tags or have the tag utility but the following appears to do what the OP wants. The setTags handler is Shane’s.
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
tell application "Finder"
set fileList to selection as alias list
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with aFile in fileList
set theComment to comment of aFile
set myComments to every text item of theComment
setTags(aFile, myComments) of me
end repeat
set AppleScript's text item delimiters to oldDelimiters
end tell
on setTags(theFile, tagList)
set aURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFile)
aURL's setResourceValue:tagList forKey:(current application's NSURLTagNamesKey) |error|:(missing value)
end setTags