Re: UI scripting question
Re: UI scripting question
- Subject: Re: UI scripting question
- From: Bill Cheeseman <email@hidden>
- Date: Fri, 18 Jul 2008 15:22:52 -0400
- Thread-topic: UI scripting question
on 2008-07-18 1:24 PM, John Baltutis at email@hidden wrote:
> On 7/17/08, Bill Cheeseman <email@hidden> wrote:
>> on 2008-07-17 3:55 PM, John Baltutis at email@hidden wrote:
>>
>>> "System Events got an error: Can't get sheet 1 of window 1 of process
>>> \"Safari\". Invalid index."
>>
>> Try inserting 'delay 1' before the 'click' statement, to give the print
>> sheet more time to open up.
>
> Tried that with a delay 5 and the print sheet never opens.
I'm afraid I'm losing track of who is having what problems in this thread.
But I get the impression that people are having difficulty opening the Page
Setup sheet (in Safari in Mac OS X 10.4) or the Print sheet (in Safari in
10.4 or 10.5).
I no longer have 10.4 around, but my memory is that Safari on Tiger had a
Page Setup and a Print menu item in the File menu, and that clicking
either of them opened either a sheet or a dialog, I can't remember which. I
know they worked with GUI Scripting, because I did it often.
This should not be a difficult issue. The available techniques are well
understood and used all the time by many people. To make sure you've got it
right, try a simple test script, with conditions already set up manually in
Safari as you expect them to be when your final production script gets to
the point where it wants to open the sheet or dialog. For example, you have
to be looking at an actual Web site in the frontmost Safari window, because
the Page Setup or Print menu item is disabled if an empty Safari window is
frontmost. Any attempt to open the sheet with GUI Scripting will fail if the
menu item is disabled, so you have to open some Web site in the window and
eyeball the Page Setup or Print menu item to make sure it is enabled before
proceeding with this test.
Then you're ready to write a simple test script to find out how to open the
sheet.
1. One approach is to send the keyboard shortcut for the menu item, like
this (where Command-P is the shortcut for the Print menu item):
activate application "Safari"
tell application "System Events"
keystroke "p" using {command down}
end tell
Notice that the 'keystroke' command does not have to be sent to a specific
application process. System Events will send it to the frontmost
application, automatically.
Notice also that the "p" must be lowercase. If it is uppercase, GUI
Scripting will interpret it as 'keystroke "p" using {shift down, command
down}' -- which will open the Page Setup sheet in some applications but fail
in most.
Notice also that there is usually a pause before the sheet or dialog
actually opens. That's why it is often necessary to insert a 'delay 1' or
'delay 2' command after sending the 'keystroke' command to open the sheet.
2. Another approach is to click the menu item, like this:
activate application "Safari"
tell application "System Events"
tell process "Safari"
tell menu "File" of menu bar 1
click
click menu item "Print"
end tell
end tell
end tell
Notice that it is necessary to click the File menu first, then click the
Print menu item separately. The first click opens the File menu on the
screen. This is essential, because in Mac OS X, menu items don't exist until
the menu has been opened. If you simply 'click menu item "Print" of menu
"file"' in a single statement, it will fail because menu item "Print"
doesn't yet exist.
Notice also that the trailing ellipsis (three dots) in the menu item name
must be included. Usually, this is the single ellipsis character
(Option-semicolon), but in some applications it is actually three successive
dots. An easy way to know which is required is to try one and, if it fails,
try the other.
I just ran both of these scripts successfully in Safari 3.1.2 on Leopard
10.5.4.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
www.quecheesoftware.com
PreFab Software - www.prefabsoftware.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden