4

Possible Duplicate:
Open URL in new Safari tab with AppleScript

How do I tell Safari to open a URL in a new tab of the current window?

This is a duplicate of Open URL in new Safari tab with AppleScript, however its answer no longer works with the current version of Safari (5.1 on 10.6.8). The tab is created but empty:

tell front window of application "Safari"
make new tab with properties {URL:"http://www.stackoverflow.com"}
end tell

If you make a new "document" instead the URL is honored, but this opens a window.

For what it's worth, I reported this as a bug to Apple. But a workaround would be great.

Community
  • 1
  • 1
Tobias
  • 3,882
  • 2
  • 22
  • 25

1 Answers1

6

I don't have the latest Safari here, but this might work

tell front window of application "Safari"
    set newTab to make new tab
    set the URL of newTab to "http://www.stackoverflow.com"
    set the current tab to newTab
end tell
Flambino
  • 18,507
  • 2
  • 39
  • 58
  • It does. Why didn't I think of that? – Tobias Aug 04 '11 at 14:12
  • @Tobias: Well, your original code should've worked, so why _would_ you think of this convoluted way? :) – Flambino Aug 04 '11 at 14:29
  • That won't work if the `front window` isn't a browser window. (E.g. if Safari isn't open, has no windows or some other type of window is focused.) – Lri Aug 12 '11 at 20:55
  • @Lri: It won't, no, but that's trivial to check and handle using `number of windows` and adding a try…catch or checking the front window's properties – Flambino Aug 12 '11 at 21:43
  • I posted another answer at [osx - Open URL in new Safari tab with AppleScript - Stack Overflow](http://stackoverflow.com/questions/2892622/open-url-in-new-safari-tab-with-applescript). – Lri Aug 13 '11 at 01:23