3

I know that I can use open command to open a URL in a default browser like:

$ open http://google.com

But what I would like is to execute a similar or same command to open different URLs one after the other, but reusing the same tab of the browser.

Something similar to the HTML version of what I'm trying to achieve:

<a href="http://google.com" target="my_target_window">Same tab</a>
6

You can do this with some AppleScript. For Google Chrome it looks like:

on run argv
  tell application "Google Chrome"
    set URL of active tab of window 1 to item 1 of argv
    activate
  end tell
end run

You'd save that as a script file (.scpt) on disk and then call it with osascript from the command line. For example, if you saved that as ~/bin/chrome.scpt you'd do:

osascript ~/bin/chrome.scpt https://apple.stackexchange.com/

to open https://apple.stackexchange.com/ in the first Chrome window's currently active tab. If you wanted to make it a little simpler you could prefix the script with #! so it can be executed directly from your shell like so:

#!/usr/bin/env osascript 
on run argv
  tell application "Google Chrome"
    set URL of active tab of window 1 to item 1 of argv
    activate
  end tell
end run

And then save that as ~/bin/chrome and chmod u+x ~/bin/chrome and call it like so:

~/bin/chrome https://apple.stackexchange.com/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .