I am working on a script to organize images based on their aspect ratio and for some reason it doesn’t work when launched via the Script Menu but it works fine when run via the Script Editor. It is still a work in progress but here is the code so far:
tell application "Finder"
set theFolder to (choose folder) as alias
set folder16by10 to POSIX file "/path/to/16x10/"
set folder16by9 to POSIX file "/path/to/16x9/"
set folder4by3 to POSIX file "/path/to/4x3/"
set folder5by4 to POSIX file "/path/to/5x4/"
set folderOther to POSIX file "/path/to/Other/"
set theItems to (every file in theFolder) as alias list
end tell
repeat with thisItem in theItems
tell application "Finder" to set theKind to kind of thisItem
if theKind contains "JPEG" or theKind contains "PNG" or theKind is "Portable Network Graphics image" or theKind is "Adobe Photoshop file" then
tell application "Image Events" to set thisImage to open (thisItem as alias)
tell application "Image Events" to copy dimensions of thisImage to {theWidth, theHeight}
set theRatio to ((theWidth / theHeight as number) as string)
if theRatio is "1.6" then
tell application "Finder" to move file thisItem to folder folder16by10
else if theRatio is "1.777777777778" then
tell application "Finder" to move file thisItem to folder folder16by9
else if theRatio is "1.333333333333" then
tell application "Finder" to move file thisItem to folder folder4by3
else if theRatio is "1.25" then
tell application "Finder" to move file thisItem to folder folder5by4
else
tell application "Finder" to move file thisItem to folder folderOther
end if
end if
end repeat
set theFolder to choose folder
set folder16by10 to POSIX file "/path/to/16x10/" as alias
set folder16by9 to POSIX file "/path/to/16x9/" as alias
set folder4by3 to POSIX file "/path/to/4x3/" as alias
set folder5by4 to POSIX file "/path/to/5x4/" as alias
set folderOther to POSIX file "/path/to/Other/" as alias
tell application "Finder" to set theItems to every file in theFolder
launch application "Image Events"
repeat with thisItem in theItems
tell application "Finder" to set theKind to kind of thisItem
if theKind contains "JPEG" or theKind contains "PNG" or theKind is "Portable Network Graphics image" or theKind is "Adobe Photoshop file" then
tell application "Image Events"
set thisImage to open (thisItem as alias)
copy dimensions of thisImage to {theWidth, theHeight}
close thisImage
end tell
set theRatio to theWidth / theHeight
if theRatio is 1.6 then
tell application "Finder" to move file thisItem to folder16by10
else if theRatio > 1.77 and theRatio < 1.78 then
tell application "Finder" to move file thisItem to folder16by9
else if theRatio > 1.33 and theRatio < 1.34 then
tell application "Finder" to move file thisItem to folder4by3
else if theRatio is 1.25 then
tell application "Finder" to move file thisItem to folder5by4
else
tell application "Finder" to move file thisItem to folderOther
end if
end if
end repeat
I changed a few things, especially I removed all useless coercions
to use Image Events it is recommended to launch the application explicitly first and close also every opened image.
Probably it’s more reliable to work with aliases of the folder instead of AppleScript’s file specifier, which could cause problems with the Finder’s file specifier.
Instead of
POSIX file "/path/to/16x10/" as alias
I’d recommend to use a relative path like
((path to startup disk as text) & "path:to:16x10:") as alias