Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created November 12, 2024 17:01
Show Gist options
  • Save ozgurshn/624601446e1c4a9f32728d71b51cb8c6 to your computer and use it in GitHub Desktop.
Save ozgurshn/624601446e1c4a9f32728d71b51cb8c6 to your computer and use it in GitHub Desktop.
sort landscape portrait square photos on mac
select all photos in photos and open a script editor and paste below code and run this will create 3 albums
tell application "Photos"
-- Define album names
set LandscapeAlbumName to "LandscapeAlbum"
set PortraitAlbumName to "PortraitAlbum"
set SquareAlbumName to "SquareAlbum"
activate
-- Create or get existing albums
try
if exists container LandscapeAlbumName then
set theLandscapeAlbum to container LandscapeAlbumName
else
set theLandscapeAlbum to make new album named LandscapeAlbumName
end if
if exists container PortraitAlbumName then
set thePortraitAlbum to container PortraitAlbumName
else
set thePortraitAlbum to make new album named PortraitAlbumName
end if
if exists container SquareAlbumName then
set theSquareAlbum to container SquareAlbumName
else
set theSquareAlbum to make new album named SquareAlbumName
end if
on error errText number errNum
display dialog "Cannot create/open albums: " & errNum & return & errText
return
end try
-- Get selected photos
try
set imageSel to (get selection)
if imageSel is {} then
display dialog "Please select some images."
return
end if
on error errText number errNum
display dialog "Cannot get the selection: " & errNum & return & errText
return
end try
-- Initialize lists for different orientations
set portraits to {}
set landscapes to {}
set squares to {}
-- Process each selected image
repeat with im in imageSel
try
tell im
set h to its height
set w to its width
end tell
-- Categorize based on dimensions
if w < h then
set portraits to portraits & {im}
else if w > h then
set landscapes to landscapes & {im}
else
set squares to squares & {im}
end if
on error errText number errNum
-- Try once more with a delay
try
delay 2
tell im
set h to its height
set w to its width
end tell
if w < h then
set portraits to portraits & {im}
else if w > h then
set landscapes to landscapes & {im}
else
set squares to squares & {im}
end if
on error errText2 number errNum2
display dialog "Skipping image due to error: " & errNum2 & return & errText2
-- Continue with next image
end try
end try
end repeat
-- Add photos to respective albums
try
if (count of portraits) > 0 then
add portraits to thePortraitAlbum
end if
if (count of landscapes) > 0 then
add landscapes to theLandscapeAlbum
end if
if (count of squares) > 0 then
add squares to theSquareAlbum
end if
-- Show completion message with counts
set portraitCount to count of portraits
set landscapeCount to count of landscapes
set squareCount to count of squares
display dialog "Processing complete!" & return & return & ¬
"Portrait photos: " & portraitCount & return & ¬
"Landscape photos: " & landscapeCount & return & ¬
"Square photos: " & squareCount
on error errText number errNum
display dialog "Error adding photos to albums: " & errNum & return & errText
end try
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment