Script: Sort Landscape-Portrait-Square Photos

by: 
Last modified: Mar 10, 2019 1:36 AM
1 4924 Last modified Mar 10, 2019 1:36 AM

This user tip has been moved to Script: Sort Landscape-Portrait-Square Photos 2019 version - Apple Community


The code below has been damaged by the forum transition to a new platform - don't use it, only the linked version above.


------------- deprecated -----------------------------------------------------------

The constraints of smart albums in Photos do not yet support to search for photos of a certain aspect ratio. The best we can do is to add keywords and then search by keyword.

However, we can check the aspect ratio of a photo in Photos using an AppleScript. Compare, if the width of the photo is larger than the height.

I tested this with a quick-and-dirty script, that will take a set of selected photos in Photos and distributes them across three albums,one album with landscape photos called "LandscapeAlbum",one album with portrait photos called "PortraitAlbum",one album with square photos.

You could take this script as an example to create your own script:

(* How to use this script:

This script will split the selection and add the photos to three top level albums: LandscapeAlbum, PortraitAlbum, SquareAlbum. The names can be changed in the first three lines of the script. The photos will be added to the existing photos in these albums. You may want to empty the albums before running the script.

Open this script in Script Editor. Launch Photos.Select the Photos you want to distribute between the three albums:

When all all photo are selected, press the "Run" button in Script Editor.*)


setLandscapeAlbumName to "LandscapeAlbum" -- change this to the name of the album where you want to collect the Landscape Photos

setPortraitAlbumName to "PortraitAlbum" -- change this to the name of the album where you want to collect the Portrait Photos

setSquareAlbumName to "SquareAlbum" -- change this to the name of the album where you want to collect the Square Photos

tell application "Photos"

activate try

-- Ensure that the albums do exist. If they do not exist, create them. if existscontainerLandscapeAlbumNamethen set theLandscapeAlbumtocontainerLandscapeAlbumName else

makenewalbumnamedLandscapeAlbumName end if

if existscontainerPortraitAlbumNamethen set thePortraitAlbumtocontainerPortraitAlbumName else

makenewalbumnamedPortraitAlbumName end if

if existscontainerSquareAlbumNamethen set theSquareAlbumtocontainerSquareAlbumName else

makenewalbumnamedSquareAlbumName end if

on error errTexttwonumbererrNumtwo display dialog "Cannot open albums: " & errNumtwo & return & errTexttwo end try


-- process the selected photos from the All Photos album try set imageSel to (get selection) -- get a list of the selected photos on error errTexttwonumbererrNumtwo display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo end try

set portraits to {} -- the list of portrait photos set landscapes to {} -- the list of landscape photos set squares to {} -- the list of square format photos



-- check, if the album or the selected photos do contain images

if imageSel is {} then -- error "Please select some images." else repeat with im in imageSel -- check the width try tell im--get the pixel size set h to its height set w to its width end tell on error errText number errNum display dialog "Error: " & errNum & return & errText & "Trying again" try delay 2 -- try again after a delay tell im set h to its height set w to its width end tell on error errTexttwonumbererrNumtwo display dialog "Skipping image due to repeated error: " & errNumtwo & return & errTexttwo end try

end try

if (w < h) then set portraits to {im} & portraits end if if (w > h) then set landscapes to {im} & landscapes end if if (w = h) then set squares to {im} & squares end if

end repeat


addportraitstothePortraitAlbum

addlandscapestotheLandscapeAlbum

addsquarestotheSquareAlbum return squares end if

end tell

Comments

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