Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thedaviddias/0093e741d7e5bcc54044fbee330cfdbb to your computer and use it in GitHub Desktop.
Save thedaviddias/0093e741d7e5bcc54044fbee330cfdbb to your computer and use it in GitHub Desktop.
AppleScript and JavaScript for Automation to get frontmost tab’s url and title of various browsers.
-- AppleScript --
-- This example is meant as a simple starting point to show how to get the information in the simplest available way.
-- Keep in mind that when asking for a `return` after another, only the first one will be output.
-- This method is as good as its JXA counterpart.
-- Google Chrome
tell application "Google Chrome" to return title of active tab of front window
tell application "Google Chrome" to return URL of active tab of front window
-- Google Chrome Canary
tell application "Google Chrome Canary" to return title of active tab of front window
tell application "Google Chrome Canary" to return URL of active tab of front window
-- Chromium
tell application "Chromium" to return title of active tab of front window
tell application "Chromium" to return URL of active tab of front window
-- Opera
tell application "Opera" to return title of active tab of front window
tell application "Opera" to return URL of active tab of front window
-- Vivaldi
tell application "Vivaldi" to return title of active tab of front window
tell application "Vivaldi" to return URL of active tab of front window
-- Brave
tell application "Brave Browser" to return title of active tab of front window
tell application "Brave Browser" to return URL of active tab of front window
-- Safari
tell application "Safari" to return name of front document
tell application "Safari" to return URL of front document
-- Safari Technology Preview
tell application "Safari Technology Preview" to return name of front document
tell application "Safari Technology Preview" to return URL of front document
-- Webkit
tell application "Webkit" to return name of front document
tell application "Webkit" to return URL of front document
-- This example will return both the title and URL for the frontmost tab of the active browser, separated by a newline.
-- Keep in mind that to be able to use a variable in `tell application` — via `using terms from` — we’re basically requiring that referenced browser to be available on the system.
-- That means that to use this on "Google Chrome Canary" or "Chromium", "Google Chrome" needs to be installed. Same for other browsers.
-- This method also does not exit with a non-zero exit status when the frontmost application is not a supported browser.
-- For the aforementioned reasons, this method is inferior to its JXA counterpart.
tell application "System Events" to set frontApp to name of first process whose frontmost is true
if (frontApp = "Google Chrome") or (frontApp = "Google Chrome Canary") or (frontApp = "Chromium") or (frontApp = "Opera") or (frontApp = "Vivaldi") or (frontApp = "Brave Browser") then
using terms from application "Google Chrome"
tell application frontApp to set currentTabTitle to title of active tab of front window
tell application frontApp to set currentTabUrl to URL of active tab of front window
end using terms from
else if (frontApp = "Safari") or (frontApp = "Safari Technology Preview") or (frontApp = "Webkit") then
using terms from application "Safari"
tell application frontApp to set currentTabTitle to name of front document
tell application frontApp to set currentTabUrl to URL of front document
end using terms from
else
return "You need a supported browser as your frontmost app"
end if
return currentTabUrl & "\n" & currentTabTitle
// JavaScript for Automation (JXA) //
// This example is meant as a simple starting point to show how to get the information in the simplest available way.
// Keep in mind that when asking for a value after another, only the last one one will be output.
// This method is as good as its AppleScript counterpart.
// Google Chrome
Application('Google Chrome').windows[0].activeTab.name()
Application('Google Chrome').windows[0].activeTab.url()
// Google Chrome Canary
Application('Google Chrome Canary').windows[0].activeTab.name()
Application('Google Chrome Canary').windows[0].activeTab.url()
// Chromium
Application('Chromium').windows[0].activeTab.name()
Application('Chromium').windows[0].activeTab.url()
// Opera
Application('Opera').windows[0].activeTab.name()
Application('Opera').windows[0].activeTab.url()
// Vivaldi
Application('Vivaldi').windows[0].activeTab.name()
Application('Vivaldi').windows[0].activeTab.url()
// Brave
Application('Brave Browser').windows[0].activeTab.name()
Application('Brave Browser').windows[0].activeTab.url()
// Safari
Application('Safari').documents[0].name()
Application('Safari').documents[0].url()
// Safari Technology Preview
Application('Safari Technology Preview').documents[0].name()
Application('Safari Technology Preview').documents[0].url()
// Webkit
Application('Webkit').documents[0].name()
Application('Webkit').documents[0].url()
// This example will return both the title and URL for the frontmost tab of the active browser, separated by a newline.
// This method is superior to its AppleScript counterpart. It does not need a "main" browser available on the system to reuse the command on similar ones and throws a proper error code on failure.
const frontmost_app_name = Application('System Events').applicationProcesses.where({ frontmost: true }).name()[0]
const frontmost_app = Application(frontmost_app_name)
if (['Google Chrome', 'Google Chrome Canary', 'Chromium', 'Opera', 'Vivaldi', 'Brave Browser'].indexOf(frontmost_app_name) > -1) {
var current_tab_title = frontmost_app.windows[0].activeTab.name()
var current_tab_url = frontmost_app.windows[0].activeTab.url()
} else if (['Safari', 'Safari Technology Preview', 'Webkit'].indexOf(frontmost_app_name) > -1) {
var current_tab_title = frontmost_app.documents[0].name()
var current_tab_url = frontmost_app.documents[0].url()
} else {
throw new Error('You need a supported browser as your frontmost app')
}
current_tab_url + '\n' + current_tab_title

Firefox

Absent since although it’s possible to get the window’s title, it’s not possible to get its URL (it used to be, before version 3.6). It’s possible via hacky ways that consist of sending keystrokes, but those can be unreliable. This bug is being tracked in Bugzilla.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment