R C-R

Q: 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:46 AM

Close

Q: Anyone scripting Photos.app yet?

  • All replies
  • Helpful answers

Page 1 of 3 last Next
  • by francdesp,Helpful

    francdesp francdesp Apr 13, 2015 3:42 AM in response to R C-R
    Level 1 (5 points)
    Apr 13, 2015 3:42 AM in response to R C-R

    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

  • by R C-R,

    R C-R R C-R Apr 13, 2015 3:50 AM in response to francdesp
    Level 6 (17,623 points)
    Apr 13, 2015 3:50 AM in response to francdesp

    Thanks for that. The way Photos handles keywords is unusual. As my script demonstrates you can add a keyword by treating the keywords list as a text string (using the & operator), but as yours demonstrates that only works when adding a single keyword.

     

    Weird!

  • by ags_for_discussions,

    ags_for_discussions ags_for_discussions Apr 14, 2015 4:28 AM in response to R C-R
    Level 1 (0 points)
    Apr 14, 2015 4:28 AM in response to R C-R

    Hi,

     

    I try to make something the same (more or less). My test code for today is:

     

    tell application "Photos"

    set theContainersList to every container whose name is "XXX"

    set theItemsList to every media item of the first item of theContainersList

    repeat with theItem in theItemsList

      get keywords of theItem

      set theCurrentKeywordsList to result

      if ”YYY” is not in theCurrentKeywordsList then tell theItem to set keywords to theCurrentKeywordsList & {”YYY”}

    end repeat

    end tell

     

    It works mainly but sometimes I receive an AppleEvent error with incorrect index of the item, sometimes not. It looks that Photos doesn't keep ids of items permanently and I don't know for today how I can cope with it.

     

    If anybody has an idea I would be really appreciated.

    B

  • by R C-R,

    R C-R R C-R Apr 14, 2015 7:18 AM in response to R C-R
    Level 6 (17,623 points)
    Apr 14, 2015 7:18 AM in response to R C-R

    .

  • by R C-R,

    R C-R R C-R Apr 14, 2015 7:27 AM in response to ags_for_discussions
    Level 6 (17,623 points)
    Apr 14, 2015 7:27 AM in response to ags_for_discussions

    I think it might be that an empty keyword list returns missing value (which despite what it looks like isn't a text string) so you can't concatenate that with a text string like "YYY."

     

    That's why I had to use the if/then/else clause in my script to test for the missing value result. You might try something like that & see if you get better results.

  • by ags_for_discussions,

    ags_for_discussions ags_for_discussions Apr 14, 2015 8:07 AM in response to R C-R
    Level 1 (0 points)
    Apr 14, 2015 8:07 AM in response to R C-R

    Thanks a lot R C-R. It was one reason of mistakes. I've already made it like it was in your example. But the other type of mistakes 'invalid index of media item'. I made some experiments. The script is working when the album is opened and isn't working when album is not opened (not in front window, for instance when we launched app but haven't opened any albums yet - model of situation when we have used 'activate' or 'launch' commands). So, the conclusion which we have a right to make: Photos.app is recalculating indexes (id) of all items in time when you open an album (container) and doesn't keep indexes of items permanently.  Statement 'tell container "XXX" to activate' doesn't work unfortunately. For implementing keywords only to items in special album we should push Photo.app to open (bring on the front window) this album firstly... And it is a case which I can't solve.

     

    My aim is to add special keywords to all items in album automatically especially when I add new items. Like it works in Folder actions. It is simple to make it by hands but I'm sure that in the future i will forget to make it and my smart-album wouldn't recognize these items.

  • by Omniver,

    Omniver Omniver Apr 16, 2015 5:09 AM in response to ags_for_discussions
    Level 1 (10 points)
    Apr 16, 2015 5:09 AM in response to ags_for_discussions

    I'm hitting the same wall over and over again trying to simply get any details out of a 'media item' object.  No mater how I try and reference any of the attributes (filename, name, keywords, etc.) I continually get the error "Photos got an error: AppleEvent handler failed." number -10000

     

    Here is a very simple script to log the filenames of selected items. I would truly appreciate some assistance on what I might be doing wrong here:


    tell application "Photos"

    try

      set itemlist to selection

    on error errStr number errorNumber

      return errorstr

    end try


    set medTot to count of itemlist

    if medTot = 0 then

    display dialog "You have to select the images" buttons {"Cancel"}

    error number -128

    end if


    log "Number of selected media items is " & medTot

    repeat with mediaIndex in itemlist

    log itemlist

    tell mediaIndex

    log "The filename is " & filename

    end tell

    end repeat

    end tell

     

    The output currently is:

     

    tell application "Photos"

      get selection

      (*Number of selected media items is 3*)

      (*media item id P2nHm5%RR72aCQnj9TvNrg, media item id xGYuPCjsTz2JcUM3qLYBuw, media item id 249VfpJzTBaGRcRdluXCSw*)

      get filename of media item id "P2nHm5%RR72aCQnj9TvNrg"

    Result:

    error "Photos got an error: AppleEvent handler failed." number -10000

     

    Again, any help appreciated.  I've tried it with several variants (assigning variables instead of logging, casting to different types, trying different media type attributes) but every time I try and deference the media type attribute, I get the handler failure. 

  • by R C-R,

    R C-R R C-R Apr 16, 2015 6:10 AM in response to Omniver
    Level 6 (17,623 points)
    Apr 16, 2015 6:10 AM in response to Omniver

    I ran into similar problems trying to access media items by id inside tell statements. No idea why, but if you remove the tell mediaIndex & its matching end tell, & change the log statement that enclosed to log "The filename is " & filename of mediaIndex it should run fine.

     

    EDIT: BTW, how did you get your script so nicely formatted in your reply? I have a script that used to format other scripts for display in web pages & put that on the clipboard, but it does not work with this version of the ASC software.

  • by Omniver,

    Omniver Omniver Apr 16, 2015 6:59 AM in response to R C-R
    Level 1 (10 points)
    Apr 16, 2015 6:59 AM in response to R C-R

    Thanks R C-R, I had already tried that as well and just tried it again.  Exact same outcome.  Here is the revised script:

     

    tell application "Photos"

      try

      set itemlist to selection

      on error errStr number errorNumber

      return errorstr

      end try

     

      set medTot to count of itemlist

      if medTot = 0 then

      display dialog "You have to select the images" buttons {"Cancel"}

      error number -128

      end if

     

      log "Number of selected media items is " & medTot

      log itemlist

      repeat with mediaIndex in itemlist

      log "The filename is " & filename of mediaIndex

      end repeat

    end tell

     

    The results were:

     

    (*Number of selected media items is 3*)

    (*media item id P2nHm5%RR72aCQnj9TvNrg, media item id xGYuPCjsTz2JcUM3qLYBuw, media item id 249VfpJzTBaGRcRdluXCSw*)

    Result:

    error "Photos got an error: AppleEvent handler failed." number -10000

     

     

    Does it work for you?

     

    p.s. the nice formatting was a cut/past from applescript editor which maintained the coloring - but messed up the indentation, I then manually re-indented.  I took too much time to do first time around, thus the rather plain cut/paste in the script above.

  • by R C-R,

    R C-R R C-R Apr 16, 2015 7:34 AM in response to Omniver
    Level 6 (17,623 points)
    Apr 16, 2015 7:34 AM in response to Omniver

    It does work fine for me. Below is the Events log for a typical run:

     

    tell application "Photos"

      get selection

      (*Number of selected media items is 3*)

      (*media item id 3OiTNwpkQYCuHm%6QZ+PCQ, media item id DYMmv0sXQySn97VSkQWlFw, media item id utzUBReNQZCaTPjOZMSlIA*)

      get filename of media item id "3OiTNwpkQYCuHm%6QZ+PCQ"

      (*The filename is TaDa!.jpg*)

      get filename of media item id "DYMmv0sXQySn97VSkQWlFw"

      (*The filename is BigTex.jpg*)

      get filename of media item id "utzUBReNQZCaTPjOZMSlIA"

      (*The filename is Woof.jpg*)

    end tell


  • by Omniver,

    Omniver Omniver Apr 16, 2015 7:51 AM in response to R C-R
    Level 1 (10 points)
    Apr 16, 2015 7:51 AM in response to R C-R

    Hmm, maybe it's not an applescript error then.  Perhaps something with my library. 

  • by Omniver,

    Omniver Omniver Apr 16, 2015 7:53 AM in response to Omniver
    Level 1 (10 points)
    Apr 16, 2015 7:53 AM in response to Omniver

    Just to help debug, can you tell me the context of your selection in the Photos app?  Were you in an album, the "moments/Photos" or something else. 

     

    Thanks again

  • by R C-R,

    R C-R R C-R Apr 16, 2015 8:00 AM in response to Omniver
    Level 6 (17,623 points)
    Apr 16, 2015 8:00 AM in response to Omniver

    I was in an Album view, one with 90 photos, all jpegs.

  • by Omniver,

    Omniver Omniver Apr 16, 2015 3:12 PM in response to R C-R
    Level 1 (10 points)
    Apr 16, 2015 3:12 PM in response to R C-R

     

    OK, found something very interesting in the debugging.  When I used a different, smaller library - the code worked.  When I switched back to my main library of about ~20K photos,  it failed.  Then, all of a sudden,  it worked - but only a few times, then started failing again.  I modified the code to keep trying on the error and keep track of the failures and I got some very interesting results.  Below is a run from a selection of six images, the "errorcount" value is the number of time I received an error trying to get the filename before it finally got one.

     

    (*Number of selected media items is 6*)

    (*media item id lfb089rDQ1aQn%kESlLw5A, media item id 5JldvvOvTO2INNFHjWTQ2A, media item id WLM%GPU1QH6Wuv+2M7+2TQ, media item id EpZGePUFTWqTBgbSRqCsTQ, media item id FHV9oElTRZWxf56VC4ix6Q, media item id C4z2L0UKSH2t8Gph1AhY3A*)

    (*The filename is suz_disp800.jpg and errorcount=8*)

    (*The filename is DSC_1170.JPG and errorcount=6*)

    (*The filename is suzh.jpg and errorcount=1*)

    (*The filename is DSC_1169.JPG and errorcount=27*)

    (*The filename is DSC_1169.JPG and errorcount=22*)

    (*The filename is DSC_1170.JPG and errorcount=5*)

     

    Later, it worked consistently with my large album several times for a few minutes, then started failing again.  No change in code, no change in which images I'm selecting.

     

    There definitely seems to be some sort of race condition going on.  I would appreciate it if someone else with a large library could try the code and report back, I'd like to identify if this is a problem with my library or with larger libraries in general.


    The code I'm using is:


    tell application "Photos"

         try

               set itemlist to selection

         on error errStr number errorNumber

              return errorstr

         end try


         set medTot to count of itemlist

         if medTot = 0 then

              display dialog "You have to select the images" buttons {"Cancel"}

              error number -128

         end if


         log "Number of selected media items is " & medTot

         log itemlist


         repeat with mediaIndex in itemlist

              set errorcount to 0

              repeat

                   try  

                        log "The filename is " & filename of mediaIndex & " and errorcount=" & errorcount

                        exit repeat

                   on error number errNum

                        if (errNum is not equal to -10000) then

                             error number errNum

                        end if

                        set errorcount to errorcount + 1

                   end try

              end repeat

         end repeat

    end tell

Page 1 of 3 last Next