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 Top-ranking reply

Posted on Mar 5, 2011 10:30 AM

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

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

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