Automator Folder Script to TAG files with a Dialog Box

Hello friends!
Can anybody help me with this question?
I would like to create an Automator folder script, in which the moment a file was added to this folder, a dialog box was opened to enter a TAG to be assigned to this file.

Mac Pro, macOS Sierra (10.12.5)

Posted on Jul 15, 2017 12:52 PM

Reply
12 replies

Jul 29, 2017 12:25 PM in response to Luis Scarparo

Ok. Have a functionally correct Automator Folder Action that retrieves all available Tag names, and presents this as a selectable list once files are dropped on the assigned folder. It then sets each dropped file with these selected Tag(s). The dropped files are not moved out of this folder, per your requirements, but proper Folder Action implementation recommends they do not remain in the drop folder.


User uploaded file

User uploaded fileUser uploaded file


I have tested this on Yosemite and macOS Sierra without issue. Works as it should. With the exception of your specific folder name, you copy/paste the entire AppleScript below, into an Automator Run AppleScript action — after you have selected the default content, and removed it. How to configure in Automator was described in a similar post recently.


Save the Automator Folder Action (e.g. selTags), and selTags.workflow will be written into your ~/Library/Workflows/Applications/Folder Actions location. You can open this workflow using the Finder secondary menu's Open With : Automator, or by accessing Automator's Open Recent menu.


AppleScript:


-- select_tags.applescript (* Automator Folder Action using Run AppleScript action. Drag and drop files on folder assigned to this script, and a list of known Tag names will appear allowing assignment of single, or multiple tag names to all dropped files. The requirements for this folder action chose not to move the post-processed files to another external folder location. Any subsequent Tag name changes on the folder items should be performed interactively via the secondary Finder Tags... menu item. Requires Yosemite 10.10 or later. Tested: El Capitan 10.11.6, macOS Sierra 10.12.6 Version: 1.1 Author : VikingOSX, 2017-07-29, Apple Support Communities *) property ca : current application -- this file only exists if you created custom Finder Tag names in Finder Preferences. 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 : {} use framework "Cocoa" use AppleScript version "2.4" -- Yosemite 10.10 or later required use scripting additions on run {input, parameters} -- 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 end if repeat with anItem in input set_tag(anItem, selTags) end repeat return end run on set_tag(theFile, atag) -- place single, or multiple tags in atag on the specified file set tagArray to ca's NSArray's arrayWithArray:atag set fileURL to ca's |NSURL|'s fileURLWithPath:(POSIX path of theFile) fileURL's setResourceValue:tagArray forKey:(ca's NSURLTagNamesKey) |error|:(missing value) return end set_tag

Jul 28, 2017 7:28 AM in response to Luis Scarparo

In AppleScript, the possibility exists to get all of the active Finder tag names (those with their boxes checked in Finder Preferences : Labels, and present this as a scrollable list. You select the tag name from this list, and then that tag name (and its color) can be applied to the particular active file that was dropped on the folder.


I wrote a folder action on Jul 2 that applied a specific tag name (and color) to any file dropped on a specified folder. It shouldn't be too much work to adapt it to the first paragraph.

Jul 28, 2017 8:30 AM in response to Luis Scarparo

I don't have a complete integrated example. I have code that can retrieve all System and user-defined tag names into an AppleScript list. That tag list can be fed to a choose list prompt as input, so the tag names appear in a scrollable list for single, or multiple selection.


Questions for you before I can make modifications and post a custom solution:

  1. Will you ever drop more than one file?
  2. Will you need the ability to set more than one tag name on a dropped file? The choose list feature can be set for single, or multiple selection.
  3. Are the freshly tagged files to be then moved/copied to a folder outside of the dropped folder?

Jul 28, 2017 7:56 PM in response to VikingOSX

Thanks for the help my friend, I will be grateful!

  1. Will you ever drop more than one file?
    Yes, it could be one or more files.
  2. Will you need the ability to set more than one tag name on a dropped file? The choose list feature can be set for single, or multiple selection.
    Yes, you could select one or more tags.
  3. Are the freshly tagged files to be then moved/copied to a folder outside of the dropped folder?
    No, the files would be in the same folder they were dropped to.

Jul 29, 2017 8:24 AM in response to Luis Scarparo

Once you have dropped the file(s), and set the initial tag(s), you cannot run the script again to change them.


You can however, interactively add, change, or remove the tag(s) on single/multiple file(s) in the drop folder. Right-click/control-click/two-finger tap on the tagged file, and from the secondary menu, choose Tags... . Depending where your insertion point is located, you can add, change, or remove tags on one, or multiple files. Always press return in the Tags... field to set the changes.


Working on the drop folder script now...

Jul 29, 2017 1:49 PM in response to Luis Scarparo

Good.


I found a custom Swift function on stackoverflow that retrieves all System plus User Tag names. However, it does require at minimum that one has the non-default Swift interpreter/compiler installed, and mixing AppleScript and Swift together remains taboo.


I did not want to hold up your script solution while I attempted to translate that Swift code into AppleScript/Objective-C, or PyObjC, so the do shell script approach was effective use of time.

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.

Automator Folder Script to TAG files with a Dialog Box

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