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

Apr 16, 2015 11:45 PM in response to Omniver

Since about 6,000 of my iPhoto photos ended up without titles after conversion to Photos, I have tried using a script someone shared on how to create photo titles out of the file names. I could not get it to work on my main computer, even though it works well for the author, and curiously enough it works on a different Mac I have. The script looks like this:


on run {input, parameters}

tell application "Photos"

activate

set imageSel to (get selection)

if imageSel is {} then

error "Please select an image."

else

repeat with im in imageSel

set title to the name of im

if not (exists (title)) then

set the name of im to the filename of im

end if

end repeat

end if

end tell

return input

end run


On my main iMac, if I have not selected any photos in Photos, it returns the message to "Please select an image.", but if I select one or more, there is an error message in running AppleScript. Strangely enough, on my secondary Mac, selecting no photos returns an error message, but with photos selected, it works like a charm...


Any ideas what could be causing this?

Apr 17, 2015 1:45 AM in response to BJ4Rock

léonie's script is intended for use in an Automator "Run Applescript" action that creates an OS X Service workflow accessible from the Photos > Services menu item. For that use, it is desirable to begin by creating a new document of type Service in Automator:

User uploaded file

and in Automator to set the "Service receives" choices like this:

User uploaded file


If this is not how you created & are using her script, you may get unexpected results.


However, a few users who apparently are using it that way have reported errors occurring at the "set the name of im to the filename of im" line of the script. We are not sure why this is happening -- the script run as a service works without errors on my system so it is hard for me to see what the problem might be.


It may be helpful to run the script in Script Editor with the Replies log open to see exactly what the reported error is. I tried running it that way but Script Editor kept crashing. To avoid that, I had to comment out the following three lines in the script:

-- on run {input, parameters}

-- return input

-- end run


I don't know why that was necessary -- at worst I expected to get an error, not a crash!

Apr 19, 2015 4:38 PM in response to mbp20111

To answer (or suggest an answer) a couple of the questions in this thread:


  1. I have discovered that the "get selection" in Photos is extremely buggy. I notice also that the developers of Duplicate Annihilator are having the same problem. Furthermore, there are other discussions where one user has had zero problems when working with a selection, while other users (with the same script) have had huge problems.
    For me, with 25k media items in my library, getting the selection rarely works, returning the ""Photos got an error: AppleEvent handler failed." number -10000." In a repeat loop, sometimes it will process a few items in the selection and fail, other times it fails to even get the selection.
    But, it works perfectly using albums. So instead of using selection, I use a smart folder to get the items I want, and then add them to a temporary album and then use that album for my AppleScript... and it works fine. NOTE: AppleScript will not see Smart Albums, hence me adding items to a temporary album.
  2. To paste a script text so that it appears in this discussion as legible is a bit cumbersome, but not as cumbersome as a previous poster:
    1. Select all and copy the script from Script Editor.
    2. Paste this into a new Text Editor document.
    3. Save the Text Editor document as HTML to a temp file name (on your desktop).
    4. Open the saved file in Safari (just double click on the file)
    5. Copy the text from the Safari Windows...
    6. ... and paste into the discussion editor.
  3. I haven't yet played with KeyWords, but I do know that there seems to be a bug in Photos where a description or title that does not exist is treated differently both in Smart Folders and in AppleScript.
    1. If the media item never had a description or title, then it will appear in AppleScript as a missing value.
    2. But if it did once exist, and the user manually in Photos deleted it, then it will return an empty string in AppleScript
    3. EXCEPT... that you can't test myItem = "" as you would expect for an empty string. I only got it to work by counting the number of characters:
      ifcountofmyTitle) = 0 then ...

If anyone is interested, here is a script I have posted on some other discussions, that adds titles to items without titles from their filename (or even overwrites). You can download it form my DropBox: Photos Filename to Title V1.2

Apr 19, 2015 4:42 PM in response to NicFletcher

Regarding your #1 item, I suspect that it isn't so much a bug as that Photos does not always respond to script requests as quickly as it does to other events. Thus, it may be necessary to include delay statements in scripts, or where possible to use a try & repeat structure, to make sure Photos has time to respond to avoid the AE handler timing out & failing. Limiting the scope of the objects it references, like to an album, may accomplish much the same thing.


Regarding the #3 item, this isn't a bug, or at least if it is, it is one common to many apps. Basically, since there is no compelling reasons for apps to set an empty string value for anything unless & until there is a good reason to do so, it is up to the script writer to test for both an empty text string & no text string at all (IOW, the infamous missing value). countof works nicely for that in your script, but so do some other tests. Depending on the purpose of the script & the objects it works with, some other test might be faster or in some other way a better choice.


