How to Launch the Default Web Browser?

I have two Macs which sync their macros. I really love this feature and it works seamlessly.

On one Mac the default broswer is Chrome, on the other (the new) Firefox.

How can I activate the respective default browser with the same (synced) macro?

Hey @halloleo,

Assuming your two Macs are a desktop and a laptop, one way that comes to mind is to test for screen size:

Activate Default Browser.kmmacros (3.1 KB)

A one line Execute Shell Script action which uses open followed by a URL will use the system’s default browser.

open https://www.duckduckgo.com
1 Like

Good tip! I knew that KM's own Open URL action worked like that:

But I did not know you could accomplish the same thing in the shell. That said, I took the OP's "activate" terminology (hopefully correctly!) to indicate that they wanted to just activate the app, without necessarily opening the same URL every time, which is why I suggested the method I did. I know I certainly wouldn't want to open the same URL every time I switched to my default browser :slightly_smiling_face:

1 Like

Various routes to finding out what the default browser is - one of them would be:

ObjC.import('AppKit')

$.NSWorkspace.sharedWorkspace
    .URLForApplicationToOpenURL($(
        $.NSURL.URLWithString('https://www.duckduckgo.com')
    ))
    .fileSystemRepresentation

3 Likes

and to concretise the mechanics of using KMVars in Execute Shell Script actions:

open some URL with default browser.kmmacros (18.2 KB)

But most Maestronically, simply:

From the Web group of actions:

Okay, scratch my last macro. I don't know a thing about JavaScript, but thanks to @ComplexPoint's easy-to-follow example, I was able to cobble this together:

Activate Default Browser 1.1.kmmacros (3.6 KB)

This seems like it should be less finicky than my screen size method, and has the significant bonus of working across different Macs with identical screen sizes. Thanks for another great tip, @ComplexPoint!

1 Like

And to activate any default browser, even when the choice is a little more exotic, unexpected or out of the ordinary:

Activate default Browser.kmmacros (18.1 KB)

ObjC.import('AppKit')

Application(
        $.NSWorkspace.sharedWorkspace
        .URLForApplicationToOpenURL($(
            $.NSURL.URLWithString(
                'https://www.duckduckgo.com'
            )
        ))
        .fileSystemRepresentation
    )
    .activate();
3 Likes

Here's another method using Perl:
###Perl Script by @ccstone

It runs very fast for me in macOS 10.11.6, but Chris reported the first run of the day was slow for him running in Sierra.

You can make your macros specific to each Mac using the MacUUID token, something like:

Keyboard Maestro Actions.kmactions (2.1 KB)

2 Likes

Thank you very much for all the answers! Very helpful!

I really like the detection via Javascript: Smooth 'n simple!

This one is good to know, too. Can be handy for cases where I really want to do different things depending on the Mac!