Using Automator to apply tags to selected files

Using Catalina 10.15.7


I've created a Quick Action script in Automator to copy selected folders in Finder from one location to another, once moved to then rename / replace a text string in the folder name.


Now I want Automator to do a further step to apply two colour tags (green & grey) which have custom names but only to the selected folders in Finder where they have been copied from, not where they've been copied to.


I've done a bit of searching and can see you need to run an AppleScript to accomplish this but not sure how to proceed from there. Thanks for any pointers in advance.

MacBook Pro 15″

Posted on Aug 22, 2024 6:12 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 24, 2024 5:53 AM

If all you are selecting in the Finder are Folders, then change the Workflow receives [ folders ] in [ Finder ]. As a Quick Action, you select one or more folders in the Finder, right-click on the first and choose Quick Actions > name of your action.


Whatever emits from your last Rename Finder Items: Replace Text action before the Run AppleScript will be in that on run {input, parameter} content. Are you sure that is an originating folder path? If not, at some point in your Automator workflow, you will need to assign that originating folder or list of originating folders to a set value of variable action, and then provide a Get Value of Variable just before your Run AppleScript so that the list of originating folders is fed to the tagging loop operation.

Similar questions

27 replies
Question marked as Top-ranking reply

Aug 24, 2024 5:53 AM in response to pinkbeats

If all you are selecting in the Finder are Folders, then change the Workflow receives [ folders ] in [ Finder ]. As a Quick Action, you select one or more folders in the Finder, right-click on the first and choose Quick Actions > name of your action.


Whatever emits from your last Rename Finder Items: Replace Text action before the Run AppleScript will be in that on run {input, parameter} content. Are you sure that is an originating folder path? If not, at some point in your Automator workflow, you will need to assign that originating folder or list of originating folders to a set value of variable action, and then provide a Get Value of Variable just before your Run AppleScript so that the list of originating folders is fed to the tagging loop operation.

Aug 22, 2024 8:44 AM in response to pinkbeats

Had a mess around with ChatGPT, the scripts it was spitting out didn't really work. The files are being being copied from an external drive, so I'm not sure if that had anything to do with it. I could only get one tag to be applied and then by only using a number, which wasn't the correct one and wasn't the custom name.


So, ChatGPT suggested installing a CLI tag programme using Homebrew which I do use a bit, so I installed that:


brew install tag


and then in Automator, added a further step 'Run AppleScript' and copied and pasted the code below into that, seems to work...


tell application "Finder"
	-- Get the selected folders in Finder
	set selectedFolders to selection
	
	-- Check if there are any selected items
	if selectedFolders is {} then
		display dialog "No folders selected." buttons {"OK"} default button "OK"
		return
	end if
end tell

-- Initialize a success flag
set success to true

-- Loop through the selected items
repeat with aFolder in selectedFolders
	try
		-- Apply the Green tag
		do shell script "/usr/local/bin/tag --add 'CustomTagName_1' " & quoted form of POSIX path of (aFolder as alias)
		
		-- Apply the Gray tag
		do shell script "/usr/local/bin/tag --add 'CustomTagName_1' " & quoted form of POSIX path of (aFolder as alias)
		
	on error errMsg number errNum
		set success to false
		display dialog "Error applying tags to " & (name of aFolder) & ": " & errMsg buttons {"OK"} default button "OK"
	end try
end repeat

-- Display a dialogue only if there was an error
if success is false then
	display dialog "One or more tags could not be applied." buttons {"OK"} default button "OK"
end if

Aug 24, 2024 2:48 AM in response to pinkbeats

Think we've got there, this seems to work


use taglib : script "Tag_cafe"
use scripting additions

-- You need to make sure `taglib` is correctly set up in your environment
-- and that it supports the methods you are trying to call.

on run {input, parameters}
	
	tell application "Finder"
		-- Get the selected folders in Finder
		set selectedFolders to selection
		
		-- Check if there are any selected items
		if selectedFolders is {} then
			display dialog "No folders selected." buttons {"OK"} default button "OK"
			return
		end if
	end tell
	
	-- Initialize a success flag
	set success to true
	
	-- Loop through the selected items
	repeat with aFolder in selectedFolders
		try
			-- Ensure the path is correctly obtained for `taglib`
			set folderPath to POSIX path of (aFolder as alias)
			
			-- Add both custom tags and avoid duplicate tag names
			taglib's add_tags(folderPath, {"On NAS", "Not yet on LMS"})
			
		on error errMsg number errNum
			set success to false
			display dialog "Error applying tags to " & (name of aFolder) & ": " & errMsg buttons {"OK"} default button "OK"
		end try
	end repeat
	
	-- Display a dialog only if there was an error
	if not success then
		display dialog "One or more tags could not be applied." buttons {"OK"} default button "OK"
	end if
	
end run


Thanks for persevering with me. 👍

Aug 22, 2024 10:05 AM in response to pinkbeats

From an AppleScript perspective, I wrote a script library named Tag_cafe.applescript and saved it into ~/Library/Script Libraries/Tag_cafe.scptd. That is a script bundle saved from Script Editor. When you assign a tag name to a file, the color associated with that tag name appears on that file.


Now, in your AppleScript that you are using in Automator as a Run AppleScript action, you can use the handlers from that Tag_Cafe like so:


use taglib : "Tag_cafe"
use scripting additions

repeat with aFolder in selectedFolders
  try
     # add both custom tags and avoids duplicate tag names
     taglib's add_tags(aFolder, "Custom Green tagname", "Custom Gray tagname"])
  on error errMsg number errNum
     set success to false
     display dialog …
  end try
end repeat



Here is the Tag_cafe.applescript source:



Aug 24, 2024 3:21 AM in response to pinkbeats