However, your #2 item is a great tip, & well worth the 'helpful' I gave it! 🙂 I have an old Applescript that would format one nicely for forums like this one, but the "new" ASC trashes it, so I will be using your method in the future. Well done!

Apr 20, 2015 1:54 AM in response to unlight

I stand corrected!


It is perfectly possible to get Smart Album containers.


I think I came to this conclusion early in playing with Photos, and I was failing for different reasons. But I have tested, and I can easily get the Smart Album and do anything with it that I can with any container (doh... it's a container)!


Apologies for the confusion.


BTW, I can export the photos from a Smart Album (aka any container) - I've answered your other thread / discussion.

Apr 20, 2015 2:05 AM in response to R C-R

Thanks for your comment on what I called a "bug."


I fully agree with your statement... but I called it a bug because not only can it catch out an Apple Scripter... it has caught out the developers of Photos because in a Smart Album, once I have had an item that once had a title or description that was subsequently removed, it will not return those items if I use the condition "Description is Empty" or "Title is Empty" - even though to the user they are absolutely empty. That is a bug in my opinion!

Apr 20, 2015 3:11 AM in response to unlight

unlight wrote:

I posted yesterday regarding the export function ofPhotos. Just wanted to mention that with the container statement it is possible to access smart albums. I can access them and for example log the names of the pics within, but export of the list is not possible.

I posted a simple change to your Photos and Applescript export discussion that I think will make your export script run without errors. It wasn't obvious in that topic if your "NEW PHOTOS" album was a smart one or not, so I tested on my system using both smart & regular albums as the source, & with the change your script ran fine for me.


Good to know that the container reference works & that the "whose name is" specifier is supported with it. 🙂


Inspired by your script, I played around a little with container references & discovered a couple of things that might be useful:


• You can reference an album (smart or regular) directly if you know its name with a statement like set theAlbum to container "test"


• get name of every container demonstrates that Faces & the built-in Panoramas, Videos, Time-lapse, & Bursts items that are listed as "albums" in the Photos sidebar are not containers, at least as far as Applescript is concerned, but Last Import is.

Apr 20, 2015 3:56 AM in response to R C-R

R C-R wrote:


• You can reference an album (smart or regular) directly if you know its name with a statement like set theAlbum to container "test"



But only if it is at the top level.


If you have an album in a folder, then you will also need to reference the folder:


set srcContainer to container "my album" of container "my folder"


or if you have sub folders, you'll need to continue, etc.


set srcContainer to container "my album" of container "my subfolder" of container "my main folder"

Apr 20, 2015 4:05 AM in response to NicFletcher

As far as I know, there is no support for a "Description is Empty" or "Title is Empty" test in the Photos Applescript dictionary, or for that matter an "empty" property of any 'noun' object in the dictionary of any app I am familiar with. If you were to try to compile an script with either of these forms inside a tell application "Photos" construction, "empty" would be interpreted as a variable, which you would have to set to something to use it as a test.


That's one of the things you have to be careful about when writing Applescripts -- you cannot assume that anything in the user interface of an app is directly comparable to anything in its dictionary. Even though Applescript uses an English-like language, & smart enough to do certain unambiguous class coercions that are not explicitly stated in a script, it is still just an interface to an abstract, object oriented model of the app that exposes script writers to things that the UI hides.


One of those things is text strings. Consider for instance that in the UI users can't easily tell if a text item is truly empty, unassigned, or consists only of white space (spaces, tabs, etc.). In the UI, that distinction may not be significant; at the scripting level it always is (which is what your 'count of' test addresses).

Apr 20, 2015 4:12 AM in response to NicFletcher

NicFletcher wrote:

R C-R wrote:

• You can reference an album (smart or regular) directly if you know its name with a statement like set theAlbum to container "test"

But only if it is at the top level.

Of course. You always have to take into account the object hierarchy in any app's Applescript dictionary.

Apr 20, 2015 4:22 AM in response to R C-R

Sorry if I wasn't clear. I meant while using the Photos app and creating a Smart Album through the user interface.

There is, in my opinion, a serious bug with Title & Description when searching for "is empty" or "is not empty" because it doesn't return the correct results.


But I fully agree with everything you write about Applescript. Indeed we could overcome the above "bug" in the app.

May 6, 2015 12:17 AM in response to R C-R

I found this post as the only help for Photos scripting, I used to use a bunch of apple scripts to label photos quickly. It of course no longer works in Photos. I was trying to update it with the help of this set of posts but I only hit errors like "Photos got an error: Can’t get keywords."

I was wondering if anyone could help me get this working. This is an amalgamation of my original script and what I learned here. Any help or direction would be greatly appreciated.

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 keywords to keywords & theword

end repeat

end tell

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.