Get the Browser Page Title & URL for Safari, Chrome, or FireFox

Get the Browser Page Title & URL for Safari, Chrome, or FireFox

I wrote this KM macro (Ver 6) as a SUB-MACRO to be called by another macro I'm working on. It is a general purpose macro that works with all 3 of the major browsers:

  1. Safari
  2. Google Chrome
  3. FireFox

I thought FireFox was going to be a huge challenge since it basically does NOT support any type of scripting. Fortunately I discovered the Info window which can be opened and manipulated by keyboard commands.

Please post if you find any bugs, or have comments, questions, or suggestions.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EDIT #2: 2019-06-12 22:23 GMT-5

For KM Ver 8+, this macro is not much needed, with the release of the FrontBrowser tokens and Actions. I suppose the Firefox section is still relavent.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EDIT #1: Tue, Dec 29, 2015 at 12:33 PM
Note I have changed the name of the macro.
The macro was actually updated on my system on Jul 29, 2015, but I failed to update here. My apologies.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DOWNLOAD:

[SUB] BRW Get Page Title & URL from Safari, Chrome, or FF.kmmacros (19.7 KB)

Macro Image:

6 Likes

Very cool thanks for sharing. Are you using this to put into the clipboard or to send then info into another application or something?

I plan to use the KM variables set by this macro to construct Rich Text Hyperlink AND plain text Markdown link, and put both on the clipboard.

Another way to get the URL is command-L, which will highlight the URL, followed by command-C.

Enrique

1 Like

Good point. Since I have to go to the Info screen to get the Page Title, it just seemed easier to get the URL while I was there. But either way is fine.

Cool, thank you! But in KM7 the Chrome-Variables are empty?

And the result:

Google Chrome Version: Version 43.0.2357.134 (64-bit)

The Chrome actions work by AppleScript requests to Chrome, and for some reason (I suspect related to Chrome’s auto-update mechanism), the AppleScript access from Keyboard Maestro to Chrome periodically evaporates.

Relaunching Keyboard Maestro, relaunching Chrome, or restarting will likely clear it up.

Thanks! But relaunching KM, then Chrome and then rebooting my Mac has no effect…

Hey Michael,

Hmm… Works for me on Yosemite with KM7.

Open the Script Editor.app.

Open the Log View in the View Menu if it’s not already open.

Run this:

tell application "Google Chrome"
	tell active tab of front window
		set _url to its URL
		set _title to its title
	end tell
end tell

If it doesn’t work then something is very wrong somewhere.

-Chris

Hi Chris, thank you, but there seems to be an syntaxerror:

It's the german version, don't know the english error message...

Michael.

Hey Michael,

Well, that's less than thrilling.

Try this.

tell application id "com.google.Chrome"
	tell active tab of front window
		set chromeURL to its URL
		set chromeTitle to its title
	end tell
end tell

If that doesn't work I suggest you reinstall Google Chrome.

-Chris

Or experiment with .js :wink:

(function () {
  var lstWins = Application("Google Chrome").windows(),
    oWin = lstWins.length ? lstWins[0] : null,
    oTab = oWin ? oWin.activeTab() : null;

  return oTab ? {
    url: oTab.url(),
    title: oTab.title()
  } : {};
})();
1 Like

OK, now it works:

But the KM-Variables are still empty...

This works:

FWIW something which I like about .js is that the built in JSON functions (stringify() and parse()) easily store and retrieve multiple (and nested) key:value pairs in a single KM variable.

function run() {
	// Save multiple key:value pairs in a KM variable as a JSON string

	var lstWins = Application("Google Chrome").windows(),
		oTab = lstWins.length ? lstWins[0].activeTab() : null,
		oVariable = kmVar("url and title");


	// MULTIPLE AND/OR NESTED VALUES STORED IN ONE KM VARIABLE
	// ( AS A JSON STRING VERSION OF A JAVASCRIPT OBJECT )

	oVariable.value = JSON.stringify(
		oTab ? {
			url: oTab.url(),
			title: oTab.title()
		} : {}
	);

	/********************* LATER, IN ANOTHER ACTION OR SCRIPT ****************/

	// The JSON string in a KM variable can be instantly revived 
	// as as multi-value (arbitrarily nested) .js object

	var strVar = kmVar("url and title").value(), // a JSON string
		dctVar = JSON.parse(strVar); // a Javascript object

	return [

		dctVar["title"],

		dctVar["url"]

	];
}

// Get a reference to a Keyboard Maestro variable (existing or new)
function kmVar(k) {
	var kme = Application("Keyboard Maestro Engine"),
		vs = kme.variables,
		vks = vs.where({
			name: k
		});

	return vks.length ? vks[0] :
		vs.push(kme.Variable({
			'name': k
		})) && vs[k];
}
1 Like

It shouldn't be necessary to use either AppleScript or JavaScript to get the Chrome page title and URL. The KM tokens are easy to use and should work.

@peternlewis, is there some issue here with KM7?

It just occurred to me - the Chrome page has to be the frontmost app/screen when you execute this macro in order to return the Chrome data. Is this what you are doing?

Hey Michael (snapitup),

Okay. At this point I'm guessing there's an issue with Chrome and German localization.

What is the actual name of Google Chrome on your system?

In any case this script should be a bit more efficient:

------------------------------------------------------------
tell application id "com.google.Chrome"
  tell active tab of front window
    set chromeURL to its URL
    set chromeTitle to its title
  end tell
end tell
------------------------------------------------------------
tell application "Keyboard Maestro Engine"
  try
    set value of variable "chromeURL" to chromeURL
  on error
    make new variable with properties {name:"chromeURL", value:chromeURL}
  end try
  try
    set value of variable "chromeTitle" to chromeTitle
  on error
    make new variable with properties {name:"chromeTitle", value:chromeTitle}
  end try
end tell
------------------------------------------------------------

-Chris

Hey Michael (JMichaelTX),

That's why we're in the process of troubleshooting.   :smile:

-Chris

Hey Michael (snapitup),

You've figured it out already, but let's make things a little more clear for others who might read this page later.

The purpose of that script was to help troubleshoot the problem you were having not complete your task of populating Keyboard Maestro variables.

Now that we have a working AppleScript we can proceed to solve your task (see post 18 on this page).

-Chris