Suggestions for Copy URL From Active Browser

Sometimes I run Safari, sometimes Brave, sometimes Firefox. Yeah, I have a problem. Any at rate, what I want to create is a macro that copies the URL from the active tab of the most recently active browser and pastes it in whatever text editor I am writing in at the moment.

So if I happen to have Safari open next to a text editor and am typing, a hotkey will grab the URL from there and paste it in where I am currently typing. If it is Brave, it will do the same. The extra tricky bit is sometimes I may have both Safari and Brave running, but I want it to be smart enough to copy the URL from the most recently active browser, which is likely to be the one I have next to the text editor and am copying from.

Of course I could make separate macros for each browser then give them the same hotkey so a palette pops up prompting me to pick the one I want. But if possible I'd like to make the macro smarter than that and automatically pick the most recently active browser. Can anyone suggest any ideas to do this?

Untested, but here's the first thing that came to mind:

Then when you are in your text editor and want to figure out which was the most recent browser, you can refer to the KM variable LAST_BROWSER.

Then you'd basically just need a macro which said:

if LAST_BROWER = /Applications/ then activate Safari
if LAST_BROWER = /Applications/ then activate Brave
if LAST_BROWER = /Applications/ then activate Firefox

once the browser has activated (pause until -> front window exists),

  • send the keystroke ⌘L which will get you to the URL field
  • pause 0.5s
  • send keystroke ⌘A (select all)
  • pause 0.5
  • send keystroke ⌘C (copy)
  • pause 0.5

Then activate your text editor.

You could have the macro paste for you too, if you wanted, once your text editor is activated.

p.s. -

  1. I don't have Firefox installed, so I tested with Chrome, but you get the idea.

  2. My macro has a second action of sending a notification, which you would only want to do for debugging this initially to make sure it's working ok. Otherwise it would get annoying very quickly.

I second TJ's idea of using one macro to track the last used browser in a variable, and using that variable in a separate macro used for pasting URLs. His suggested workflow should work fine for Firefox, but for Safari and Brave, KM's built-in %FrontBrowserURL% token simplifies and speeds things up considerably, letting you do something like this:

Example Macro.kmmacros (4.1 KB)

1 Like

I knew there was something like that in Keyboard Maestro but could not find it in my too late/too sleepy trip to the forum last night.

Definitely better to use that when available.

Actually I was also thinking that window.location.href should work, but it doesn't work in Firefox either so I would just use %FrontBrowserURL%

Thank you, both of you. Your workflows works perfectly. I appreciate the help.

One problem with using %FrontBrowserURL% for Safari is it no longer copies unicode, which is a nice feature Safari (and as far as I know, only Safari) supports. That is, if I copy a url that includes unicode in it (a url with Japanese characters, for example) it will paste the ascii code for that character instead of the character itself, whereas if I use the keystrokes method, it copy/pastes the unicode version. As result I am only using %FrontBrowserURL% for Brave, and using the keystroke method for Safari.

Incidentally, Gabe, it's good to see you here. I think we haven't talked in several years. Are you still kicking around Japan?

1 Like