Add Finder Tags to File Name

I am looking for a way to add the Finder Tags on a specific file to the end of its file name. For instance, I have a folder of photos and videos labeled:

  • Photo1
  • Photo2
  • Photo3
  • Video1

Each file has been tagged uniquely and I would like to append these tags to the end of the file name so they look like this:

  • Photo1 tag1 tag2
  • Photo2 tag2 tag3
  • Photo3 tag1 tag4
  • Video1 tag 2 tag3 tag5


Is there a way to do this with applescript or automator? I would love to select all the files in a folder, right-click and chose this from the services menu.

Mac Pro, OS X El Capitan (10.11.6)

Posted on Nov 28, 2017 9:51 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 1, 2017 3:04 PM

Replace the entire contents of the current Run AppleScript in your Service with the following code. Here are the updated processing results:

User uploaded fileUser uploaded file

Also, the AppleScript/Objective-C code used to extract the tag names requires Yosemite or later, and it was actually developed on El Capitan 10.11.6.


Code: (scrollable window)


use framework "Foundation" use AppleScript version "2.4" -- Yosemite or later use scripting additions on run {input, parameters} tell application "Finder" activate set media_selected to (selection as alias list) try repeat with anItem in media_selected set tagnames to my get_tagnames(POSIX path of anItem) if not tagnames = {""} then set newname to my append_tagnames(anItem, tagnames) tell application "Finder" to set name of (anItem as alias) to newname else log anItem -- faux continue statement end if end repeat on error errmsg number errnbr my error_handler(errnbr, errmsg) return end try end tell return input end run on get_tagnames(theFile) -- get the names of any tags assigned to theFile -- return a list of these tag names set NSURLTagNamesKey to {"NSURLTagNamesKey"} set metadict to current application's NSDictionary's dictionary set fileURL to current application's NSURL's fileURLWithPath:theFile set metadict to fileURL's resourceValuesForKeys:NSURLTagNamesKey |error|:(missing value) if not metadict = {""} then return metadict's NSURLTagNamesKey as list end if return metadict as list end get_tagnames on append_tagnames(theFile, tags) set DELIM to {space, "."} set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM} set appendage to space & (tags as text) -- string of white-space punctuated tagnames -- split filename and extension to elements of list. Only works for one "." in filename tell application "Finder" set file_parts to text items of (name of (theFile as alias) as text) end tell set newname to ((items 1 thru -2 of file_parts) as text) & appendage & "." & (last item of file_parts) set AppleScript's text item delimiters to TID return newname end append_tagnames on error_handler(nbr, msg) return display alert "[ " & nbr & " ] " & msg as critical giving up after 10 end error_handler

14 replies
Question marked as Top-ranking reply

Dec 1, 2017 3:04 PM in response to chadsgreene

Replace the entire contents of the current Run AppleScript in your Service with the following code. Here are the updated processing results:

User uploaded fileUser uploaded file

Also, the AppleScript/Objective-C code used to extract the tag names requires Yosemite or later, and it was actually developed on El Capitan 10.11.6.


Code: (scrollable window)


use framework "Foundation" use AppleScript version "2.4" -- Yosemite or later use scripting additions on run {input, parameters} tell application "Finder" activate set media_selected to (selection as alias list) try repeat with anItem in media_selected set tagnames to my get_tagnames(POSIX path of anItem) if not tagnames = {""} then set newname to my append_tagnames(anItem, tagnames) tell application "Finder" to set name of (anItem as alias) to newname else log anItem -- faux continue statement end if end repeat on error errmsg number errnbr my error_handler(errnbr, errmsg) return end try end tell return input end run on get_tagnames(theFile) -- get the names of any tags assigned to theFile -- return a list of these tag names set NSURLTagNamesKey to {"NSURLTagNamesKey"} set metadict to current application's NSDictionary's dictionary set fileURL to current application's NSURL's fileURLWithPath:theFile set metadict to fileURL's resourceValuesForKeys:NSURLTagNamesKey |error|:(missing value) if not metadict = {""} then return metadict's NSURLTagNamesKey as list end if return metadict as list end get_tagnames on append_tagnames(theFile, tags) set DELIM to {space, "."} set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM} set appendage to space & (tags as text) -- string of white-space punctuated tagnames -- split filename and extension to elements of list. Only works for one "." in filename tell application "Finder" set file_parts to text items of (name of (theFile as alias) as text) end tell set newname to ((items 1 thru -2 of file_parts) as text) & appendage & "." & (last item of file_parts) set AppleScript's text item delimiters to TID return newname end append_tagnames on error_handler(nbr, msg) return display alert "[ " & nbr & " ] " & msg as critical giving up after 10 end error_handler

