You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

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.

    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.

    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

Reply
Question marked as Top-ranking reply

Posted on Apr 17, 2015 5:20 AM

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 existscontaineralbName of containeralbParent then

set myContainer to containeralbName of containeralbParent

--display dialog "SecondLevel Variable set"

else

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

"OK" with iconcautiondefault button "OK"

return

end if

else

if existscontaineralbName then

set myContainer to containeralbName

--display dialog "TopLevel variable set"

else

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

"OK" with iconcautiondefault 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 dialogn & " 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

37 replies
Question marked as Top-ranking reply

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 existscontaineralbName of containeralbParent then

set myContainer to containeralbName of containeralbParent

--display dialog "SecondLevel Variable set"

else

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

"OK" with iconcautiondefault button "OK"

return

end if

else

if existscontaineralbName then

set myContainer to containeralbName

--display dialog "TopLevel variable set"

else

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

"OK" with iconcautiondefault 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 dialogn & " 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

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.

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.

Jun 7, 2015 5:28 PM in response to ArthurAJ

Hi ArthurAJ!


Thanks for your feedback.


(For new readers, see the sample scripts at the end of this post)


Before I answer your specific questions in your two posts, I'd like to say that in the beginning, I suffered very much with the change from iPhoto to Photos (I've never used Aperture, so I cannot comment). But, now I love Photos. But it took some time. There are some quirks, and some big omissions (e.g. not being able to add a geo location) and from the Apple Script side it is very buggy. I'm trying hard not to say that Apple know best, and certainly the first release of Photos is far from perfect, but now with a couple of months usage, I've got used to it.


In summary: If I know the date and / or the location, then I can find my photos pretty quickly. There's no real need to have titles (unless you have historic photos form the days when we used name our photo files in Finder as a way of cataloging them). I confess, that I do set up a Smart Album for the current month, and then when the new month starts I copy the contents to a regular album, so I can replicate the old Photo Stream monthly albums). But otherwise, I realise that (just Google with Gmail) when searching is so easy and powerful, wasting time with cataloging is not really that useful.


But anyway (I also - like you - seem to digress), to ANSWER your QUESTIONS:


  1. The AppleEvent timeout seems to be a big issue with the AppleScript implementation with Photos. The whole AppleScript side of photos is simply somewhat buggy in my opinion.
  2. The single biggest thing you can do to minimise its effect is to ALWAYS have the album you are processing displaying in Photos while you run any script. It makes a huge huge difference.
    I haven't tested more than 1500 photos in one album, so maybe even making sure that Photos is open and displaying the album you are processing still won't solve these bugs if the album is extra large.
  3. Your question on how to split an album: I've done this simply by selecting say, the first thousand items, and then adding the to a temporary album and then running a script against the temporary album. And then emptying the album and adding the next thousand items. And so forth.
  4. As for the timeout and adding timeouts to Apple Script, my limited testing has shown it makes little difference. This is substantiated for many other posts in forums. If making sure the album being processed is being displayed in Photos while the script is running doesn't make a difference, then you will simply need to manually create a temp album with a subset of the main album to process against (point #3 above).
  5. As to the process indicator, I have added one to the script you are using. You can download it here: Photos Filename to Title V1.3

    The process indicator in Yosemite cannot be used in a dialog box for a script, so your will see it on the status bar of Script Editor (i.e. the bottom).
  6. I also created and posted a "sample / test" script a while back to share with amateurs like myself my findings with Photos and AppleScript. For some reason I can't find that post, but you can see that script here: Testing with Photos V1.0

    This is not a specific script to do anything, but that rather a sharing of my experiences. It's a script with different sub routines to share my (trial and error) experiences with Photos and Applescript. I can't find my original post with this, but you take a look and learn from that script here: Testing with Photos V1.0

Good Luck!


Nic.

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.

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.

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 existscontaineralbName of containeralbParent then

set myContainer to containeralbName of containeralbParent

--display dialog "SecondLevel Variable set"

else

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

"OK" with iconcautiondefault button "OK"

return

end if

else

if existscontaineralbName then

set myContainer to containeralbName

--display dialog "TopLevel variable set"

else

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

"OK" with iconcautiondefault 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 dialogn & " 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

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?

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.

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.

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.

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.

A sample AppleScript and some technical observations on Photos

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