views:

48

answers:

1

Hi all,

I am trying this simple GUI script to open a new window of Safari:

tell application "Safari"
    activate
end tell

tell application "System Events"
    tell process "Safari"
        try
            tell menu bar 1
                tell menu bar item 3
                    click menu item 1
                end tell
            end tell
        on error theError
            display dialog ("An error occurred while performing requested action" & theError) buttons "OK" default button "OK"
        end try
    end tell
end tell

but it is giving this error message:

Expected end of line but found """

Can anyone suggest me where I may be wrong?

Thanks,

Miraaj

+1  A: 

Wow, that was weird. Your script broke AppleScript Editor. After running your script and it not working... I tried to recompile the script and then the error you posted starting showing up. So somehow your code caused AppleScript editor to break and thus the error. I had to quit and relaunch AppleScript Editor to get it working again.

I used the application UI Browser and found the problem. Your reference to the menu item was wrong. There's an extra menu in there that we can't see... and you didn't reference that extra menu. This is the problem with gui scripting. And even if a gui script works it may break at some future date as an application is updated. As such avoid gui scripting if at all possible.

Anyway, here's what your code should look like...

tell application "Safari"
    activate
end tell

tell application "System Events"
    tell process "Safari"
        try
            tell menu bar 1
                tell menu bar item 3
                    click menu item 1 of menu 1
                end tell
            end tell
        on error theError
            display dialog ("An error occurred while performing requested action " & theError) buttons "OK" default button "OK"
        end try
    end tell
end tell

EDIT: As I mentioned in my comment below, if you can't find a native command from an application's dictionary, the next most reliable method is using keyboard shortcuts. Most menu items have them. For example, if I wanted to open a new tab in a window that menu item has the keyboard shortcut command-t. So we can use that like this. Note there is a native command to open a new tab without using keystrokes, I'm just showing this as an example.

tell application "Safari" to activate
tell application "System Events"
    keystroke "t" using command down
end tell
end

Keyboard commands don't usually change between application updates whereas gui commands often do because programmers redesign their interface in updates... and when that happens gui scripting goes haywire. One of the gotcha's with both gui scripting and keystrokes is that sometimes the script goes too fast and these techniques can't keep up with the speed of the program, so they often error. When this happens you need to slow down the script using small delays to allow the interface to keep up with the script.

regulus6633
thanx your code is working as I intended :), but I am really surprised with this line in it: click menu item 1 of menu 1, I would have never imagined it!
Miraaj
I just tried to generalize this approach and try it for opening: "show top sites" menu item from menu "History". I used your script and just replaced the line: 'tell menu bar item 3' to 'tell menu bar item 6', but it started getting the error which I faced in script posted above. As a beginner GUI script appeared to be very easy to me but as I am trying to dive more into it, it has started driving me nuts! Can you suggest some suitable link for it, which can help begineer like me?
Miraaj
Just as a quick footnote, when I tried it this morning, it compiled and ran successfully, but I instead got a -1702 error, which basically means the given command isn't scriptable. Ah, Applescript...always keeping us on our toes.
Philip Regan
Miraaj, I edited my post to show another more reliable method. First we would always want to use natural applescript commands from an application's dictionary. Failing that I go with keyboard commands. GUI scripting is always last.
regulus6633
@regulus6633... thanx, you provided me very useful information :)
Miraaj