Re: Excel2004, System Events & NSReceiverEvaluationScriptError
Re: Excel2004, System Events & NSReceiverEvaluationScriptError
- Subject: Re: Excel2004, System Events & NSReceiverEvaluationScriptError
- From: Larry McMunn <email@hidden>
- Date: Thu, 16 Dec 2004 17:56:14 -0500
Earlier today --12/16/04, Larry McMunn wrote:
>I'm moving a lot of scripts from Excel X to Excel 2004. I'm slowly getting comfortable with the changes, but there's a lot to relearn--especially without the ability to record to get a starting point. This makes debugging even more important. When I working on a script, I have problems checking it in debugging mode. I'm using Excel 2004 with the Service Pack applied and Script Debugger 3.0.8. A part of this particular project is making a PDF of a worksheet using the "Save As PDF" button in the "Print" window.
>
>The following script works exactly as expected when saved as an application and run with the source workbook open.
>
>When run from compiled mode, whether debugging is enabled or not, I get the following message --"System Events got an error: NSReceiverEvaluationScriptError: 4" upon clicking the "Save As PDF" button:
>
>-----------------------------------------------------
>tell application "Excel_2004"
> activate
> activate object worksheet "MyDesiredSheet"
> tell worksheet "MyDesiredSheet"
> set left header of page setup object to "xx"
> set center header of page setup object to "Here's My PDF"
> set right header of page setup object to "xx"
> end tell
> tell application "System Events"
> tell process "Excel_2004"
> keystroke "p" using command down
> -- problem occurs at the next line
> click button "Save As PDFä" of UI element 4 of window "Print"
> keystroke "d" using command down
> keystroke "My Desired Worksheet"
> keystroke return
> end tell
> end tell
>end tell
>-----------------------------------------------------
>
>This happens as the script appears above and also if the 'tell application "System Events"' block is put outside the tell Excel block. This is just a small portion of the complete script. I would like to be able to run the script from Script Debugger as I work on it rather than closing and running as an application to test each change.
>
>Any help out there?
>--
This looks like a timing issue. After a little trial and error, I found that simply inserting two delays let the script work:
--------------------
tell application "Excel_2004"
activate
activate object worksheet "MyDesiredSheet"
tell worksheet "MyDesiredSheet"
set left header of page setup object to "xx"
set center header of page setup object to "Here's My PDF"
set right header of page setup object to "xx"
end tell
tell application "System Events"
tell process "Excel_2004"
keystroke "p" using command down
delay 2 --<<<<<--INSERTED DELAY--shorter doesn't work, tried 1 & 1.5
click button "Save As PDF" of UI element 4 of window "Print"
delay 1.5 --<<<<<--INSERTED DELAY--shorter doesn't work, tried 1
keystroke "d" using command down
keystroke "My Desired Worksheet"
keystroke return
end tell
end tell
end tell
--------------------
I'm using a 1.5Ghz G4 Powerbook with 2 GB of ram. It seems that adding the third application (Script Debugger) in the sequence should slow the script and the window would have more time to develop. But it seems that System Events is trying to click the button before it can develop. It's puzzling, but at least I have a work around for now.
--
Happy Automation! :-)
Larry McMunn
President
McMunn Associates, Inc.
Specialists in automated Data Visualization through Apple products
Collingswood, NJ
(856) 858-3440
---------------------------------------------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden