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.

Script: Sort Landscape-Portrait-Square Photos, 2019 version

by: 
Last modified: Feb 17, 2019 12:43 AM
8 4674 Last modified Feb 17, 2019 12:43 AM

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:

-- copy this codeblock into Script Editor, starting here

(* How to use this script:
This script will split the selection and add the photos to three albums:  
-- LandscapeAlbum: The photos with a larger width than height in pixels
-- PortraitAlbum: The photos with a larger height than width in pixels
-- SquareAlbum: The photos with the same width as height

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



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


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

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

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

tell application "Photos"
	activate
	-- Ensure that the albums do exist
	
	try
		
		if exists container LandscapeAlbumName then
			set theLandscapeAlbum to container LandscapeAlbumName
		else
			make new album named LandscapeAlbumName
		end if
		
		if exists container PortraitAlbumName then
			set thePortraitAlbum to container PortraitAlbumName
		else
			make new album named PortraitAlbumName
		end if
		
		if exists container SquareAlbumName then
			set theSquareAlbum to container SquareAlbumName
		else
			make new album named SquareAlbumName
		end if
		
	on error errTexttwo number errNumtwo
		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)
	on error errTexttwo number errNumtwo
		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
			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
					tell im
						set h to its height
						set w to its width
					end tell
				on error errTexttwo number errNumtwo
					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
		
		add portraits to thePortraitAlbum
		add landscapes to theLandscapeAlbum
		add squares to theSquareAlbum
		return squares
	end if
	
end tell
-- copy until here


The script compiled as an application can be downloaded from this tutorial site: P01 - Applescripts from Photos’ User Tips Compiled as Applications

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