-- tagging.applescript -- Prompts user for a folder containing movies, then prompts with a list of -- selectable tag names that are applied to the movie files only. -- Version: 1.2 -- Tested: macOS 10.13.6 (17G5019), macOS 10.14.3 (18D109) -- Works as AppleScript, or Automator application (with Run AppleScript). -- Can set tags on movies in a folder selected via Sharing from another Mac. -- VikingOSX, 2019-03-25, Apple Support Communities, No warranties/liabilites at all. use framework "Cocoa" use AppleScript version "2.4" -- Yosemite or later use scripting additions property NSWorkspace : a reference to current application's NSWorkspace property NSString : a reference to current application's NSString property NSArray : a reference to current application's NSArray property |NSURL| : a reference to current application's |NSURL| property NSURLTagNamesKey : a reference to current application's NSURLTagNamesKey -- this file only exists if user created custom tag names in Finder preferences set plistTilde to "~/Library/SyncedPreferences/com.apple.finder.plist" set keyPlist to "values:FinderTagDict:value:FinderTags" set defloc to (path to desktop) as alias set cfmsg to "Select the initial folder containing items to be tagged." set listmsg to "For sequential selection, select a tag, press and hold the shift key, and select another tag." & return & return & "For non-sequential tag selection, or deselection, press and hold the ⌘ key." & return & return & "To take the default tag, just press return." & return & return & "To remove all tags on a file, select None." -- file extensions to receive one or more tags set valid_tags to {"mkv", "mp4", "m4v", "mov"} set plistPath to expand_tilde(plistTilde) -- make a list of tag names if exists plistPath is true then set allTags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & keyPlist & "\" " & plistPath's quoted form & " | awk '/n/ {print substr($0, index($0, $3))}'") if allTags does not contain "None" then copy "None" to the beginning of allTags else display alert "No custom user tags found." message "Only System tag colors available." as critical giving up after 10 -- Use default System tag names set allTags to (NSWorkspace's sharedWorkspace()'s fileLabels()) as list end if -- prompt user for folder containing images to tag try set movie_folder to (choose folder with prompt cfmsg default location defloc without invisibles, multiple selections allowed and showing package contents) on error errmsg number errnbr my error_handler(errnbr, errmsg) return end try -- Present a list of selectable tag names set tagvect to (choose from list allTags with title "Tag Names" with prompt listmsg default items {"None"} with multiple selections allowed and empty selection allowed) -- if user cancelled tag selection, then stop now if tagvect is false then return tell application "Finder" -- processes folder hierarchy, only tagging valid movies, and not their enclosing folders. set mList to (every item in entire contents of folder movie_folder where valid_tags contains name extension) as alias list end tell repeat with anItem in mList set_tag(anItem, tagvect) end repeat return on set_tag(theFile, atags) set tagArray to NSArray's arrayWithArray:atags set fileURL to |NSURL|'s fileURLWithPath:(POSIX path of theFile) fileURL's setResourceValue:tagArray forKey:(NSURLTagNamesKey) |error|:(missing value) end set_tag on expand_tilde(tpath) set apath to NSString's stringWithString:tpath return (apath's stringByExpandingTildeInPath()) as text end expand_tilde on error_handler(nbr, msg) return display alert "[ " & nbr & " ] " & msg as critical giving up after 10 end error_handler