Open URL in Firefox from user input OR clipboard – Reliable, Lightning Fast, Works with any browser

First time poster, novice/intermediate KM user. I was just so excited about this one I had to share it :​)

Unfortunately Keyboard Maestro can't control Firefox in the same way as it does Chrome or Safari, because Firefox doesn't speak AppleScript.
This post shares a workaround which allows me to instantly open a particular URL in Firefox, from typed input or a clipboard.
I use it as a standalone macro, but it might work well as a subroutine too.

The most obvious and simple way to open a URL in Firefox would be a UI script for something like

  • get URL from user input or clipboard
  • activate Firefox
  • Cmd+T
  • Cmd+V
  • Return / Enter

Unfortunately, I am not a patient woman, and sometimes the "Activate Firefox" action takes a few seconds to actually happen. And then every now and then Firefox throws a tantrum and relaunches in safe mode, like it's offended at being launched by a macro. Come on! All I want is for everything to work instantly every single time – is that so much to ask??

So I came up with something that goes:

[ trigger macro + type URL + Enter = Firefox loads typed URL ], or
[ trigger macro = Firefox loads URL from clipboard ]

And it's hella quick. It goes like this:

  1. get the specified URL (encoded as "%Variable%url%") from user input
  2. check if %Variable%url% already contains "http://" or "https://"
  3. create a .url file in a specified disk location which points to the correct URL, including "https://" if needed
  4. use Firefox (or any other browser) to open the .url file
  5. delete .url file

The clipboard version works the same, except %Variable%url% is replaced with %SystemClipboard%.

What I've uploaded contains some extra actions and a "container macro", which combine to let me use the same hotkey (Alt + Space) with a single or double press to trigger the macro: single press = user input, double press = cancel macro for user input and load url from clipboard.
If you prefer distinct triggers, disable or remove everything coloured in Teal, set your own triggers and it should all still work well.

FIREFOX_GO-TO-URL.kmmacros (15.4 KB)

• • •

Extra notes:

  • Duplicating and modifying the stock Search the Web macro was the first thing I tried, but half the time Firefox would launch with only a blank new tab. infuriating! and even when it worked, it was slow.

  • you'll have to add some extra exceptions / checks if you want to use this for anything outside of http / https, eg. your local network portal

  • the action which deletes the .url file is not strictly necessary, because the file is rewritten every time you run one of the macros

  • I'm using Keyboard Maestro 11.0.3. It's December, 2024.

1 Like

One variant on that theme might be to:

  1. Specify a variable name in the menu behind the small chevron to the left of an execute shell script action, perhaps something like local_URL, and give that name a value, perhaps from the clipboard.
  2. Use the incantation open -a "Firefox" $KMVAR_local_URL

Open URL in Firefox.kmmacros (2,1 Ko)

:+1:

When a macro works, it works, and that's the main thing. If I may offer a few tips that occur to me:

  • Temporary files are not often needed in KM macros, because text can be stored in a clipboard or in variables.
  • The checks for "http://" and "https://" could be combined into one if action by using "matches" instead of "contains" and matching against the regular expression https*:\/\/ (https://regex101.com is very useful for testing and for explanations).
  • If a macro uses a variable that does not need to be shared with other macros, you might prefer to make it a local variable (e.g. local url or local_url rather than url – any name beginning with "local"). Variables are global by default, but eventually you can accumulate a load of global variables in KM that are really just clutter. Local variables (see also Instance variables) are used by a macro and then forgotten about, which is neater.

I have never found that, but perhaps it is related to what you then mentioned:

Firefox won't be able to tell whether its inputs or triggers originate from a KM macro, but that doesn't of course rule out the possibility of a macro doing something that Firefox reacts to in an unexpected way. If Firefox is relaunching (that is quitting and then launching again) without being told to, that seems strange but I expect a reason could be tracked down.

As for Firefox launching in safe mode, that will be because the key is down. This can happen if one launches Firefox using a hotkey combination that contains the option key, but there is an easy solution: under "This hot key:", change "is pressed" to "is released".

This is my implementation of the suggested idea. My macro works in Chrome and opens the current page in Firefox and Safari. It should work in all macOS except macOS 10.9 and be compatible with every Keyboard Maestro version. To get started, import it as a macro group, open any Chrome link and select "Open With..." from the Status Menu dropdown (Status Menu sits in the system menu bar).

Open With....kmmacros (5.2 KB)

If you ask why Chrome, it's because in Safari you can open the page in any browser through the Developer menu item, and in Firefox I use an extension.