Tag selected file purple

I am trying to figure out how to use Applescript to label a selected file with the purple tag in Finder then open in Compressor. I have the open in compressor part working, but can't seem to get the tagging part to work.

User uploaded file

Here is what I have so far... any ideas?

tell application "Finder"

set sel to selection

if sel is not {} then

set filepath to item 1 of sel as text--> The path to the one selected item.

set label index to 6


--open filepath using application file id "com.apple.Compressor"

end if

end tell

Mac Pro, OS X El Capitan (10.11.6)

Posted on May 18, 2018 10:13 AM

Reply
Question marked as Top-ranking reply

Posted on May 18, 2018 11:06 AM

You can also use self-documenting record keys in the label index assignment.


property tagColor : {clear:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}


tell application "Finder"

set sel to selection

if sel is not {} then

set filepath to item 1 of sel as text--> The path to the one selected item.

# you can also just use 5 instead of purple of tagColor

set label index of itemfilepath to purple of tagColor

--open filepath using application file id "com.apple.Compressor"

end if

end tell

Similar questions

13 replies
Question marked as Top-ranking reply

May 18, 2018 11:06 AM in response to chadsgreene

You can also use self-documenting record keys in the label index assignment.


property tagColor : {clear:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}


tell application "Finder"

set sel to selection

if sel is not {} then

set filepath to item 1 of sel as text--> The path to the one selected item.

# you can also just use 5 instead of purple of tagColor

set label index of itemfilepath to purple of tagColor

--open filepath using application file id "com.apple.Compressor"

end if

end tell

May 20, 2018 11:29 AM in response to chadsgreene

I have written the following routines that can be called by your AppleScript from an AppleScript Library:

  • set_tags

    Destructively sets tags on a file

  • add_tags

    Sets new tags on a file where none are present, or adds a list of tags to existing tags on the file.

  • remove_tags

    Removes list of tags from tagged file.

  • clear_tags

    Clears all tags from the file


The intention is that you would copy/paste the following AppleScript into Script Editor, and then save it as an AppleScript first (e.g. TagLib.applescript), and then option:Save As… as a Script Bundle (TagLib.scptd) into your local Library : Script Libraries folder, or /Library/Script Libraries folder if multi-user application.

