I know that this topic has been discussed in a few other threads here on macscripter, but I’m still having trouble understanding why I’m experiencing the problems I am.
Basically what I want to know is what causes a script that runs successfully when run from inside of the script editor to fail when it’s run from an application’s script menu? What’s the difference between the two methods of launching the script?
Specifically, I’ve spend the last few days writing an applescript for Excel, which allows the user to select a worksheet from a list, then saves that worksheet as a PDF to a folder on a file server, and attaches the saved PDF to an email.
When run from the AppleScript Editor, it works flawlessly. When I try to run the script from the Microsoft Excel (2008) script menu, the script hangs after launching the Print Dialog window. Specifically, it hangs here, where I tell the script to click on the “Preview” button inside of the print dialog.
--TELL EXCEL TO OPEN A PDF COPY OF THE QUOTE IN PREIVEW
tell application "System Events" to tell process "Microsoft Excel"
set frontmost to true
keystroke "p" using command down
->>> click button "Preview" of window 1
Thanks for your help!
I’m assuming your code to target the button is correct since you said it works from script editor.
When you GUI script things you are scripting the user interface just like if a person were pushing and clicking things with the mouse. Therefore the way it works is dependent on how busy the computer is at the moment you are trying to gui script something. For example, in your script you are first keystroking “p” which opens the print window. When you perform that task yourself how long does the print window take to open? It doesn’t happen instantaneously does it? It takes time and if the computer is busy doing something at that moment it may take longer one time and other times it may open quicker. So it’s dependent on the computer speed and what is happening on the computer.
As such when you gui script you always have to put delays in the code to make sure the program waits until windows open or other actions are performed before the next line of code can be executed. So you should have some sort of delay between the line of where you open the print window and when you press the “preview” button.
You can use a hard delay like “delay 2” but of course 2 seconds may be too long or sometimes it may be too short. You can’t be certain. So a more exact way to delay is to check for the existence of an object before performing the task… this might work for the delay.
repeat until (exists button "Preview" of window 1)
click button "Preview" of window 1
So your problem is not that you are running the script from the script menu as opposed to running it from script editor. Your problem is that you have not coded your program properly.
I don’t believe that the problem has to do with delaying the execution of the script… Inserting a 2, 3, or 5 second delay into the script does not resolve the problem.
Again, because the script runs successfully from the AppleScript Editor application, I think it has something to do with how the script is interpreting which application is being targeted by the GUI Scripting.
I’m thinking that the targeting isn’t working because the scope of the script changed based on how it’s executed (from Applescript Editor vs from the Excel Script Menu).
Maybe the Print menu isn’t considered to be owned by Excel when targeted by a script launched from within Excel?
I’m not sure really, I’ve been attempting to target the Print dialog menu in various ways with no success.
OK, now I understand your problem. You’re running it from Excel’s script menu. I thought you meant applescript’s script menu. I don’t know anything about Excel’s menu and how they coded it. In any case, I tested your problem and I get it too using Excel’s script menu. I then tested it in applescript’s script menu and didn’t have the problem. So use that!
NOTE: I have the latest version of Excel and looked at its print window… there is no “Preview” button. I tested it clicking the “Page Setup…” button instead.
If you need help showing applescript’s script menu let me know. Also did you know you can setup the menu with “application specific” scripts meaning those scripts will only show in the menu when you have the application frontmost. With this feature it works just like using the menu you are currently using. To use this feature, create an “Applications” folder in the ~/Library/Scripts/ folder. Then inside that create a “Microsoft Excel” folder and put your script in that.
It won’t make up for all the hair I’ve torn out of my scalp, but this seems like it will be a viable workaround!