The input entity is a list of the selected folders in Finder before running the QA. You don't need any of this code in an Automator QA, but would in a standalone AppleScript:


tell application "Finder"
	-- Get the selected folders in Finder
	set selectedFolders to selection
	
	-- Check if there are any selected items
	if selectedFolders is {} then
		display dialog "No folders selected." buttons {"OK"} default button "OK"		return
	end if
end tell

If you haven't selected any folders as input for the QA, then there will be no means to run it and thus no reason to check for {}.


Instead, try this code without invoking Finder:

-- Initialize a success flag
set success to true

-- Loop through the selected items
repeat with aFolder in input
	try
			# add both custom tags and avoids duplicate tag names
			taglib's add_tags(aFolder, "On NAS", "Not yet on LMS")
		
	on error errMsg number errNum
		set success to false
        set folderName to name of (info for aFolder)
		display dialog "Error applying tags to " & folderName & ": " & errMsg buttons {"OK"} default button "OK"
	end try
end repeat

-- Display a dialogue only if there was an error
if success is false then
	display dialog "One or more tags could not be applied." buttons {"OK"} default button "OK"
end if

end run



Aug 23, 2024 5:25 AM in response to pinkbeats

When you copy and paste the original Tag_cafe code into the Script Editor, you click the hammer icon to compile it from purple to multi-color text. At that point, you just Save it to your Desktop as Tag_cafe.applescript. Next, you press the option key while clicking the Script Editor's File menu > Save As… and now you select File Format: Script Bundle and no options. The Save As: name field will now be Tag_cafe.scptd. At this point, you can temporarily save this to your Desktop, or directly to /Users/username/Library/Script Libraries, or /Library/Script Libraries.


Automator does not know your PATH variable so it may not find that script bundle in your local Script Library location. You might move it to /Library/Script Libraries and in the Automator Run Applescript action, provide the full path to Tag_cafe:


use taglib : "/Library/Script Libraries/Tag_cafe.scptd"



In Finder, press shift+cmd+G (e.g. Go To Folder) and enter /Library/Script Library followed by a return. This opens a Finder Window in that location and you can then drag and drop Tag_cafe.scptd into it.

Aug 24, 2024 2:19 AM in response to VikingOSX

Thanks for your suggestion, ok going to use this code 👇


tell application "Finder"
	-- Get the selected folders in Finder
	set selectedFolders to selection
	
	-- Check if there are any selected items
	if selectedFolders is {} then
		display dialog "No folders selected." buttons {"OK"} default button "OK"
		return
	end if
end tell

-- Initialize a success flag
set success to true

-- Loop through the selected items
repeat with aFolder in selectedFolders
	try
			# add both custom tags and avoids duplicate tag names
			taglib's add_tags(aFolder, "On NAS", "Not yet on LMS")
		
	on error errMsg number errNum
		set success to false
		display dialog "Error applying tags to " & (name of aFolder) & ": " & errMsg buttons {"OK"} default button "OK"
	end try
end repeat

-- Display a dialogue only if there was an error
if success is false then
	display dialog "One or more tags could not be applied." buttons {"OK"} default button "OK"
end if

Aug 23, 2024 12:04 AM in response to VikingOSX

Well firstly, thanks for taking the time to write all this.


Unfortunately, for the moment, I can't get it to work, when attempting to test / compile (the hammer icon) the Run AppleScript step in the Automator workflow, I get the error message:


Syntax Error
Expected script or application but found Tag_cafe.


In a moment, I'll go through and check the steps against what you instructed and post feedback. Thank you.

Aug 23, 2024 12:27 AM in response to VikingOSX

Steps I took were:

  • copied the Tag_cafe.applescript source into Script Editor
  • saved as File Format: Script Bundle, filename Tag_cafe.scptd into /MyUserName/Library/Script Libraries/ however there wasn't a Script Libraries (there is one called just Scripts)folder so I had to create one (nor was there one under the system Library folder either)
  • in Automator, wasn't sure what to put here after your last post but after trying various combinations, I get the same error when trying to the run this step


use taglib : "Tag_cafe"
use scripting additions

on run {input, parameters}

repeat with aFolder in selectedFolders
  try
     # add both custom tags and avoids duplicate tag names
     taglib's add_tags(aFolder, "Custom Green tagname", "Custom Gray tagname"])
  on error errMsg number errNum
     set success to false
     display dialog …
  end try
end repeat

end run


I've made some error somewhere! Thanks for your assistance.


Aug 23, 2024 10:12 AM in response to VikingOSX

Thanks for helping me troubleshoot this but not matter where I put Tag_cafe.scptd, I get the same error when attempting to compile it in Automator:


Syntax Error


Expected script or application but found /Users/Username/Library/Script Libraries/Tag_cafe.scptd.


In each case, I am specifying the full path to Tag_cafe within the Automator action.


I don't know if it's worth noting that there is no Script Library or Script Libraries folder in the locations and I have to create one but that's ok.


I'm double-checking what I'm doing and struggling to see where I've gone wrong.

Aug 24, 2024 1:29 AM in response to VikingOSX

Thanks, that's moved it on.


Now getting:


Syntax Error

The variable selectedFolders is not defined.


AppleScript in Automator:


use taglib : script "Tag_cafe"
use scripting additions

on run {input, parameters}
	
	repeat with aFolder in selectedFolders
		try
			# add both custom tags and avoids duplicate tag names
            taglib's add_tags(aFolder, "Custom Green tagname", "Custom Gray tagname")
		on error errMsg number errNum
			set success to false
			display dialog "Error applying tags to " & (name of aFolder) & ": " & errMsg buttons {"OK"} default button "OK"
		end try
	end repeat
	
end run




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.

Using Automator to apply tags to selected files

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