Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Anyone scripting Photos.app yet?

I have just started exploring the (quite limited) Applescript support in the new Photos app introduced with OS X 10.10.3.


One of my first tests was to try adding a keyword to every "media item" (photos & videos) to resolve a discrepancy in the item counts the app shows in its Info window & in the main one when the latter is set to the "All Photos" view. This is the not particularly elegant script I used:

tell application "Photos"
set newKeyword to "found_it"
set medTot to count of media items
repeat with mediaIndx from 1 to medTot
tell media item mediaIndx
if keywords = missing value then
set keywords to newKeyword
else
set keywords to keywords & newKeyword
end if
delay 0.1

end tell
end repeat
end tell

It works, but there are two things about it that seem odd & I would appreciate any comments about:


• One is that even though the app's dictionary says keywords is a list of text, to add a keyword, I had to treat it as a single text object & concatenate the added keyword to it in the usual way with the & operator. Attempting to access the individual items of the list by an index reference into the list failed. I would like to know if anyone has had success with this, & if so what syntax worked.


It is perhaps worth noting that there is some internal logic at work here: if I try to add an existing keyword through a script action, it does not create a second instance of that keyword. So it is sort of a 'smart' list, I guess.


• The other thing is the delay action. Without it, the repeat loop fails on some random media item with a 'can't get missing value' error. It seems that it takes a brief amount of time for Photos to create the first keyword for an item, & without the delay the script tells the app to get the next item's keywords before it has finished writing to the last one.


I would like to know if anyone else sees the same thing (failure without the delay) & if so, is the 0.1 second delay enough to avoid it. I'm using a 2012 iMac with a 2.9 GHz core i5 CPU, a moderately fast machine, so I would like to know if slower ones would need a longer delay, & if faster ones can get by with a shorter one (or none at all).


My tests were done with a media library of about 1200 items, but without the delay the failure always occurred somewhere in the first 50 or so, so 50 to 100 items should be enough for testing purposes. Also, once the test is done, you can use the app's Keyword Manager (in the windows menu) to remove the added keyword to return the library to its previous state. Be advised that this could take some time -- longer than the script takes to run, so if you are testing with a lot of items be patient -- the progress bar that will appear in the app may appear to hang at the end but it will eventually go away by itself.


Thanks for reading this far, & in advance for any comments you are kind enough to add to my overly long ramble.

iMac (27-inch, Late 2012), OS X Yosemite (10.10.2), 2.9 GHz, 8 GB RAM

Posted on Apr 11, 2015 4:43 AM

Reply
Question marked as Best reply

Posted on Apr 13, 2015 3:42 AM

Hi


I have test AppleScript and the new App Photos for keyword operation


to add several keyword you must do that

-- create list

set listKeyWord to {}

- add keyword to list

copy "keyword1" to the end of listKeyWord

copy "keyword2" to the end of listKeyWord

--apply keyword to a picture

set keywords to listKeyWord


and you can retrieve these 2 keyword in Photos App


it's works for me


Regards

37 replies

May 6, 2015 3:19 AM in response to SDSJ

The error occurs because you have not specified what the keywords property refers to. You need to change the set keywords line to something like this:

set keywords of mypicture to keywords of mypicture & theword


That will work as long as the selected items already have at least one keyword. If not, you will get an error, "Can’t make missing value into type text" because "missing value" isn't text, so you can't concatenate text with it. Trap that out with a test for missing value & the script should do what you want for any selection.

May 6, 2015 4:06 AM in response to SDSJ

Hi,


Try this:


tell application "Photos"

set myPictures to selection

repeat with mypicture in myPictures

(choose from list {"Picture: Color", "Picture: B/W"} ¬

with prompt "Color?")

set theword to result as text

set oldKeyWords to keywords of mypicture

if theword is not in oldKeyWords then

if not (existsoldKeyWords) then set oldKeyWords to {}

copy theword to end of oldKeyWords

set keywords of mypicture to oldKeyWords

end if

#set keywords to keywords & theword <-- This won't work!

end repeat

end tell


This contains both a test for no keywords existing (in which case you need to create an empty list) and also to make sure that you don't already have that keyword attached to the photo (although I haven't tested what happens if you try to set the keywords with a duplicate keyword, I think it's best to be safe).


Also, for what it's worth, many users have had issues using selection. You might want to do this by getting the album instead... but then it seems other users have not had issues. But if you see an AppleEvent Handler -1000 error, then you will need to put these pictures into a temporary album instead and loop through a reference to the album.


Finally, I wrote a script around a few weeks ago, mainly for me just to test how different things worked... and I've not had time to do much more with it. But it will show you different ways of referencing albums. etc.


You're welcome to take a look at it here: Testing with Photos V1.0


But please NOTE: It's a NOT a script to necessarily do anything (although I do have a go at finding duplicates, updating keywords, etc.)... RATHER, it is a collection of sections that show how to perform base functions such as getting containers or lists, and I wrote lots of notes to explain my findings. You will probably want to COMMENT out several sections when running it... and you will certainly need to read the script.

May 6, 2015 5:13 AM in response to NicFletcher

NicFletcher wrote:

This contains both a test for no keywords existing (in which case you need to create an empty list) and also to make sure that you don't already have that keyword attached to the photo (although I haven't tested what happens if you try to set the keywords with a duplicate keyword, I think it's best to be safe).

Although the keywords property is a list, Photos does not object if you treat it as a text object for concatenation purposes, which is why the and operator ("&") works when adding a keyword to a keyword list. It also won't duplicate keywords, even if a script tells it to do so. Because of that, the test for duplicates is unnecessary.


Also, the if not (existsoldKeyWords) test works to detect the missing value condition, which occurs if no keyword has ever been set for the item, but won't detect an empty keywords list (a string value of ""), which occurs if all existing keywords for the item have been deleted, or even if a user clicks on the title area of a thumbnail but doesn't type anything before clicking elsewhere. That shouldn't matter in your script because you are explicitly treating the keywords list as a list (with the end of construction), but it is one of the things that can complicate 'exists' tests in many apps that behave the same way.

May 6, 2015 5:28 AM in response to R C-R

Thanks for your observations.


Good to know that one doesn't need to test for a Keyword already existing.


Also a useful comment on the if not (existsoldKeyWords) test. However, as you seem to observe, I believe for clarity's sake it is best to always treat a property in it's right class - in this case a list. And (apart from my redundancy already discussed) this script will work, and it's not necessary to check further.


But, you are right, that if I was only checking for the absence of a keyword, I would fail to trap those media items that had once had Keywords and then had them removed. So I would also need to test for an empty list as well. Thank you!

May 6, 2015 7:19 AM in response to NicFletcher

I only mentioned how Photos handles keywords lists because it is unusual. I agree that it is always best to treat objects in their proper class but one of the nice features of Applescript is automatic coercion between classes when it is unambiguous. Of course, I'm not sure that's what's happening with keywords, or even if this will continue to work in updated versions of the app.


In a few other topics about using Applescript with Photos some other oddities have been noted, so at this point I consider it a work in progress.

Anyone scripting Photos.app yet?

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