Help Tagging Photos with same name using a Quick Action

Hello, all! Found this forum from a Reddit thread and thought you might be able to help!

I have a need for a Quick Action that will help me tag photos. I need to create an Apple Automator script that will help me to speed up organizing tagged photos in a folder. Usually, I will have a folder with anywhere from 500-2000 total files that I will go through, preview the JPG images, and tag the best ones with a green tag. I want to be able to select all the files (or select the folder) and right-click, go to quick actions, and have it select/tag all the .RAF files (red) that have the same name as green-tagged images.

I have attempted many times over the past 6 months to make something like this with Automator/Script Editor. I have endlessly researched this to see if this has been asked on the internet, and I have not been able to find anything I can use. Or if I have, it has not worked.

I have attached a mock-up video/gif of how I would like it to work. If anyone has an idea on how to get something like this up and running, I would be extremely grateful. Let me buy you a beer/coffee, please.
Tagging Quick Action Example

I’d do that with a shell script because that seems (to me) to be easier than scripting it with AS. AFAICT, the Finder dictionary does not support setting nor inquiring tags (I may be overlooking something obvious there), so one would have to go through Objective-C, which seems a bit convoluted.

#!/bin/bash

IFS=$'\n'
for f in $(mdfind -onlyin "$PWD" 'kMDItemUserTags = Green')
do
  baseName=$(basename "$f" .JPG)
  RAFName="$baseName".RAF
  if [ -f "$RAFName" ] ; then
     xattr -w com.apple.metadata:_kMDItemUserTags '("Red\n6")'  "$f"
  fi
done

Here’s what happens:

  • The IFS lines set the shell’s field separator to newline so that processing of files with spaces in their names is easier
  • the $(mdfind …) thingy finds all files with a green tag in the current directory
  • the for … done loops over these files and for each of them
    • removes the ending JPG to find the base name (using the aptly named basename command)
    • checks if a file with the same name but the extension RAF exists (using the if construct)
    • if so, adds a red tag to this file

The script is only minimally tested, but since it’s fairly trivial, possible bugs should be easy to fix. The xattr and mdfind calls work as they are used here. I suppose one could add this script to automator somehow, but I’m not using this program myself.

2 Likes

Try this. It’s a bit clunky and there is no error checking (it will barf if you run it on a folder without any images) but I think it basically works:

set imageFolder to (choose folder)
tell application "Finder"
	set {taggedJPGs, allRAFs} to {name of every file of imageFolder whose label index is 6 and name extension is in {"JPG", "JPEG"}, every file of imageFolder whose name extension is "RAF"}
end tell
set AppleScript's text item delimiters to "."
set taggedJPGNames to {}
repeat with eachTaggedJPG in taggedJPGs
	set end of taggedJPGNames to text item 1 of eachTaggedJPG
end repeat
repeat with eachRAF in allRAFs
	tell application "Finder"
		set RAFname to text item 1 of (name of eachRAF as string)
		if RAFname is in taggedJPGNames then set label index of eachRAF to 2
	end tell
end repeat

If it does what you want it could be converted into an Automator Quick Action without much trouble.

Edited for correct display

1 Like

So, it is possible to do it with AS and Finder – thanks for clarifying that. But I think the labels should be the other way around: search for green (index 2) and set to red (index 6).

This is my ancient list of label index colours:

– No colour = 0

– Orange = 1

– Red = 2

– Yellow = 3

– Blue = 4

– Purple = 5

– Green = 6

– Grey = 7

The script finds the green-tagged jpgs (6) and flags the corresponding RAFs with red (2) as per the OP’s request.

Interesting. With kMDItemUserTags, green is 2 and red is 6. I guess that’s another of AppleI’m inconsistencies.

THIS WORKED! Exactly the way I wanted to. Seriously, I can not thank you enough. I have wanted to do something like this for over 2 years now, but my knowledge of anything coding is slim.

You are my hero.

Is there a way I can buy you a coffee/beer? PayPal? Anything. I am so grateful for this.

1 Like

Glad it worked! Very kind offer, but the mental exercise is its own reward.

Best,

H

1 Like

Looks like the lists are reversed. kMDITEMFSLabel for orange is 7 (1 using AS+Finder), while for grey kMDITEMFSLabel is 1 (7 for AS+Finder). Very odd…

So, there’s kMDItemFSLabel (set by the label property in tinder?), and kMDItemUserTags (set by my script, using xattr). Seems others are confused, too:

It seems that a file can only have one label, but several tags.

After testing, it looks like there is a rough limit before it times out completely. At 76 files, it took about 3 mins to complete. Anything past that, it times out. On smaller batches, let’s say 10-20 files, It gets done in about 5-20 seconds.

I think I may be asking WAY too much from an Apple Script. Since I will be needing to process sometimes up to 3000 files I will have to continue searching to find an application that can handle that many.

Thank you for helping, hubert0! (If you happen to know of any applications that would be able to handle a load like I am needing, suggestions would be greatly appreciated!)

I was able to run this using Automator, and it works well! Just the same as @hubert0’s. However, sadly it runs into the same limitation where at about 75 files, it takes about 3 min, and anything past this it would most likely time out (however, I did not do a test large enough to try this). 10-20 files, it works in about 5-20 seconds.

Thank you for being so helpful with this! I appreciate it.

I if you see the same timing behavior with both solutions, it’s not AS related, since my script doesn’t use that. I suppose that it’s simply Apple’s frameworks that are slow here.

Especially since the behavior ist not linear: if 20 files take 20 seconds, 75 should not take more than 75 seconds. It will of course not get faster for 5000 than 5000 seconds, which is well more than an hour.

Maybe moving the files to another location instead of tagging them is faster.

Amended version of the shell script, not using basename anymore. That might speed things up a little bit, but I still suppose that the bottleneck is the underlying framework. In addition, one could try to

  • build a list of all possible RAF files corresponding to JPGs, regardless of their existence
  • pass that list onto xattr instead of calling it for every single file.

That approach might produce error messages if there’s no RAF for every JPG, though.

#!/bin/bash

IFS=$'\n'
for f in $(mdfind -onlyin "$PWD" 'kMDItemUserTags = Green')
do
  RAFName=${f/.JPG/".RAF"}
  if [ -f "$RAFName" ] ; then
     xattr -w com.apple.metadata:_kMDItemUserTags '("Red\n6")'  "$f"
  fi
done

UPDATE

With the help of user ccstone over at forum.keyboardmaestro.com, there is a working macro that can be installed and triggered via Apple Script, to achieve a result like the GIF above. It is INCREDIBLY fast.

I am still grateful for the helpful community here, however. I just wanted to update this post in case someone in the future has a similar want/issue and wants to find a solution that worked well.