Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Open URL in New Safari Tab

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

Posted on Mar 3, 2011 2:14 PM

Reply
Question marked as Best reply

Posted on Mar 3, 2011 2:31 PM

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,


....
....
17 replies

Mar 4, 2011 9:35 AM in response to MartinLiss

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

Mar 4, 2011 9:46 PM in response to Tony T1

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).

Mar 5, 2011 6:20 AM in response to MartinLiss

<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>

Jul 24, 2011 1:21 PM in response to MartinLiss

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 makenewtabwith properties {URL:theURL}

on error


open locationtheURL

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.

Jul 24, 2011 3:38 PM in response to MartinLiss

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 makenewtabwith properties {URL:theURL}

on error

open locationtheURL

end try


end tell

Open URL in New Safari Tab

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.