Get URL of opened tab / tabs from terminal

Solution 1:

To get the URL or Title of a Tab within Google Chrome via the command line, in a Terminal you can use osascript to either execute AppleScript statements or a programfile.

To see what is available in the Google Chrome AppleScript Dictionary, the dictionary can be opened and reviewed in the Script Editor using the File > Open Dictionary... command.

Here's a simple example to get the URL of the first Tab of the frontmost Google Chrome window:

osascript -e 'tell application "Google Chrome" to get URL of tab 1 of window 1'

For the Title under the same conditions:

osascript -e 'tell application "Google Chrome" to get title of tab 1 of window 1'

To get them both at the same time, under the same conditions:

osascript -e 'tell application "Google Chrome" to get {title, URL} of tab 1 of window 1'

Depending on exactly what it is you're trying to accomplish the statements can be more complex and handle multiple Tabs and or Windows, etc. Although this is where using osascript to call a programfile verses using -e statement will probably be easier.


Here's the output from Terminal when this page is the target Tab:

$ osascript -e 'tell application "Google Chrome" to get URL of tab 1 of window 1'
http://apple.stackexchange.com/questions/232565/get-url-of-opened-tab-tabs-from-terminal
$ osascript -e 'tell application "Google Chrome" to get title of tab 1 of window 1'
osx - Get URL of opened tab / tabs from terminal - Ask Different
$ osascript -e 'tell application "Google Chrome" to get {title, URL} of tab 1 of window 1'
osx - Get URL of opened tab / tabs from terminal - Ask Different, http://apple.stackexchange.com/questions/232565/get-url-of-opened-tab-tabs-from-terminal
$