NicFletcher

Q: A sample AppleScript and some technical observations on Photos

Hi,

 

While I've had my own fair share of issues with Photos, I'm overall very happy with it. But I have noticed some quirks, so I thought I'd share them and also share an AppleScript I wrote to help out on some other discussions.

 

  1. Don't add a KeyWord to more than 1,000 photos at a time as a precaution. Photos crashed when I tried a huge huge number! And it didn't recover, so good job I use Time Machine religiously.
    1. I've found KeyWord updates in chunks of around 750 to be the best optimum

  2. Talking of Time Machine, I'd noticed that it doesn't backup the database files. I was going to try and research this, but when I lost 80% of my library (see point 1 above), it restored just fine.
    1. After restoring, and starting Photos, it displays a message knowing it has been restored, and does some re-building of the database. But it was a perfect copy. So, kudos to Apple for at least not losing me my library!
    2. On the two occasions in my life I have had to restore (one with iPhoto and the recent one with Photos), I find that the restore in Time Machine takes forever. So, I heartedly recommend this tip to use terminal to restore the library. With a USB3 hard drive, I restored an 85 GB library in around 25 minutes.  Stack Exchange
  3. Smart Albums are not visible to AppleScript

  4. When changing or adding a title to an item in Photos, the change is not written to the database until you close Photos. Might be a good idea to close and reopen often if you are doing this much.

  5. I got loads of duplicates when I converted my iPhoto Library. It was a combination of a sync problem I had with PhotoStream and thousands of Zombie files that were hidden in my iPhoto package and imported into Photos during the conversion.

  6. I've been playing with AppleScript to help me tidy up my library. I wrote one script for another discussion, but I share it here, because it will give those that use AppleScript as an amateur (like myself) some clues on how to get a media item and do something with it.
    1. The documentation says that you can also process moments, but I haven't got that far yet!

 

I'll share the script and link in a separate post to make it easier to read!

MacBook Air, OS X Yosemite (10.10.3)

Posted on Apr 17, 2015 5:15 AM

Close

Q: A sample AppleScript and some technical observations on Photos

  • All replies
  • Helpful answers

