Search code examples
safariapplescript

Applescript to open Safari


(newbie here) I'd to be able to save an applescript file as an app to use to open Safari, I cobbled together this from some old posts I found, it's not exactly what I want but it worked ok, however today it stopped working giving an error "The variable theURL is not defined."

If anyone could help me out with a better script that would work on Monterey I would be really grateful. Also, when I save as an app - should I select 'Stay open after run handler' or 'Run-only'? It just needs to open Safari when it's clicked, I don't need it to really open any particular page. Thanks in advance!

    tell application "Safari"
    activate
    try
        tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
    on error
        open location theURL
    end try
end tell

Solution

  • I cobbled together this from some old posts I found:

    tell application "Safari"
        activate
        try
            tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
        on error
            open location theURL
        end try
    end tell
    

    A lot (virtually all) applescripts you see online make overuse of try blocks, which, apart from being completely unnecessary, are problematic in a number of ways, including making debugging more difficult. I'd advise taking out the line try, and everything including and between the lines on error, and end try.

    Without those lines, you'll probably get a different error message, which was being masked by the try block. It most likely that window 1 didn't exist, which would be the case if Safari had already been running and didn't have any windows open. It would have then thrown an error, forwarded the script on to the on error block, where open location theURL then, itself, threw an error because theURL hasn't been defined.

    I don't need it to really open any particular page

    Then all you need is this:

    tell application "Safari" to activate
    

    If Safari is already running without any open windows, all this does is bring Safari to the front; it doesn't open a new window for you. If you need to create a new window you can usually do this:

    tell application "Safari" 
            make new document
            activate
    end tell
    

    However, this will create a new window even if there's already a window open, which might not be what you want. To create a window but only if a window doesn't already exists, you can do this:

    tell application "Safari"
            if documents = {} then make new document
            activate
    end tell
    

    I suspect this is the script you will want to use.

    Should I select 'Stay open after run handler' or 'Run-only'?

    No and no. The first option prevents your app from quitting without explicit instruction to do so, which isn't needed in this case: you simply need your app to open, start Safari, then quit. The last option prevents you from making any edits to the script in the future, which, again, probably isn't what you want.