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

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

OK changed...


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, "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
	
end run


Still getting The variable selectedFolders is not defined.

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

OK, amended AppleScript code to


use taglib : script "Tag_cafe"
use scripting additions

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
			# 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

end run


when run get, now get this error


Error applying tags to MySelectedFolder: Can’t make «class cfol» "MySelectedFolder" of «class cdis» "ExternalHDD" of application "Finder" into the expected type.


Aug 24, 2024 3:41 AM in response to VikingOSX

Thanks for your reply.


OK, have taken your suggestions on board and have amended the code to:


use taglib : script "Tag_cafe"
use scripting additions

on run {input, parameters}
	
	-- 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


Unfortunately, the result of this was no tags being applied to where the folder was being copied from and a single green tag being applied to where it was copied to.

Aug 24, 2024 5:36 AM in response to VikingOSX

Yes, that's what I want. For the tags to be applied to the folders at the source.


Going back to your earlier comment, I understand why you said there is no need for


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 ifend tell



this code, as yes, the whole workflow is dependent on a selection of folder(s) in Finder, yet it's not working unless some code in that section is included, maybe just the

set selectedFolders to selection


I don't know, as you've no doubt grasped, I'm not a coder and just trying different things to see what works.


Could it be to do with the prior steps in the Workflow?



So plain language, my steps are:

  • copy the selected folder(s) in Finder to the destination
  • rename those folders in the destination if applicable
  • apply the two custom tags to the folders at source

Aug 24, 2024 6:25 AM in response to VikingOSX

Right, so if I understand correctly, it's what happens after the last Rename Finder Items, that in the AppleScript it appears that it is necessary to specify the originating Folders?


Is that the reason why it works with the tell application "Finder" piece of code?


If that works, is it not best practice to include it or is there a better more elegant way to specify in the AppleScript to assign the originating folder or list of originating folders as a variable?


Thanks for your continued engagement, hope it doesn't feel too much like having your fingernails pulled out… 😉

Aug 24, 2024 7:10 AM in response to pinkbeats

Then put your Finder relevant code back into the AppleScript; ignore input, and change the repeat loop target to selectedFolders, and you may be back on track. Note that I would keep this code:


set folderName to name of (info for aFolder)


in the on error branch as your (name of aFolder) would blow up since name is a Finder reserved word and you are not in a Finder block there. My use of name in the above line of code is because name is a key in the (info for aFolder) dictionary.

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.