Page 1 of 3 last Next
  • by NicFletcher,

    NicFletcher NicFletcher Apr 17, 2015 5:20 AM in response to NicFletcher
    Level 1 (89 points)
    Apr 17, 2015 5:20 AM in response to NicFletcher

    I've been playing with AppleScript to help me tidy up my library. I wrote one script for another discussion, but I share it here, because it will give those that use AppleScript as an amateur (like myself) some clues on how to get a media item and do something with it. The documentation says that you can also process moments, but I haven't got that far yet!

     

    There are some scripts about that use "get selection" but I have found this to be very buggy... as has the developer of Duplicate Annihilator. So, I recommend that you add the photos you want to process into an album and then use AppleScript on that album. Note: Applescript will not process SmartAlbums. You can use a SmartAlbum to get the selection of items you want... and then select all and add to a temporary album for AppleScript to use.

     

    You can download this script from dropbox here, but I've also pasted the script below:

     

    I personally don't run this script as an application or service... I prefer to open it in Script Editor and just hit run (or CMD/R). Make sure you have Photos open - I  haven't put any checks in the script to wait for the app to open and be available to AppleScript.

     

    Final warning: something odd about Photos: If you change the title of an item (either manually or with this script) it will not get written to the Photos database until you close the application. So, if you have changed any titles in preparation for running this script, make sure you close and reopen Photos. Also, if you are going to re-process any items that this script processed, you should as a precaution also close and reopen Photos. (BTW, for keywords, this is not a problem. They get written immediately.

     

    -- Photos Filename to Title V1.0

    -- Nic Fletcher - 17 April 2015

     

    -- For a given album in a given folder, check if the image / video has a title

    -- If not, make the title the same as the file name

     

    -- NOTE: Photos does NOT write titles to the database (or at least so AppleScript

    -- can see) until the application is closed. So, even after running the script

    -- where you will see the new Titles displayed, if you re-run the script it will

    -- reprocess the items. So always CLOSE and REOPEN if you have played with titles

    -- before running this script.

     

    tell application "Photos"

         

         -- Album name you want to process. You could use a temp album at the top

         -- level, and then you won't need to change the script every time

         set albName to "Test Album"

         

         -- If the albName is at the top level in Photos (i.e. not in a folder) then set this

         -- to "True" - Set it to false to allow the script to fidn the album in a folder.

         -- NOTE: This particular script will not process multiple levels in folders.

         set topLevel to false

         

         -- If the albName is in a folder (only one deep), set the name of the folder

         -- here and make sure the topLevel varaiable is set to "False"

         set albParent to "iPhoto Events"

         

         if topLevel is false then

               if exists container albName of container albParent then

                     set myContainer to container albName of container albParent

                     --display dialog "SecondLevel Variable set"

               else

                     display dialog "Folder or Album doesn't exist - Exiting" buttons ¬

                           "OK" with icon caution default button "OK"

                     return

               end if

         else

               if exists container albName then

                     set myContainer to container albName

                     --display dialog "TopLevel variable set"

               else

                     display dialog "Album doesn't exist - Exiting" buttons ¬

                           "OK" with icon caution default button "OK"

                     return

               end if

         end if

         

         set myCount to count of media items in myContainer

         display dialog "About to process " & myCount & " item(s) in the container" as text

         

         set n to 0

         repeat with img in every media item of myContainer

               set title to the name of img

               if (count of title) = 0 then -- See comment about bug at bottom

                     set the name of img to the filename of img

                     set n to n + 1

               end if

         end repeat

         display dialog n & " item(s) were processed" as text

         

    end tell

     

    -- Photos bug: If a title or keyword is deleted, Photos till thinks one exists!

    -- So, need to count the number of characters to determine if it exists.

    -- If a title or keyword never existed in Photos, then it will return not existing

  • by R C-R,

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

    Regarding the title bug, I recently discovered that for items showing it as "Untitled" in Photos, that can return one of two values to 'get title' statements in Applescript: missing value (not a text string even though it kind of looks like one) or the empty text string ("").

     

    The former is the default; the latter occurs when you 'touch' the title field below the photo to either delete whatever is there or just click on it while it is showing as "Untitled" & then click somewhere else without entering anything for the title (so it reverts to "Untitled" when you roll the pointer over it).

     

    Accordingly, a test like exists name will return false if name (the item's title in Applescript terminology) is missing value but true if it is the empty string "". So some alternate ways to test for untitled items besides counting characters might be:

     

    set title to the name of img

    if not (exists (title)) or title = "" then

      -- do whatever

    end if

     

    or

     

    set title to the name of img

    if title = missing value or title = "" then

      -- do whatever

    end if


    I have not tested which method is fastest (or if there is any difference) but at least knowing that what appears as "Untitled" in Photos can return two very different values to a script might have some use, although I can't think of what that would be.

  • by NicFletcher,

    NicFletcher NicFletcher Apr 17, 2015 6:26 AM in response to R C-R
    Level 1 (89 points)
    Apr 17, 2015 6:26 AM in response to R C-R

    Interesting!

     

    And someone else on another discussion has run straight into this problem. I stupidly (even though I knew there was a difference in the app), only tested with photos that I had manually taken away the title from.

     

    So, I will need to modify with an extra test.

     

    However, FYI, I did try with the

         title = "" then

    and I found this failed. That is why I used count of the string in my script.

  • by R C-R,

    R C-R R C-R Apr 17, 2015 6:38 AM in response to NicFletcher
    Level 6 (17,623 points)
    Apr 17, 2015 6:38 AM in response to NicFletcher

    However, FYI, I did try with the

         title = "" then

    and I found this failed. That is why I used count of the string in my script.

    As you probably know, the title = "" test has to be ORed with the missing value one to catch both possibilities.

  • by NicFletcher,

    NicFletcher NicFletcher Apr 17, 2015 7:01 AM in response to NicFletcher
    Level 1 (89 points)
    Apr 17, 2015 7:01 AM in response to NicFletcher

    Apologies... despite my mentioning the difference between a title or Keyword that has never been set or has simply been deleted by the user, I didn't test with both situations. So, now here is V1.1 of my script.

     

    (BTW, this bug is quite serious, because when using a SmartFolder to search for all photos without a keyword, it will not include photos for which I removed all keywords - and I guess the same for the titles. So, it's much more than an AppleScript issue).

     

    Sorry, but Apple doesn't allow one to edit a post after a certain time after posting, so I have to re-post it!

     

    Here is a download link from Dropbox: Photos Filename to Title V1.1

     

    And here is the modified script to test for both situations of a title:

     

    -- Photos Filename to Title V1.1

    -- Nic Fletcher - 17 April 2015

     

    -- For a given album in a given folder, check if the image / video has a title

    -- If not, make the title the same as the file name

     

    -- NOTE: Photos does NOT write titles to the database (or at least so AppleScript

    -- can see) until the application is closed. So, even after running the script

    -- where you will see the new Titles displayed, if you re-run the script it will

    -- reprocess the items. So always CLOSE and REOPEN if you have played with titles

    -- before running this script.

     

    tell application "Photos"

         

         -- Album name you want to process. You could use a temp album at the top

         -- level, and then you won't need to change the script every time

         set albName to "Test Album"

         

         -- If the albName is at the top level in Photos (i.e. not in a folder) then set this

         -- to "True" - Set it to false to allow the script to fidn the album in a folder.

         -- NOTE: This particular script will not process multiple levels in folders.

         set topLevel to true

         

         -- If the albName is in a folder (only one deep), set the name of the folder

         -- here and make sure the topLevel varaiable is set to "False"

         set albParent to "iPhoto Events"

         

         if topLevel is false then

               if exists container albName of container albParent then

                     set myContainer to container albName of container albParent

                     --display dialog "SecondLevel Variable set"

               else

                     display dialog "Folder or Album doesn't exist - Exiting" buttons ¬

                           "OK" with icon caution default button "OK"

                     return

               end if

         else

               if exists container albName then

                     set myContainer to container albName

                     --display dialog "TopLevel variable set"

               else

                     display dialog "Album doesn't exist - Exiting" buttons ¬

                           "OK" with icon caution default button "OK"

                     return

               end if

         end if

         

         set myCount to count of media items in myContainer

         display dialog "About to process " & myCount & " item(s) in the container" as text

         

         set n to 0

         repeat with img in every media item of myContainer

               set title to the name of img

               if not (exists (title)) or (count of title) = 0 then -- See comment about bug at bottom

                     set the name of img to the filename of img

                     set n to n + 1

               end if

         end repeat

         display dialog n & " item(s) were processed" as text

         

    end tell

     

    -- Photos bug: If a title or keyword is deleted, Photos till thinks one exists!

    -- So, need to count the number of characters to determine if it exists.

    -- If a title or keyword never existed in Photos, then it will return not existing

  • by R C-R,

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

    NicFletcher wrote:

    (BTW, this bug is quite serious, because when using a SmartFolder to search for all photos without a keyword, it will not include photos for which I removed all keywords - and I guess the same for the titles. So, it's much more than an AppleScript issue).

    I'm not sure what you mean here. Are you talking about a 'smart album' in Photos or a 'smart folder' in the Finder or maybe something else?

     

    If a smart album in Photos, how are you checking for photos without a keyword, given the very limited choices Photos provides for smart album criteria?

  • by NicFletcher,

    NicFletcher NicFletcher Apr 17, 2015 7:20 AM in response to R C-R
    Level 1 (89 points)
    Apr 17, 2015 7:20 AM in response to R C-R

    You're right... my bad memory.

     

    I actually discovered this with the "Description" field, where I tested in Photos app with a Smart Folder to give me all items where description is not empty... and I got results of photos where I had deleted the description.

     

    But it seems to be happening with Descriptions and Titles, and probably would with Keywords if they had that option in the Smart Folder.

  • by R C-R,

    R C-R R C-R Apr 17, 2015 8:35 AM in response to NicFletcher
    Level 6 (17,623 points)
    Apr 17, 2015 8:35 AM in response to NicFletcher

    Keywords are handled differently (& oddly) by Applescript in Photos. See my Anyone scripting Photos.app yet? topic for more about that but basically they are a list of text items instead of a single one like titles or descriptions.

  • by Steven R Jackson,

    Steven R Jackson Steven R Jackson Apr 18, 2015 3:27 PM in response to NicFletcher
    Level 1 (105 points)
    Apr 18, 2015 3:27 PM in response to NicFletcher

    Hi Nic,

     

    I'm dying to use your script, but I'm a bit worried I'll end up with lots of "Titles" that contain a file extension (.jpg, in my case). Is there any way to modify the script to ensure this doesn't happen?

     

    I also have a further question that will require a bit of explanation. I've partially copied this from another thread that didn't provide any answers...

     

    I had a messed-up Aperture Library and I didn't want to risk transferring that whole mess to the new Photos app. Consequently, over the last few weeks I manually uploaded all my photos to iCloud using the iCloud.com web interface.

     

    When I opened the new photos app for the first time, all my pictures downloaded, just as I expected.

     

    However, when I use the Get Info menu option, my photos have no descriptions. If I export the picture as an Unmodified Original and use Finder's Get Info button, I can see my old descriptions from Aperture.

     

    Would it be possible to use a modified version of your script to find the "description" I can see in Finder and apply it to the "description" field in the Photos app?

     

    Any help much appreciated!

     

    Thanks,

     

    Steve.

  • by NicFletcher,

    NicFletcher NicFletcher Apr 19, 2015 9:01 AM in response to Steven R Jackson
    Level 1 (89 points)
    Apr 19, 2015 9:01 AM in response to Steven R Jackson

    Firstly, please find here an updated version of the script that how has a few warning messages to make sure the right albums are selected, and will also NOT include the file extension.

     

    Furthermore, I've added an option (that you need to set at the top of the script) to use an "Overwrite" mode, so anyone who ran an earlier version of this script that gave the file extension as well.

     

    I've only tested with up to 50 items in an album, and I only have limited time for testing, but it should work with larger numbers, but personally, I would go up in increments of 100 before choosing a large number.

     

    You can get it from my dropbox: Photos Filename to Title V1.2.


    To anyone using this script for first time, please read the notes for my first posting at the top of this thread and the comments in the script.

     

    As to your second question:

     

    1. Firstly, I'm not / wasn't an Aperture user (although I know about messed up libraries!).
    2. But if, when using the Get Info window in Photos, there is no description, then there is no description! Because all I can see in AppleScript is what you can see in Photos.
    3. I suspect the descriptions are not included when uploading pictures to iCloud, and that is your problem.
    4. So, I think you'd be better off tidying up your Aperture / iPhoto library and then converting to Photos (all my descriptions came across from iPhoto).

     

    FYI, I'm pasting below the Photos Dictionary section for a Media Item in Photos. These are the only things AppleScript can see (and those that are marked "r/o" are read-only, which means an AppleScript can read that information for a given item, but not update or change it.

     

    Hope this helps!

     

    Nic.

  • by NicFletcher,

    NicFletcher NicFletcher Apr 19, 2015 9:24 AM in response to NicFletcher
    Level 1 (89 points)
    Apr 19, 2015 9:24 AM in response to NicFletcher

    Here is the list of properties for a Media Item - See previous post (I forgot to paste).

     

    keywords (list of text) : A list of keywords to associate with a media item

    name (text) : The name (title) of the media item.

    description (text) : A description of the media item.

    favorite (boolean) : Whether the media item has been favorited.

    date (date) : The date of the media item

    id (text, r/o) : The unique ID of the media item

    height (integer, r/o) : The height of the media item in pixels.

    width (integer, r/o) : The width of the media item in pixels.

    filename (text, r/o) : The name of the file on disk.

    altitude (real, r/o) : The GPS altitude in meters.

    location (list of real, r/o) : The GPS latitude and longitude, in an ordered list of 2 numbers. Latitude in range -90.0 to 90.0, longitude in range -180.0 to 180.0.

  • by Steven R Jackson,

    Steven R Jackson Steven R Jackson Apr 19, 2015 12:45 PM in response to NicFletcher
    Level 1 (105 points)
    Apr 19, 2015 12:45 PM in response to NicFletcher

    Thanks so much for your help. I haven't yet tried your AppleScript, but will be spending this evening doing so...

     

    As you seem to know your way around AppleScript, maybe you could point me in the right direction. As my files *do* have a description when using Get Info in the Finder, can you think of a way to trawl through the Photos library outside of Photos and maybe write this information to a text file or Numbers spreadsheet? If I could have a text file with the filename and description it would be easy (though time-consuming) to copy/paste this information into Photos...

     

    Any further help would be much appreciated.

     

    Cheers,

     

    Steve.

  • by R C-R,

    R C-R R C-R Apr 19, 2015 3:39 PM in response to NicFletcher
    Level 6 (17,623 points)
    Apr 19, 2015 3:39 PM in response to NicFletcher

    Hi Nic!

     

    Just wanted to say I liked the way you used 'the reverse of every character' construction to strip off the filename extension. I'm not exactly sure when reverse became a part of Applescript, but using it for something like this sure beats the clumsy text manipulations I used to use.

  • by R C-R,

    R C-R R C-R Apr 19, 2015 4:06 PM in response to Steven R Jackson
    Level 6 (17,623 points)
    Apr 19, 2015 4:06 PM in response to Steven R Jackson

    Steven R Jackson wrote:

    Any further help would be much appreciated.

    Just a suggestion, but you might find it helpful to ask about things like this in the Mac OS X Technologies forum. That's where some of the most knowledgeable Applescript gurus hang out, & many of them are quite willing to help you learn how to write scripts, adapt existing ones to your own needs, or in some cases to write one for you.

     

    One reason for doing this is what you are asking for involves scripting several different apps, so folks who are becoming familiar with the quirks of scripting Photos do not necessarily have the same familiarity with the others.

     

    You can right-click & copy the link to this and/or other related discussions on the main Photos for Mac page & paste that into your post to that forum. Doing that can save you some time because you won't have to reexplain everything (but it is still a good idea to include a brief summary of what you want) & it helps make the regulars in that forum more aware of what is going on in this one.

Page 1 of 3 last Next