Nov 28, 2017 2:11 PM in response to chadsgreene

The standard Finder tag color names, or custom tag names that you have created in the Finder Preferences? You can assign multiple tag colors to a given file via the Finder secondary menu too, and avoid the actual trailing name clutter, and overall extended filename length. These would then be searchable in a Finder window, or Spotlight with tag:blue AND tag:red, as an example.


Although an Automator Service can operate on selected files in the Finder, how is it to reproduce the logic of appending multiple, different tag names to individual photo or video files. Is there a key about the respective filename that provides a logic to this process of appendant tag names?

Nov 28, 2017 2:30 PM in response to VikingOSX

I am talking about custom tags not color labels. Maybe a little context would help.


I work for a video production company. We send multiple cameras to an event. The video and stills come back and we add some custom tags such as Interview, Broll, Location, Person... At this point I can search the finder for tag info and find what I am looking for. However, our Archiving catalog does not search tag info so I would like to append this basic tag info to the name before Archiving and using.

Nov 28, 2017 3:21 PM in response to chadsgreene

Clarity is in the details. So you created the italicized tag names in the Finder for name (not color) recognition that accommodate your archiving catalog limitation with Finder tags.


Again, what logic does automated software follow to assign the correct tag names to the selected media files?


The Finder's scripting definitions do not give access to custom tag names. I had previously wrote some code that achieves this, so that is not a hurdle.

Nov 29, 2017 3:46 PM in response to chadsgreene

Ok. I have an Automator Service that appends individual tag names to Finder selected files. Here is the before and after Finder list view:

User uploaded fileUser uploaded file

You select the individual files in the Finder, and then right-click to access the Services menu from the secondary menu. I named this Service: Append Tag Names. I do not attempt to process files that have multiple "." in the filename.


One Automator Library action: Utilities : Run AppleScript. In Sierra or later, Run AppleScript is in the Automator library.


  1. Dock : Launchpad : Other : (click Automator).
  2. New : Service and click Choose.
  3. Library : Utility : Run AppleScript. Drag/Drop to larger work flow window.
    1. Settings:
      User uploaded file
    2. Remove all boilerplate text in the Run AppleScript window, and replace with copy/paste of source code in A
    3. Click the hammer icon in the Run AppleScript action to compile copy/paste content from 3b above
    4. Automator File menu : Save... Append Tag Names (no extension, and name is your choice)
  4. Quit Automator.
  5. Select target media files, right-click, and Service menu : Append Tag Names


Code A (copy/paste all content in this scrollable window)


use framework "Foundation" use AppleScript version "2.4" -- Yosemite or later use scripting additions on run {input, parameters} tell application "Finder" activate set media_selected to (selection as alias list) repeat with anItem in media_selected set tagnames to my get_tagnames(POSIX path of anItem) if not tagnames = {""} then set newname to my append_tagnames(anItem, tagnames) if not newname is false then tell application "Finder" to set name of anItem to newname else log anItem -- serve as a faux continue statement end if else log anItem end if end repeat end tell return input end run on get_tagnames(theFile) -- get the names of any tags assigned to theFile -- return a list of these tag names set NSURLTagNamesKey to {"NSURLTagNamesKey"} set metadict to current application's NSDictionary's dictionary set fileURL to current application's NSURL's fileURLWithPath:theFile set metadict to fileURL's resourceValuesForKeys:NSURLTagNamesKey |error|:(missing value) if not metadict = {""} then return metadict's NSURLTagNamesKey as list end if return metadict as list end get_tagnames on append_tagnames(theFile, tags) set DELIM to {space, "."} tell application "Finder" set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM} set appendage to space & (tags as text) -- string of white-space punctuated tagnames -- split filename and extension to elements of list. Only works for one "." in filename set file_parts to text items of (name of theFile as text) -- guard agains multiple dots in filenames if (count of file_parts) > 2 then return false as boolean -- filename has multiple "." set AppleScript's text item delimiters to TID set newname to (item 1 of file_parts) & appendage & "." & (rest of file_parts) end tell return newname end append_tagnames

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.

Add Finder Tags to File Name

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