Applescript from the Terminal command line

Other topics

Get the current URL in Safari or Google Chrome

From the Terminal command line

Get the current URL from Safari

osascript -e 'tell app "safari" to get the url of the current tab of window 1'

Get the active URL in Google Chrome

osascript -e 'tell app "google chrome" to get the url of the active tab of window 1'

Get the Title of the current page in Safari or Google Chrome

Get the name of the web page in Safari

osascript -e 'tell app "safari" to get the name of the current tab of window 1'

Get the title of the web page in Google Chrome

osascript -e 'tell app "google chrome" to get the title of the active tab of window 1'

Use an Applescript as a shell function

You're not limited to single line Applescript code. Here we take the previous two examples and combine them into a single function.

#!/bin/bash    

pageinfo() {
  osascript -e \
  'tell app "safari" 
    tell the current tab of window 1
      return {url & "\n" & name}
    end tell
  end tell'
}

Contributors

Topic Id: 10886

Example Ids: 32604,32605,32606

This site is not affiliated with any of the contributors.