7

I have a tab open in Google Chrome. Is it possible to get or check it's url from terminal? If not, how about automator script?

Will also be satisfied with a title.

0

1 Answer 1

13

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
$ 
1
  • Very handy. Slight variation for Safari: tell application "Safari" to get {name, URL} of tab 1 of window 1 although for some reason the URL is often "missing value". Nov 2, 2017 at 2:03

You must log in to answer this question.

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