Hello,
This is my second time using UI scripting, and my first time relying on it so heavily. I have this script:
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:")
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
try
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
on error error_message number error_number
return error_number
end try
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false and the type identifier of the item_info is "com.adobe.pdf") then
process_item(this_item)
end if
end repeat
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false and the type identifier of the item_info is "com.adobe.pdf") then
process_item(this_item)
end if
end repeat
end process_folder
on process_item(this_item)
tell application "Adobe Acrobat Pro"
activate
open this_item
tell application "System Events"
tell process "Acrobat"
click menu item "PDF Optimizer..." of menu 1 of menu bar item "Advanced" of menu bar 1
click pop up button 1 of window "PDF Optimizer"
click menu item "MySettings" of menu 1 of pop up button 1 of window "PDF Optimizer"
click button "OK" of window "PDF Optimizer"
--run time error occurs here unless I manually click the PDF
delay 10
click button "Save" of window "Save Optimized As"
delay 2
click button "Replace" of sheet 1 of window "Save Optimized As"
end tell
end tell
close active doc saving yes
end tell
end process_item
Where it says “delay 10”, what happens is that Applescript throws an error (-43) saying it cannot find the Save Optimized As window at the same exact time as the window actually appears. Accidentally, I discovered that if I click on the Acrobat pdf itself during the “delay 10”, the Save Optimized As window shows up fine and the rest of the script executes just fine.
How can I keep the focus on the pdf so that I don’t receive the error (should I reactivate the window, use System events to “click” on the screen, or what?
Thanks in advance.