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

by: 
Last modified: Feb 17, 2019 1:17 AM
1 4595 Last modified Feb 17, 2019 1:17 AM

The user Tip has been moved to Script: Sort Panoramic-Landscape-Portrait… - Apple Community


Please Ignore this page. The code has become corrupted by the transition to a new software platform for the Apple Support Communities.



This script is an extension of the script posted here: Script: Sort Landscape-Portrait-Square PhotosIt now supports the detection of panoramic photos as well. This version has been tested with Photos 2.0 on macOS 10.12.5 Sierra.

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. The script will prompt you to enter this threshold as a real number.

When all photo have been selected in Photos, 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 a s a default

set dialogResult to display dialog ¬ "Enter the aspect ratio: " 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 heightset 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

makenewalbumnamedLandscapeAlbumName end if set theLandscapeAlbum to containerLandscapeAlbumName

if not (exists container PortraitAlbumName) then

makenewalbumnamedPortraitAlbumName end if set thePortraitAlbum to containerPortraitAlbumName

if not (exists container SquareAlbumName) then

makenewalbumnamedSquareAlbumName end if set theSquareAlbum to containerSquareAlbumName


if not (exists container MyPanoramicNameH) then

makenewalbumnamedMyPanoramicNameH end if set thePanoHAlbum to containerMyPanoramicNameH

if not (exists container MyPanoramicNameV) then

makenewalbumnamedMyPanoramicNameV end if set thePanoVAlbum to containerMyPanoramicNameV

if not (exists container MySkippedName) then

makenewalbumnamedMySkippedName end if set thePanoSkipAlbum to containerMySkippedName


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) 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 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 errTexttwonumbererrNumtwo 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


addportraitstothePortraitAlbum

addlandscapestotheLandscapeAlbum

addsquarestotheSquareAlbum

addpanosHtothePanoHAlbum

addpanosVtothePanoVAlbum

addskippedtothePanoSkipAlbum

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


-- Thanks to Old Toad for testing this script

Comments

Nov 19, 2017 3:18 AM

Here is a short version, that will only find Square format photos. use this as an example for how to search for just one aspect ratio:


-- -- copy and paste from here to the en︎d ✄ ✄

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



-- Album for media files without width and height

set MySkippedName to "SquarelbumSkipped"

-- 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 SquareAlbumName) then


makenewalbumnamedSquareAlbumName

end if

set theSquareAlbumtocontainerSquareAlbumName



if not (exists container MySkippedName) then


makenewalbumnamedMySkippedName

end if

set thePanoSkipAlbumtocontainerMySkippedName



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)

on error errTexttwonumbererrNumtwo

display dialog "Cannot get the selection: " & errNumtwo & return & errTexttwo

end try


set squares to {} -- the list of square format photos

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 errTexttwonumbererrNumtwo

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 noDimensionsthen

set skipped to {im} & skipped

else


if (w = h) then

set squares to {im} & squares

end if

end if


end repeat



addsquarestotheSquareAlbum


addskippedtothePanoSkipAlbum


return "square photos: " & (length of squares) & ", skipped: " & (length of skipped)


end if


end tell

-- end ✄ ✄

Nov 19, 2017 3:18 AM

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