Previous 1 2 Next 17 Replies Latest reply: Mar 14, 2013 4:12 PM by MartinLiss
MartinLiss Level 1 Level 1 (0 points)
I would like to have Safari navigate to a specific URL in a new tab. I'm a noobie in AppleScript but I've been able to cobble the following together.

set theURL to "https://www.someURL"

tell application "Safari"
activate
set currentTab to current tab of window 1
set URL of document 1 to theURL
end tell

It works when Safari isn't running or when a Safari window is open but otherwise I get a "Safari got an error: Can’t get document 1. Invalid index." error. How do I correct my code?

Also how to I format these posts so that my code looks like code, and

Is there a good AppleScript tutorial available somewhere?

imac, Mac OS X (10.6.6), None
  • Tony T1 Level 6 Level 6 (8,720 points)
    Try:

    set theURL to "http://www.apple.com"
    tell application "Safari" to open location theURL

    Also how to I format these posts so that my code looks like code,


    ....
    ....
  • MartinLiss Level 1 Level 1 (0 points)
    I figured the rest out:)


    set theURL to "https://www.someURL"

    tell application "Safari"
    activate
    tell application "System Events"
    tell process "Safari"
    -- System Preferences|Universal Access|'Enable access for assistive devices' must be selected for the following to work
    click menu item "New Tab" of menu "File" of menu bar 1
    end tell
    end tell
    set currentTab to current tab of window 1
    set URL of document 1 to theURL
    end tell
  • Tony T1 Level 6 Level 6 (8,720 points)
    Did you need all that code?
    I get the same results with:

    set theURL to "http://www.apple.com"
    tell application "Safari"
    open location theURL
    activate
    end tell
  • MartinLiss Level 1 Level 1 (0 points)
    Thanks but there's a problem in that your code always opens theURL in a new window. When Safari is already running that's okay even though I was trying to do it instead in a new tab, but when Safari isn't running it gives me TWO windows, my home page and a page for theURL.
  • Tony T1 Level 6 Level 6 (8,720 points)
    MartinLiss wrote:
    Thanks but there's a problem in that your code always opens theURL in a new window.


    No need for GUI scripting, try:

    set theURL to "http://www.apple.com"
    tell application "Safari" to tell window 1 to make new tab with properties {URL:theURL}
  • MartinLiss Level 1 Level 1 (0 points)
    If Safari is running when that script is executed a new tab to theURL is created (good) but focus stays with the current tab (bad). If Safari isn't running then the script as is doesn't do anything. Adding a 'tell application "Safari" to activate' line solves that problem but brings us back to the first problem (tab doesn't get focus).
  • Tony T1 Level 6 Level 6 (8,720 points)
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #E6E6EE;
    overflow: auto;">
    tell application "Safari"
    activate
    tell window 1 to make new tab
    open location "http://www.apple.com"
    end tell</pre>
  • MartinLiss Level 1 Level 1 (0 points)
    Two problems:

    1. If I already have a tab for "http://www.apple.com" then running the script creates a blank tab, and
    2. If I close Safari without Quiting, running the script results in a 'Can't get window 1. Invalid index.' error.
  • Tony T1 Level 6 Level 6 (8,720 points)
    Just needed to add "set current tab" (and delete "open location")

    set theURL to "http://www.apple.com"
    tell application "Safari"
    activate
    tell window 1 to set current tab to make new tab with properties {URL:theURL}
    end tell


    Tony
  • MartinLiss Level 1 Level 1 (0 points)
    When Safari is closed but not Quited, running that script results in the same 'Invalid Index' error.
  • Tony T1 Level 6 Level 6 (8,720 points)
    This is my last attempt


    set theURL to "http://www.apple.com"
    tell application "Safari"
    activate
    try
    tell window 1 to set current tab to make new tab with properties {URL:theURL}
    on error
    open location theURL
    end try
    end tell
  • MartinLiss Level 1 Level 1 (0 points)
    Thank works perfectly. Thank you (and the others who posted) for your help, I've learned a lot.
  • MartinLiss Level 1 Level 1 (0 points)

    Now that I've upgraded to Lion and its new version of Safari, this no longer works

     

    set theURL to "https://www.airset.com/AirSet.jsp#gi.lkCezQTmARbJ+app.Calendar_v.month+t.today"

     

    tell application "Safari"

      activate

     

      (*

    tell application "System Events"

                        tell process "Safari"

                                  -- System Preferences|Universal Access|'Enable access for assistive devices' must be selected for the following to work

                                  click menu item "New Tab" of menu "File" of menu bar 1

                        end tell

              end tell

              set currentTab to current tab of window 1

              set URL of document 1 to theURL

    *)

     

              try

                        tell window 1 to set current tab to make new tab with properties {URL:theURL}

              on error

      open location theURL

              end try

     

    end tell

     

    When Safari is already open It just gives me a new tab called 'Untitled' and the URL says 'Go to this address'. My first thought was that the Universal Access setting ahdn't been carried over and sure enough I had to set it again but it didn't help.

  • MartinLiss Level 1 Level 1 (0 points)

    I'm embarrased. I didn't notice that the tell Application... code was commented out since it wasn't needed and I only kept it there for reference, so let me re-ask my question. Under Snow Lepeord the following gives me a new tab with the correct URL while under Lion I have the problem described above.

     

    set theURL to "https://www.airset.com/AirSet.jsp#gi.lkCezQTmARbJ+app.Calendar_v.month+t.today"

     

    tell application "Safari"

      activate 

              try

                        tell window 1 to set current tab to make new tab with properties {URL:theURL}

              on error

                       open location theURL

              end try

     

    end tell


Previous 1 2 Next