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 Panoramic-Landscape-Portrait-Square Photos, 2019 Version

by: 
Last modified: Feb 17, 2019 1:18 AM
4 3552 Last modified Feb 17, 2019 1:18 AM

This script is an extension of the script posted here: Script: Sort Landscape-Portrait-Square Photos 2019 version - Apple Community


It now supports the detection of panoramic photos as well. This version has been tested with Photos 1.0 to Photos 4.0 on macOS 10.14.3 Mojave.


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 six albums,

  • one album with landscape photos called "LandscapeAlbum",
  • one album with portrait photos called "PortraitAlbum",
  • one album with square photos.
  • one album for panoramas in landscape orientation named "PanoramicAlbumLandscape"
  • and one album for panoramas in portrait orientation named"PanoramicAlbumPortrait".

It will add all photos with an aspect ratio larger than a certain threshold to the panorama albums. The threshold is set by the variable "AspectRatioThreshold". The users are prompted to enter this threshold in a dialog panel.

A sixth album "PanoramicAlbumSkipped" will collect all selected items without a width or height, where the aspect ration cannot be computed.


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


-- copy and paste from here to the end ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎ ✂︎



(* How to use this script:

This script will split the selection and add the photos to six albums:  
- LandscapeAlbum: an album with photos in landscape orientation
- PortraitAlbum:  an album with photos in portrait orientation
- SquareAlbum: an album with square photos
- PanoramicAlbumLandscape: an album with panoramic photos in landscape orientation
- PanoramicAlbumPortrait: an album with panoramic photos in portrait orientation
- PanoramicAlbumSkipped: an album with media items that do not have a width or height and cannot be classified


Open this script in Script Editor. Launch Photos.
Select the Photos you want to distribute between the albums.
A photo is considered to be panoramic if the ratio of width to height exceeds a threshold. This threshold is set as the variable AspectRatioThreshold. Change this variable to the ratio you want.

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

Author: léonie
*)

set defaultAspectRatio to 25 / 10 -- change this to aspect ratio threshold you want for a photo to be counted as panoramic you want to be used as a default

set dialogResult to display dialog ¬
	"Enter the aspect ratio limit for panoramic photos: " buttons {"Cancel", "OK"} ¬
	default answer (defaultAspectRatio as text)
set AspectRatioThreshold to (text returned of dialogResult) as real


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

-- Albums for panoramic photos

set MyPanoramicNameH to "PanoramicAlbumLandscape"
-- change this to the name of the album where you want to collect the horizontally panoramic Photos


set MyPanoramicNameV to "PanoramicAlbumPortrait"
-- change this to the name of the album where you want to collect the vertically panoramic Photos

-- Album for media files without width and height
set MySkippedName to "PanoramicAlbumSkipped"
-- change this to the name of the album where you want to collect the media items that have no width or height

tell application "Photos"
	activate
	-- Ensure that the albums do exist
	
	try
		if not (exists container LandscapeAlbumName) then
			make new album named LandscapeAlbumName
		end if
		set theLandscapeAlbum to container LandscapeAlbumName
		
		if not (exists container PortraitAlbumName) then
			make new album named PortraitAlbumName
		end if
		set thePortraitAlbum to container PortraitAlbumName
		
		if not (exists container SquareAlbumName) then
			make new album named SquareAlbumName
		end if
		set theSquareAlbum to container SquareAlbumName
		
		
		if not (exists container MyPanoramicNameH) then
			make new album named MyPanoramicNameH
		end if
		set thePanoHAlbum to container MyPanoramicNameH
		
		if not (exists container MyPanoramicNameV) then
			make new album named MyPanoramicNameV
		end if
		set thePanoVAlbum to container MyPanoramicNameV
		
		if not (exists container MySkippedName) then
			make new album named MySkippedName
		end if
		set thePanoSkipAlbum to container MySkippedName
		
		
	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
	set panosH to {} -- the list of landscape panos photos
	set panosV to {} -- the list of portrait panos
	set skipped to {} -- the list of other media fiiles, not photos or videos
	
	
	--	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
			set noDimensions to (w is missing value) or (h is missing value)
			if noDimensions then
				set skipped to {im} & skipped
			else
				
				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
				if (w / h > AspectRatioThreshold) then
					set panosH to {im} & panosH
				end if
				if (h / w > AspectRatioThreshold) then
					set panosV to {im} & panosV
				end if
			end if
			
		end repeat
		
		add portraits to thePortraitAlbum
		add landscapes to theLandscapeAlbum
		add squares to theSquareAlbum
		add panosH to thePanoHAlbum
		add panosV to thePanoVAlbum
		add skipped to thePanoSkipAlbum
		
		return "landscape photos: " & (length of landscapes) & ", portrait photos: " & (length of portraits) & ", square photos: " & (length of squares) & ", panoramic landscape: " & (length of panosH) & ", panoramic portrait: " & (length of panosV) & ", skipped: " & (length of skipped)
		
	end if
	
end tell



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.