I’m using an Apple Shortcut to search for a random photo in Apple Photos which I then process. Once my process is finished, I want to mark that photo with a Keyword or Tag. The purpose is that in later searches I can omit that photo from the search result list.
From what I can see looking within the Shortcuts app it doesn’t seem like this is possible?
Update I’ve now looked into the situation further as to implementing it… It looks lie I’ve hit a few problems:
Apple Photos (on the Mac) allows you to add a ‘keyword’ to a photo - GOOD
Apple Photos (on the mac) does not support adding a hashtag at all - BAD
Apple Shortcuts can’t filter to ignore ‘keywords’ - BAD
Apple Shortcuts CAN filter based on a ‘name contains’ - GOOD
Apple Shortcuts / Apple Photos - neither seem to have an option to rename a photo - BAD
So as far as I can work out there isn’t any way of ‘marking’ a photograph that I’ve process it? ARGH
Unless there is the possibility of choosing the photograph to be processed (with relevant filters) using Apple Script, then once processed (again using Apple Script) to set the ‘flag’
Ideally what I want to use for image filter selection is: Photo is ‘Favourite’, Type ‘image’, does not include keyword ‘#mastochat’, a single random image is needed.
When the shortcut has finished performing its main tasks it needs to add the ‘#mastochat’ keyword to the image.
Does that make sense? Can anyone help or offer any thoughts?
Following on from this more research has been done. It has been referenced that:
You can add a name, description, favorite status, and keywords to media items in Photos via AppleScript. Open Script Editor, choose Open Dictionary from the File Menu, and select Photos. Then, look under Photos Suite > media item for the full list.
But I’ve no ideas as to how to access the data using Applescript with the source being an image selected from within the shortcut.
This reference also seems to show keyword manipulation is possible, again same caveat as above.
It also looks like installing ExifTool by Phil Harvey, can be used make Exif data accessible, but I’ve no ideas as to how it could be included in a script or shortcut.
Finally, I’ve looked at opening the Apple photo dictionary to see if I can read (at least) the Exit data (which would include keywords) but I can’t see any term that would be right to pull out.
Does any of the above make sense? Does it help towards finding a solution?
tell application "Photos"
keywords of item 1 of (get selection) -->"targetKeyword"
set keywords of item 1 of (get selection) to {"targetKeyword", "newKeyword"}
keywords of item 1 of (get selection) -->{"newKeyword", "targetKeyword"}
end tell
It will only be one photo processed at a time. And the keyword would be set to #MastoGPT
Of course once that is working I’d need to be able to use a companion script, earlier in the shortcut, to see if the keyword is already present on the file and stop the shortcut if it is found.
Sorry dbrewood, I’m not a Shortcuts user so I’m not the right person to ask about incorporating AS into Shortcuts. @peavine is probably the person I’d suggest.
dbrewood. I think Paul’s suggestion is an excellent one and works well in my testing. However, it requires that the photo is selected in the Photos app, and, based on your statement above, I’m not sure that’s part of your workflow.
Instead it appears that your workflow involves getting photos from a Find Photos action in a shortcut. As far as I know, there’s no way for an AppleScript in a shortcut to input the ID of a photo returned by a Find Files action.
Another possible approach seems to be to directly change the keyword which is stored in the metadata of the photo in a database in the user’s Pictures folder. From what I could find, that’s also not possible in a shortcut.
Is there an image selected in Photos.app by your Shortcut? If there is, then following AS ( in a shortcut’s ‘RunApplescript’ ) will add the Keyword “#mastochat” to the image if it doesn’t already exist.
on run {input, parameters}
tell application "Photos"
tell item 1 of (get selection)
set k to keywords
if k does not contain "#mastochat" then
try
set the end of k to "#mastochat"
set keywords to k
on error
set keywords to {"#mastochat"}
end try
end if
end tell
end tell
return k
end run
I really have no idea if this is helpful or not. ;^)
but still get:
I’ve tried changing the ‘input’ to the Apple Script to reference the ‘photos’ variable directly instead of using the sourcefile variable, but it makes no difference.
I was trying to avoid uploading the shortcut as it is very much a work in progress, but if it helps here is it is… MastoGPT 37 AS.shortcut (28.1 KB)
dbrewood. Just to get something basic working, I tested the following without issue. A single photo in the Photos app was selected when I ran this shortcut. The AppleScript input can be a shortcut list, but in that case {input} in the AppleScript will need to be changed to input.
I briefly looked at your shortcut and will spend more time with it later today. However, I now understand the issue. Based on what we know now, the target photo has to be selected in the Photos app. I had the idea that instead of the user having to manually select the photo there might be a Reveal Photo action but that doesn’t appear to be the case.
BTW, have you installed the current beta of Tahoe? I wondered if it might contain some new Photo app actions.
There might be a misunderstanding: selection in the Photos tell block refers to the currently selected items in the Photos app and obviously nothing is selected. The passed variable Source_File is not being used at all.
Thanks guys, yep the photo is selected via the earlier part of the shortcut, no manual selection is done at all. Sorry for the confusion guys.
Worst case, which I was hoping to avoid, is that I’ll locate the photograph manually in the Photos app and add the keyword manually. Before going down that route I’ll re-investigate Exif-Tools as I had used that in my Windows days, but I’m not sure if that can work within shortcuts, I know it can read / write Exif data, but if I recall it was a pain to use, and that is without adding the difficulties of Apple Script.
I can’t believe Apple have not added such functionality into the system.
I am open to any solution which allows mew to mark an image as ‘processed’ as long as it can be used (natively or via Apple script) to omit a photo from being processed again. So options of tagging, keywords, renaming etc can be considered. All as long as they don’t break the photos in the Photos App
Oh and I’m running the latest Tahoe Public beta (from last week), waiting on an update tonight or tomorrow. As far as I can see there are no new shortcut options which can assist.
I would be inclined to do all or part of the work that gets photos and set keywords with an AppleScript. The following is a simple example that works on my Sequoia computer.
tell application "Photos"
set thePhoto to id of first media item whose filename is "County Courthouse.jpg"
--set thePhoto to id of first media item whose keywords is "Not Processed" --instead of above
set keywords of media item id thePhoto to {"Processed"}
end tell
Sounds good, and a proof of concept I guess (if I understand correctly). So do you think something similar will be usable within my shortcut eventually?
I’ll hold off working on the shortcut for a while now while you guys hopefully come up with viable AppleScarpt to set / read the keywords.
Run the shortcut, it automatically selects a photograph based on the defined filters.
It parses the data and uses the ChatGPT system (as if used within Apple Shortcuts no cost / limit is incurred), to create a main description and ‘ALT’ image description for Mastodon
A copy of the image is exported.
Both descriptions are then used to manually create a mastodon post
The ‘quick look’ ‘log’ is then used to locate the photo in Apple Photos so I can mark the image as ‘processed’
Eventually I aim to get the Shortcut to ‘preview’ the image and give the okay to go ahead with the posting. Once approved I’m looking for the post process to be automatic within the Shortcut.
Does that clarify and make sense?
I’m more than happy to do everything via Apple Script (As long as the process still avoids the ChatGPT charges). If not I’ll stick with the process I have now as I can’t afford the ChatGPT costs…
Thanks, that’s helpful, and it gives me an idea how this might be made to work with a shortcut which would include two AppleScript actions. For example,
In the shortcut, get or set filters that will be used. These would probably be text or booleans.
Use an AppleScript to filter photos and to return Media Item IDs plus other necessary identifying data (e.g. file name, album, etc). AppleScript does not appear to have a media type property, though.
Use a Find Photos action with data from step 2 to get the desired photos. This is the part that may or may not work.
Do the other stuff you outline. I won’t work on this.
Set the keyword of the photos using an AppleScript by way of the Media Item ID gotten in step 2.
The part I don’t understand in your workflow is the quick-look log, but perhaps that will be made clear by your shortcut.
If this doesn’t seem feasible for some reason please let me know. Otherwise, I’ll work on this later today.
The ‘quick look log’ contains debug data and things like the image timestamp etc. Just data used to locate the photograph in the album. At the moment I copy / paste that into a text file before allowing the shortcut to continue. It also allows me to highlight certain issues like one I saw earlier when a photo had missing town data so a hashtag of '# was created.
I’m fine to carry on with 4 I understand that So go play nicely. Please yell out if you have any more questions. And hey as always this is very much appreciated.
When I re-coded the shortcut to use the file name I found that file name and extension data in the photos are in two parts, file name and extension as well as file path. These are found in three different areas of ‘Photos’:
PhotoMedia name: England, Northumbria, Druridge Bay Country Park - 2017-06-24-12.00.48-588
PhotoMedia file extn: jpeg
Photo Media file path: /Users/dbrewood/Library/Group Containers/group.com.apple.shortcuts/Temporary/com.apple.WorkflowKit.BackgroundShortcutRunner/England, Northumbria, Druridge Bay Country Park - 2017-06-24-12.00.48-588.jpeg
Image name: England, Northumbria, Druridge Bay Country Park - 2017-06-24-12.00.48-588
Image extn: jpeg
PhotoMedia name: England, Northumbria, Druridge Bay Country Park - 2017-06-24-12.00.48-588
PhotoMedia file extn: jpeg
Photo Media file path: /Users/dbrewood/Library/Group Containers/group.com.apple.shortcuts/Temporary/com.apple.WorkflowKit.BackgroundShortcutRunner/1F75BA3C-CD12-42AC-84B3-E879AA76F07C/England, Northumbria, Druridge Bay Country Park - 2017-06-24-12.00.48-588.jpeg
Therefore the extension types for a JPG for differ between what the Photos data gives in the shortcut (typically jpeg) and the actual; file extension.(jpg).
I’ve not found a solution to that one yet.
Anyway the latest version of my shortcut is attached, with a lot of debug data being shown via Quick Look