use framework "Foundation" use scripting additions property NSString : a reference to current application's NSString property |NSURL| : a reference to current application's |NSURL| property NSURLTagNamesKey : a reference to current application's NSURLTagNamesKey property NSDictionary : a reference to current application's NSDictionary property NSMutableArray : a reference to current application's NSMutableArray property NSOrderedSet : a reference to current application's NSOrderedSet (* -- Test harness set relpath to "~/Desktop/Test1.txt" set tpath to NSString's stringWithString:relpath set stdpath to (tpath's stringByStandardizingPath) as text log (get_tags(stdpath)) as list -- log (add_tags(stdpath, {"Purple", "Blue"})) -- log (remove_tags(stdpath, {"Purple"})) -- log (clear_tags(stdpath)) as list return *) on get_tags(apath) -- Get all tags from the file, or an empty list if none -- Returns list of tags on file, or empty list if no tags on file -- apath = POSIX path to file set thisKey to {"NSURLTagNamesKey"} set metadata to NSDictionary's dictionary() set aurl to |NSURL|'s fileURLWithPath:apath set metadata to aurl's resourceValuesForKeys:thisKey |error|:(missing value) -- file has no tags yet if (metadata's objectForKey:NSURLTagNamesKey) is missing value then return {""} as list -- file has tags, so return them as a list return (metadata's objectForKey:NSURLTagNamesKey) as list end get_tags on set_tags(apath, tags) -- Set new tags on specified file (replaces previous tags) -- Returns true or false based on result -- apath = POSIX path to file -- tags = tags to set on file set aurl to |NSURL|'s fileURLWithPath:apath set this_result to aurl's setResourceValue:tags forKey:NSURLTagNamesKey |error|:(missing value) if not this_result then return false end if return this_result end set_tags on add_tags(apath, tags) -- Set new tags if none present, otherwise, add non-duplicate tags -- Returns list of tags after the update -- apath = POSIX path to file -- tags = list of tags to add to the file if tags is {""} then return set aurl to |NSURL|'s fileURLWithPath:apath set tagList to get_tags(apath) set tagList to (tagList as list) & tags -- Ordered sets prohibit duplicates set tagList to (NSOrderedSet's orderedSetWithArray:tagList)'s allObjects() aurl's setResourceValue:tagList forKey:NSURLTagNamesKey |error|:(missing value) return get_tags(apath) as list end add_tags on remove_tags(apath, tags) -- Removes specified list of tags from those already set on the file -- Note: Use clear_tags() when wishing to remove all tags from file -- Returns list of tags after specified tags are removed -- Original tags remain intact if removal fails -- apath = POSIX path to file -- tags = list of tags to remove set tagList to get_tags(apath) if tagList is {""} or tags is {""} then return set origtagList to NSMutableArray's arrayWithArray:tagList origtagList's removeObjectsInArray:tags set survivingTags to ((NSOrderedSet's orderedSetWithArray:origtagList)'s allObjects()) as list set_tags(apath, survivingTags) return get_tags(apath) as list end remove_tags on clear_tags(apath) -- Clear all tags from the file -- Returns true or false based on outcome -- apath = POSIX path to file set cleartags to {""} set tagList to get_tags(apath) if tagList is {""} then return return set_tags(apath, cleartags) end clear_tags


And then, in your existing script, you can employ these routines as follows, where PosixFilePath is the UNIX path string to your particular file. Take a closer look at the documentation for each routine in the TagLib script, in case you want to use return values, including lists of tags.


tell script "TagLib"

set curtags to get_tags(PosixFilePath)

set_tags(PosixFilePath, {"Purple", "Green"})

add_tags(PosixFilePath, {"Blue"})

remove_tags(PosixFilePath, {"Purple"})

-- clear_tags(PosixFilePath)

end tell

May 18, 2018 2:47 PM in response to VikingOSX

VikingOSX, this is good, but it also wipes out any other tags that are already on the files.


Here is the big picture of what I am doing. In Final Cut Pro X I have a group of video clips that are scattered around the hard drive. I need to send them to compressor to make smaller versions of them. I tell my script that I have 121 files, so it will loop the correct number of times.

  1. Have Final Cut reveal the file in the finder.
  2. Color it purple so I can find it later if problems arise.
  3. Open it in compressor
  4. Activate Final Cut and arrow down to select the next file.
  5. Loop.


Some of my files already have important tags on them. I want to add purple, then be able to search for all them if compressor crashes.


Here is my current script.

if applicationid "com.apple.FinalCut" is running then



display dialog "IS EVERYTHING SETUP AND READY?


Be sure to...

1) Setup Compressor's default setting and location

2) Disable Skimming

3) Put Projects for Export in a keyword collection

4) Select the First Project/Video in the Collection" buttons {"Next"} default button 1 with title "Setup" with icon 2


