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.
Last active
August 21, 2024 14:42
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}` |
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)?
@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
@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.