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