Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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" to return title of active tab of front window
tell application "Brave" 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") 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').windows[0].activeTab.name()
Application('Brave').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'].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')
}
console.log(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.

@TylerEich

This comment has been minimized.

Copy link

commented Aug 24, 2013

Opera fails for me. I'm on Opera 15 Stable. There's also no AppleScript dictionary for Opera on my system.

I haven't found any word on the entire internet about Opera dropping AppleScript support, but that's certainly what it feels like 😕

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Sep 23, 2013

I remember that when I was experimenting with Opera, AppleScript Editor didn’t show a dictionary for it, but with a few tries, just in case, it worked. Maybe they’ve dropped the support for good, I’ll see what I can find. Thank you for letting me know.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Sep 24, 2013

Removed Opera support, since it seems they no longer support AppleScript.

@shawnrice

This comment has been minimized.

Copy link

commented Mar 10, 2014

Vitor,

I'm trying to write up something that will perform an option for whichever of these are the frontmost application because you're right about making the applescripts available to work with as many browsers as possible, but I'm running into a problem.

When I'm running the script in the Applescript editor and run into "Google Chrome Canary," the editor brings up the dictionary selector and asks me to find "Google Chrome Canary," which I don't have installed. Is there a way around that? I tried wrapping it in a "try" statement, but that didn't help. As is, it's inside an "if" statement that should prevent the above commands from being invoked.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented May 17, 2014

I use if statements as well, for this. I’ve refrained from making the gist more complex with those since the goal isn’t really for this to be used as is, in AppleScript, but to be adapted and used inside your language of choice, possibly by calling osascript.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Jul 22, 2014

I’ve updated the gist with a new file that should be able to help with your question, Shawn. It has a caveat, though (explained in the comment).

@berkus

This comment has been minimized.

Copy link

commented Oct 2, 2014

Any idea about Firefox support for AppleScript?

EDIT looks like Firefox does not implement any of AppleScript scripting support at all.

@GandalfSaxe

This comment has been minimized.

Copy link

commented Oct 7, 2015

Is there any way to retrieve the tab number of the current tab in Safari? Counting from the left starting from 1.
I have firgured out how to SET the current tab, for example:
tell window 1 of application "Safari" to set current tab to tab 2

Now I just need to GET the current tab for my script.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Oct 18, 2015

@GandalfSaxe tell window 1 of application "Safari" to return index of current tab.

@txaiwieser

This comment has been minimized.

Copy link

commented Dec 3, 2015

"tell application frontApp to close current tab of front window" is not working with safari, in chrome i got it working. can you help me?

@himanshuoodles

This comment has been minimized.

Copy link

commented May 26, 2016

what would we script for firefox

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented May 28, 2016

@himanshuoodles Firefox can’t get the URL via AppleScript. Not cleanly. They have removed support for it on version 3.6.

If you have a need for it, make your voice heard. Don’t get your hopes up, though. No one seems to be capable/willing to do it.

@marbetschar

This comment has been minimized.

Copy link

commented Sep 5, 2016

FWIW: I've put your apple scripts together in a neat little NSRunningApplication extension for Swift v2.2.

Usage:

if let app = NSWorkspace.sharedWorkspace().frontmostApplication{
  print("Active Tab Title: ",app.activeTabTitle)
  print("Active Tab URL: ",app.activeTabURL)
}

And here's the code:

import Cocoa


extension NSRunningApplication{

    var activeTabURL: NSURL?{
        guard self.active, let bundleIdentifier = self.bundleIdentifier else {
            return nil
        }

        let code: String? = {
            switch(bundleIdentifier){
            case "org.chromium.Chromium":
                return "tell application \"Chromium\" to return URL of active tab of front window"
            case "com.google.Chrome.canary":
                return "tell application \"Google Chrome Canary\" to return URL of active tab of front window"
            case "com.google.Chrome":
                return "tell application \"Google Chrome\" to return URL of active tab of front window"
            case "com.apple.Safari":
                return "tell application \"Safari\" to return URL of front document"
            default:
                return nil
            }
        }()

        var errorInfo: NSDictionary?
        if let code = code, let script = NSAppleScript(source: code), let out: NSAppleEventDescriptor = script.executeAndReturnError(&errorInfo){
            if let errorInfo = errorInfo{
                print(errorInfo)

            } else if let urlString = out.stringValue{
                return NSURL(string: urlString)
            }
        }

        return nil
    }


    var activeTabTitle: String?{
        guard self.active, let bundleIdentifier = self.bundleIdentifier else {
            return nil
        }

        let code: String? = {
            switch(bundleIdentifier){
            case "org.chromium.Chromium":
                return "tell application \"Chromium\" to return title of active tab of front window"
            case "com.google.Chrome.canary":
                return "tell application \"Google Chrome Canary\" to return title of active tab of front window"
            case "com.google.Chrome":
                return "tell application \"Google Chrome\" to return title of active tab of front window"
            case "com.apple.Safari":
                return "tell application \"Safari\" to return name of front document"
            default:
                return nil
            }
        }()

        var errorInfo: NSDictionary?
        if let code = code, let script = NSAppleScript(source: code), let out: NSAppleEventDescriptor = script.executeAndReturnError(&errorInfo){
            if let errorInfo = errorInfo{
                print(errorInfo)

            } else {
                return out.stringValue
            }
        }

        return nil
    }
}
@bhavyaw

This comment has been minimized.

Copy link

commented Sep 12, 2016

@vitorgalvao

This works great.

I am just facing one minor issue i.e I am running this script on regular intervals from nodejs and when the browser ( chrome or safari ) is minimised it still keeps on reporting the title of the tab which was opened in the browser

I guess this has something to do with the line :

tell application "System Events" to set frontApp to name of first process whose frontmost is true

I.e when we minimise the process, frontApp still points to the minimised process. We need to take into account visibility factor also i guess along with the frontmost.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Dec 21, 2016

@bhavyaw Frontmost means frontmost, however the the OS interprets it. If macOS can interpret a minimised application as the frontmost app, it’s for a reason. It means the application that is active, not the application whose window is visible.

What’s a minor issue for you is actually an advantage for other people. What you’re trying to do goes counter to the OS’s interpretation, so I have no intention of adding window visibility detection.

@vitorgalvao

This comment has been minimized.

Copy link
Owner Author

commented Dec 21, 2016

Made some major changes. Most notably, added a JavaScript for Automation version which is superior to the AppleScript one and a document explaining the absence of other browsers.

@dyorgio

This comment has been minimized.

Copy link

commented Sep 23, 2017

Anybody knows how to identify "Chrome Application" that is running? like Postman.
If I use this script the result is url from Chrome Browser active tab, but the front window is Postman application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.