Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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>
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

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 http://askdifferent.com/

to open http://askdifferent.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 http://askdifferent.com/
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.