set n to text returned of (display dialog "DON'T TOUCH ANYTHING while the script is running. This Script is Unstoppable and will take 2 Seconds Per video to complete.


Number of videos to send to Compressor" default answer "0" buttons {"Cancel", "Start"} default button 2 cancel button 1 with title "What Do I Do Now?" with icon 0)

tell applicationid "com.apple.FinalCut" to activate

repeat n times





--REVEAL IN FINDER FROM FINAL CUT


delay 0.5

tell application "System Events" to keystroke "r" using {command down, shift down}


delay 1





--TAG PURPLE AND OPEN IN COMPRESSOR

tell application "Finder"

set sel to selection

if sel is not {} then

set filepath to item 1 of sel--> The path to the one selected item.

set label index of filepath to 5


openfilepathusingapplication fileid "com.apple.Compressor"

end if

end tell



--SELECT NEXT FILE IN FINAL CUT

tell applicationid "com.apple.FinalCut" to activate


delay 0.5

tell application "System Events" to keystroke (key code 125)

end repeat





--CLOSE ALL FINDER WINDOWS

tell applicationid "com.apple.Finder" to activate


--tell application "System Events" to keystroke "w" using {command down, option down}



tell applicationid "com.apple.Compressor" to activate




(delay 2) activate


display dialog "Done. Go check Compressor." buttons {"Close"} default button 1 with title "I'm Finished!" with icon 2

else


display dialog "Please Launch FCP X and Prepare For Sharing" buttons {"OK"} default button {"OK"} with title "You Forgot One Little Detail..." with icon 2

end if



May 18, 2018 10:36 AM in response to Niel

Using your code...

tell application "Finder"

set sel to selection

if sel is not {} then

set filepath to item 1 of sel as text--> The path to the one selected item.

set label index of filepath to 5


--open filepath using application file id "com.apple.Compressor"

end if

end tell


I get this error

error "Can’t set «class labi» of \"Macintosh HD:Users:admin:Desktop:Screen Shot 2018-05-18 at 12.04.07 PM.png\" to 5." number -10006 from «class labi» of "Macintosh HD:Users:admin:Desktop:Screen Shot 2018-05-18 at 12.04.07 PM.png"

May 19, 2018 2:26 PM in response to chadsgreene

I hadn't forgot about you. In and out of the house all day running errands.


I have an add_tagnames handler that will set a tag/s on a file that has no tag, or add a tag/s to a file that already has a tag/s. It handles duplicates, so if there is an existing purple tag, it won't add a second purple tag. This handler would simply replace the set_tag handler that I posted earlier, and would avoid the tag/s overwrite problem.


I have a get_tagnames handler that does what it suggests, and returns a list of tags already on a file.


I also have a remove_tagnames handler that you pass the list of tag/s already on the file, and a list of tag/s to remove, and it will make the appropriate adjustment.


Either later today or tomorrow morning.

May 18, 2018 1:35 PM in response to chadsgreene

When you manually tag a file from the Finder, the resources com.apple.metadata:_kMDItemUserTags and Spotlight kMDItemUserTags are assigned the tag color name. AppleScript's set label index does neither, and cosmetically sets a color tag on the file(s).


One can programmatically achieve similar results to a manual Finder tag assignment using AppleScript/Objective-C. Though neither resource mentioned in paragraph 1 is assigned, one can still search and find tagged files using the Finder, or Spotlight after a programmatic application of the tag.


The following AppleScript/Objective-C solution will apply one (or more) selected tagnames to every selected file in the Finder. Where Finder in AppleScript only knows of the tag colors available from the system as integers, this solution will build a list of tag names, including custom tag names if you have created them. It presents this as a list where you can choose one or more tag names to assign to all selected files.


The following is a vertical/horizontal scrollable window that contains the code that you copy/paste into Script Editor. Click once in this view and then drag down until you have selected the last line of code.


-- seltags.applescript -- Gathers all tag names (including custom tag names if created) and presents this as a -- multi-selection list. The chosen tag name(s) will then be applied to all files -- currently selected in the Finder. Finder and Spotlight searches can find these -- tagged files. -- Version: 1.1 -- Tested: El Capitan 10.11.6, High Sierra 10.13.4 -- Author: VikingOSX, 2018-05-18, Apple Support Communities. use framework "Cocoa" use AppleScript version "2.4" -- Yosemite or later use scripting additions property NSWorkspace : a reference to current application's NSWorkspace 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 property tagFile : POSIX path of ((path to home folder as text) & "Library:SyncedPreferences:com.apple.finder.plist") as text property tagKey : "values:FinderTagDict:value:FinderTags" property PERLCMD : " | perl -lne '$_ =~ /^.*n = (.*?)$/ && print \"$1\"' " property lprompt : "For multiple tags, press ⌘-key, and click names." property allTags : {} set selList to {} set tagfileList to {} tell application "Finder" if selection is {} then return set selList to selection repeat with anItem in selList copy POSIX path of (anItem as text) to the end of tagfileList end repeat -- log tagfileList end tell -- if custom tag names were created in Finder this file will exist if exists (tagFile) = true then set allTags to paragraphs of (do shell script "/usr/libexec/PlistBuddy -c \"print " & tagKey & "\" " & tagFile & PERLCMD) else -- no custom tag names exist, so get default Finder tag names set allTags to (ca's NSWorkspace's sharedWorkspace()'s fileLabels()) as list end if set selTags to {} set selTags to (choose from list allTags with title "Tags Selection" with prompt lprompt default items "" with multiple selections allowed without empty selection allowed) if selTags is false then display alert "User pressed cancel, quitting." as critical giving up after 10 return end if repeat with anItem in tagfileList set_tag(anItem, selTags) end repeat return on set_tag(theFile, atag) -- atag is always a list, even if single tag element -- place single, or multiple tags from atag on the specified file set tagArray to NSArray's arrayWithArray:atag set fileURL to |NSURL|'s fileURLWithPath:theFile fileURL's setResourceValue:tagArray forKey:NSURLTagNamesKey |error|:(missing value) return end set_tag

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Tag selected file purple

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.