Skip to content

Instantly share code, notes, and snippets.

@vitorgalvao
Last active May 6, 2024 04:49
Show Gist options
  • Save vitorgalvao/5392178 to your computer and use it in GitHub Desktop.
Save vitorgalvao/5392178 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.
-- Webkit variants include "Safari", "Webkit", "Orion".
-- Specific editions are valid, including "Safari Technology Preview".
-- "Safari" Example:
tell application "Safari" to return name of front document
tell application "Safari" to return URL of front document
-- "Webkit" Example:
tell application "Webkit" to return name of front document
tell application "Webkit" to return URL of front document
-- Chromium variants include "Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge", "Arc".
-- Specific editions are valid, including "Google Chrome Canary", "Microsoft Edge Dev".
-- "Google Chrome" Example:
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
-- "Chromium" Example:
tell application "Chromium" to return title of active tab of front window
tell application "Chromium" to return URL of active tab of front window
-- This example returns both the title and URL for the frontmost tab of the active browser, separated by a newline.
-- For shorter code inclusive of all editions, only the start of the application name is checked.
-- 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 starts with "Safari") or (frontApp starts with "Webkit") or (frontApp starts with "Orion") 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 if (frontapp starts with "Google Chrome") or (frontApp starts with "Chromium") or (frontApp starts with "Opera") or (frontApp starts with "Vivaldi") or (frontApp starts with "Brave Browser") or (frontApp starts with "Microsoft Edge") or (frontApp starts with "Arc") 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
return frontApp & " is not a supported browser"
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.
// Webkit variants include "Safari", "Webkit", "Orion".
// Specific editions are valid, including "Safari Technology Preview".
// "Safari" Example:
Application("Safari").windows[0].currentTab.name()
Application("Safari").windows[0].currentTab.url()
// "Webkit" Example:
Application("Webkit").windows[0].currentTab.name()
Application("Webkit").windows[0].currentTab.url()
// Chromium variants include "Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge", "Arc".
// Specific editions are valid, including "Google Chrome Canary", "Microsoft Edge Dev".
// "Google Chrome" Example:
Application("Google Chrome").windows[0].activeTab.name()
Application("Google Chrome").windows[0].activeTab.url()
// "Chromium" Example:
Application("Chromium").windows[0].activeTab.name()
Application("Chromium").windows[0].activeTab.url()
// This example returns both the title and URL for the frontmost tab of the active browser, separated by a newline.
// For shorter code inclusive of all editions, only the start of the application name is checked.
// 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 frontAppName = Application("System Events").applicationProcesses.where({ frontmost: true })[0].name()
const frontApp = Application(frontAppName)
const webkitVariants = ["Safari", "Webkit", "Orion"]
const chromiumVariants = ["Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge", "Arc"]
if (webkitVariants.some(appName => frontAppName.startsWith(appName))) {
var currentTabTitle = frontApp.windows[0].currentTab.name()
var currentTabURL = frontApp.windows[0].currentTab.url()
} else if (chromiumVariants.some(appName => frontAppName.startsWith(appName))) {
var currentTabTitle = frontApp.windows[0].activeTab.name()
var currentTabURL = frontApp.windows[0].activeTab.url()
} else {
throw new Error(`${frontAppName} is not a supported browser: ${webkitVariants.concat(chromiumVariants).join(", ")}`)
}
`${currentTabURL}\n${currentTabTitle}`

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.

@maxhuckers
Copy link

@vitorgalvao Works a treat thank you. Is there no way to implement Firefox with this script?

@vitorgalvao
Copy link
Author

@maxhuckers
Copy link

@japanese-goblinn

If we have: tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"
in an AppleScript, do you know what the equivalent is in JXA for the same thing?

@japanese-goblinn
Copy link

@maxhuckers
well, I'm not familiar with JXA, so i don't know

@KarlPiper
Copy link

Thank you I couldn't figure out why Vivaldi wasn't responding to my scripts! "using terms from" got things going again. :)

@rathishkumar
Copy link

@vitorgalvao This works for me on my machine, I am calling this from a Python script. However, when I packaged it as a Python application and ran it on another Mac machine, it did not work. Are there any specific permissions required for this to function properly? I am new to MacOS development and would appreciate any guidance you could provide. Thank you.

@realshovanshah
Copy link

Hey, thanks for this consolidated resource.
I couldn't get Apple Script to work when I have two windows of a browser. Does anyone know if there's a way to get said data for the active window (similar to the active tab)?

@vitorgalvao
Copy link
Author

@realshovanshah That’s already how it works, and always has. Without an error, it’s impossible to help further.

Either way, as per the comments, use the JXA version, not the AppleScript version.

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