This script almost works, it does what i want it to do but I also receive an error when the folder doesn’t exist. How can I avoid any of the prompts?
property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}
--Portrait
tell application "Finder"
try
set the main_folder to the folder "Hal 9000:Users:matthew:Desktop"
set Portrait_folders to (get first folder of main_folder whose name starts with "Portrait")
set Portrait_files to every file of the Portrait_folders whose ¬
file type is in the type_list or name extension is in the extension_list
repeat with i from 1 to the count of Portrait_files
set Portrait_path to (item i of Portrait_files) as string
tell application "Image Events"
set Portrait_image to open file Portrait_path
copy the dimensions of Portrait_image to {xPortraitwidth, yPortraitheight}
close Portrait_image
end tell
set PortraitRatio to 1348 / 1832
if xPortraitwidth / yPortraitheight is greater than PortraitRatio - 1.0E-3 and xPortraitwidth / yPortraitheight is less than PortraitRatio + 1.0E-3 then
my Portrait_CorrectRatio(xPortraitwidth, yPortraitheight, Portrait_path)
else
my Portrait_CheckRatio(xPortraitwidth, yPortraitheight, Portrait_path)
end if
end repeat
on error error_message
display dialog error_message buttons {"OK"} default button 1
end try
end tell
on Portrait_CorrectRatio(xPortraitwidth, yPortraitheight, Portrait_path)
tell application "Finder"
set label index of document file Portrait_path to 6
end tell
end Portrait_CorrectRatio
on Portrait_CheckRatio(xPortraitwidth, yPortraitheight, Portrait_path)
tell application "Finder"
set label index of document file Portrait_path to 5
end tell
end Portrait_CheckRatio
first of all it’s not necessary to specify the desktop folder explicitly in a Finder tell block.
If you want to process the first portrait folder of desktop
tell application "Finder"
set Portrait_folders to (get every folder of desktop whose name starts with "Portrait")
if Portrait_folders is not {} then
set aPortraitFolder to item 1 of Portrait_folders
-- do something
end if
end tell
or all filtered folders
tell application "Finder"
set Portrait_folders to (get every folder of desktop whose name starts with "Portrait")
repeat with aPortraitFolder in Portrait_folders
-- do something
end repeat
end tell
I’m intending to set this up with other folders that start with abcd… etc. should that make any difference.
Thanks for your help so far
Matt
property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}
--Portrait
tell application "Finder"
try
set Portrait_folders to (get every folder of desktop whose name starts with "Portrait")
if Portrait_folders is not {} then
set a PortraitFolder to item 1 of Portrait_folders
end if
set Portrait_files to every file of the aPortraitFolder whose ¬
file type is in the type_list or name extension is in the extension_list
repeat with i from 1 to the count of Portrait_files
set Portrait_path to (item i of Portrait_files) as string
tell application "Image Events"
set Portrait_image to open file Portrait_path
copy the dimensions of Portrait_image to {xPortraitwidth, yPortraitheight}
close Portrait_image
end tell
set PortraitRatio to 1348 / 1832
if xPortraitwidth / yPortraitheight is greater than PortraitRatio - 1.0E-3 and xPortraitwidth / yPortraitheight is less than PortraitRatio + 1.0E-3 then
my Portrait_CorrectRatio(xPortraitwidth, yPortraitheight, Portrait_path)
else
my Portrait_CheckRatio(xPortraitwidth, yPortraitheight, Portrait_path)
end if
end repeat
on error error_message
display dialog error_message buttons {"OK"} default button 1
end try
end tell
on Portrait_CorrectRatio(xPortraitwidth, yPortraitheight, Portrait_path)
tell application "Finder"
set label index of document file Portrait_path to 6
end tell
end Portrait_CorrectRatio
on Portrait_CheckRatio(xPortraitwidth, yPortraitheight, Portrait_path)
tell application "Finder"
set label index of document file Portrait_path to 5
end tell
end Portrait_CheckRatio
of course the whole code must be within the if - end if part.
I replaced also the big Finder tell block with two single lines to avoid nested application tell blocks
try
tell application "Finder" to set Portrait_folders to (get every folder of desktop whose name starts with "Portrait")
if Portrait_folders is not {} then
set aPortraitFolder to item 1 of Portrait_folders
tell application "Finder" to set Portrait_files to every file of the aPortraitFolder whose ¬
file type is in the type_list or name extension is in the extension_list
repeat with i from 1 to the count of Portrait_files
set Portrait_path to (item i of Portrait_files) as string
tell application "Image Events"
set Portrait_image to open file Portrait_path
copy the dimensions of Portrait_image to {xPortraitwidth, yPortraitheight}
close Portrait_image
end tell
set PortraitRatio to 1348 / 1832
if xPortraitwidth / yPortraitheight is greater than PortraitRatio - 1.0E-3 and xPortraitwidth / yPortraitheight is less than PortraitRatio + 1.0E-3 then
my Portrait_CorrectRatio(xPortraitwidth, yPortraitheight, Portrait_path)
else
my Portrait_CheckRatio(xPortraitwidth, yPortraitheight, Portrait_path)
end if
end repeat
end if
on error error_message
display dialog error_message buttons {"OK"} default button 1
end try