Acrobat UI script loses focus

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.

Hi

just tested under 10.4.11 acrobat pro, changed

–click button “Replace” of sheet 1 of window “Save Optimized As”
to
click button “Replace” of window 1

and your script ran fine

Budgie

What version of Acrobat are you using?

Hi!

I have Acrobat 9 and I’m using Leopard. I tried Budgie’s suggestion (thanks), but it did not solve the problem on my machine. Sorry.

In version 9 of Acrobat Pro they have also hidden away PDF Optimizer in the file Preflights. If you wish you could set up a Preflight of your own then call it through JavaScript (which has had this class added) Then you would have NO need for any GUI driven stuff.

Hi,

delays with fixed values are always unreliable using GUI scripting.
The best solution is to wait for the existence of certain UI elements


on process_item(this_item)
	tell application "Adobe Acrobat Pro"
		activate
		open this_item
	end tell
	tell application "System Events"
		tell process "Acrobat"
			click menu item "PDF Optimizer..." of menu 1 of menu bar item "Advanced" of menu bar 1
			repeat until exists window "PDF Optimizer"
				delay 0.2
			end repeat
			tell pop up button 1 of window "PDF Optimizer"
				click
				click menu item "Custom" of menu 1
			end tell
			click button "OK" of window "PDF Optimizer"
			repeat until exists window "Save Optimized As"
				delay 0.2
			end repeat
			tell window "Save Optimized As"
				click button "Save"
				repeat until exists sheet 1
					delay 0.2
				end repeat
				click button "Replace" of sheet 1
			end tell
		end tell
	end tell
	tell application "Adobe Acrobat Pro" to close active doc saving yes
end process_item

Thank you Stefan. This solves the GUI problem. Also, I think this is the only way to process folders of files within folders, which is what I was trying to accomplish.

But Mark brought up something interesting. I can’t find PDF Optimizer in the Prelight dialogs (profiles or fixups) but I found individual fixups that, taken together, would do what PDF Optimizer does. You can then create a Preflight droplet. Hmmm, I have much more investigating and sussing out. (this is why I LOVE macscripter.net)

Cheers.

Greg

I don’t have this version of Acrobat (boo) but check out Michel’s solution here:

http://publi-script.com/Forums/index.php?topic=459.0

This script is awesome, but how do I tell it to save to a different folder rather than directly over the top of the existing file?