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

how can I set a url to a specific tab in AppleScript to avoid opening new tabs every time the script is run?

tell application "System Events"

activate application "Safari"

key code 19 using command down -- cmd 2 open a second tab or activate it if open

open location "http://example.com"

end tell


This works fine as long as one of these are true

1. the 2nd tab does not yet exist in which case it will make the 2nd tab and open the url in that location. Or..

2 If the 2nd tab already exists, it will activate the 2nd tab and reload the page as long as the 2nd tab is already at the website location we want it to be.


But if you have say 2nd or more tabs open and you run this, if the 2nd tab isd not already on the url you want it to be it will activate the 2nd tab and then open the website location in a new tab.


What I would like it to do is something like

key code 19 using command down

set location of current tab of window 1 in application safari to "http://example.com"


But this obviously doesn't work. So how can I tell Safari to only use the tabs I specify instead of opening new ones every time?

iMac, OS X El Capitan (10.11.3)

Posted on May 22, 2016 1:36 AM

Reply
Question marked as Best reply

Posted on May 22, 2016 12:01 PM

You might try something like this.



tell application "Safari" if not (exists document 1) then make new document tell window 1 if not (exists tab 2) then make new tab set current tab to tab 2 set tab 2's URL to "http://www.google.com" end tell end tell




Briefly tested with Safari 5.1.10 under OS X 10.6.8.


Regards,

H

2 replies
Question marked as Best reply

May 22, 2016 12:01 PM in response to Crackbot

You might try something like this.



tell application "Safari" if not (exists document 1) then make new document tell window 1 if not (exists tab 2) then make new tab set current tab to tab 2 set tab 2's URL to "http://www.google.com" end tell end tell




Briefly tested with Safari 5.1.10 under OS X 10.6.8.


Regards,

H

how can I set a url to a specific tab in AppleScript to avoid opening new tabs every time the script is run?

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