4

I want to clean up my Photos library and especially move all my iPhone screenshots to a new album, that way I can easily look through them and delete what I want.

Now for this I first wanted to make a smart album in Photos, but it seems that Photos can't filter on the dimensions of an image, so I started up the AppleScript Editor :).

I created the following script, which works on a "Test Album" I created:

tell application "Photos" set source_album_name to "Test Album" set target_album_name to "Screenshots" set target_width to 640 set target_height to 1136 if not (exists container named target_album_name) then make new album named target_album_name end if set target_album to container target_album_name set source_album to container source_album_name set imageList to {} repeat with photo in media items in source_album if width of photo = target_width and height of photo = target_height then set the end of imageList to photo end if end repeat add imageList to target_album end tell 

This script loops through an Album named Test Album and compares the height and width to the dimensions of an iPhone 5S. When they match, it adds the photo to the Screenshots library. No problems there.

Now I want to run this script on my entire photo collection, so I changed the line repeat with photo in media items in source_album to repeat with photo in every media item.

This generates an error once it is past the first item (Photos got an error: Can’t get item 2 of every media item. Invalid index).

After that I changed the code to:

set all_images to every media item repeat with photo in all_images 

but after loading for a while, the script exits with code -10000, probably because of the amount of photos in that library (27.000).

Is there some way to page through a set like this?

EDIT: Changing the set line to contain a better query has the same effect, resulting in an AppleEvent handler failed with error number -10000.

set all_images to every media item whose width = target_width and height = target_height

3
  • Is it possible that every media item might include, say audio tracks, which may not have a height/width? Commented Apr 29, 2015 at 10:17
  • Well, it's a property on the media item object, I would guess the value would be 0 or at least just not included in the query. But I will try to figure it out when it crashes. Commented Apr 29, 2015 at 10:24
  • The problem is that the width and height properties are integers and can never be a missing value. (Also tried it, and it crashes right at the first record, with an integer to missing value conversion error). Commented Apr 30, 2015 at 20:01

2 Answers 2

3

The -10000 error is due to a bug in Photos 1.0. I've filed this with Apple as radar 20626449 (on OpenRadar at http://openradar.appspot.com/radar?id=6090497735000064). The error is intermittent, but the more photos there are in the library, the more likely it is to occur on any given scripting command.

There is no totally reliable way around the error, but one thing that seems to help is if the "All Photos" album is selected in Photos before starting your script. Unfortunately the Photos AppleScript suite doesn't have the ability to select an album, so you'll need to do this manually before starting the script.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I think this is indeed a bug. Been trying to work around it and it happens at random at the moment.
0

Not sure if you mind dropping into the shell in Terminal, or if you can make that work with iPhoto, but if that is ok, this may get you started on finding all files in your HOME directory and below, that are PNGs and that match your sizes...

#!/bin/bash isIphone() { # Get width using sips, don't bother getting height if not 640 wide w=$(sips -g pixelWidth "$1" | awk 'END{print $2}') [ "$w" != "640" ] && return # Get height using sips, just return if not iPhone size h=$(sips -g pixelHeight "$1" | awk 'END{print $2}') [ $h != "1136" ] && return # Output image size and name echo $w,$h, $1 } # Go through all PNG files in HOME directory listing iPhone-sized ones find ~ -iname "*.png" -type f -print0 | while read -d $'\0' -r file ; do isIphone "$file"; done 

Output

640,1136, ./iPhone/2013 09 iPhone/IMG_0269.PNG 640,1136, ./iPhone/2013 09 iPhone/IMG_0363.PNG 640,1136, ./iPhone/2013 09 iPhone/IMG_0376.PNG 

2 Comments

Not so sure this is possible, because the information of the files is stored in the library and does not have to be the same for the file on disk (the file could be iCloud synced or not).
Ok, no problems. I wasn't sure if it might help - it was just something that might have got you started if no-one comes up with anything better :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.