Can I launch the default browser without a URL?

I looked, but could not find a way to simply launch the default browser (via macro) without also providing a URL. (I reset my default browser frequently.)
I looked around for a URL that would not throw an error, and could not find that either.

Maybe an Applescript. Cadged from the web:

tell application id DefaultBrowserID()
  activate
end tell

on DefaultBrowserID()
  do shell script "export VERSIONER_PERL_PREFER_32_BIT=yes; " & ¬
    "perl -MMac::InternetConfig -le 'print +(substr((GetICHelper \"http\"), 0,4))'"
end DefaultBrowserID

c

Thanks, Chris, but I ended up doing this:
Made a text file with only a space character in it.
Saved it in my home folder as "null.txt"
and then used this for the URL:

file:///Users/tracy/null.txt

Works fine.

Something like this will work without an extant file.

open location "http://about:blank"

-ccs

1 Like

Thanks for that suggestion, but in firefox and chrome, that opens
google…

Tracy
www.valleau.gallery

[quote=“tvalleau, post:5, topic:1118”]Thanks for that suggestion, but in firefox and chrome, that opens
google…[/quote]

Hey Tracy,

You’re running that as an AppleScript?

It should open the browser that is set as the default. It should be completely browser agnostic and operate according to the system settings for default browser.

-ccs

Ah… I didn’t try as AS. I’m using the “open URL” command in KM, since that’s the only one (I can find) that operates on the default browser.

How would that look in applescript without a “tell” to a specific browswer? “Defaultbrowser” isn’t recognized. (Thing is, of course, that I’ve got it working to my satisfaction as it is now, so I’m mainly academically curious.)

I’d have to say that opening an empty text document may be more simple in most cases than constructing a specific script to open the default browser… but I’m curious.

Hey Tracy,

Exactly as written:

open location "http://about:blank"

In an Execute AppleScript Action - either text-script or script-file.

OSX knows that the address is a web address and will use the default browser to open it.

You can do the same thing in an Execute Shell Script Action like this:

open "http://about:blank"

See the man page for open in the Terminal:

man open

I think KM’s own Open A URL Action just leverages the shell’s open command.

Grrf…

about:blank is not treated kindly by all browsers.

So. Let’s go back to Chris Lott’s solution and update it a bit.

set defaultBrowserID to do shell script "export VERSIONER_PERL_PREFER_32_BIT=yes; " & ¬
  "perl -MMac::InternetConfig -le 'print (GetICHelper \"http\")'"
set defaultBrowserID to text 5 thru -1 of defaultBrowserID
set defaultBrowserID to path to application defaultBrowserID as text
tell application defaultBrowserID
  if not running then
    run
  end if
  activate
end tell

Chris’ snippet relied on creator codes, and those have been deprecated in OSX - e.g. not all apps have them anymore - so I’m using the app-name instead.

The if not running - run bit is a workaround for a bug in Mavericks and perhaps Yosemite.

This AppleScript will run just a bit faster if run from a script-file rather than a text-script.

-ccs

Thanks, Chris. I appreciate your time and courtesy in responding.

I see that my knowledge of “open” was deficient, as open location
"http://about:blank" does indeed open Firefox from within Script
Debugger.

Despite a few decades (really!) in front of a computer, there’s always
something new to learn.

Again, my thanks, and I hope this tread proves useful to others in the
future.

Best wishes.

Tracy
www.valleau.gallery

Chris, my apologies for pulling up such an old topic/post, but I need to get the name of the default browser. Your script runs great from SD6, but from Keyboard Maestro 8.0.4 (8.0.4) on macOS 10.11.6, I get this:

/var/folders/hb/6xgg0y8j4g530m81rd1f9mpc0000gn/T/Keyboard-Maestro-Script-6CC38660-D9A5-4115-8A81-B3702039E518:24:152: execution error: Can’t locate Mac/InternetConfig.pm in @INC (you may need to install the Mac::InternetConfig module) (@INC contains:

and then a long list of paths.

MY KM ENV_PERL5LIB is
/Users/myusername/perl5/lib/perl5:/usr/local/Cellar/perl/5.26.0/lib/perl5/site_perl/5.26.0

The script I ran is slightly modified:

set defaultBrowserID to do shell script "export VERSIONER_PERL_PREFER_32_BIT=yes; " & ¬
  "perl -MMac::InternetConfig -le 'print (GetICHelper \"http\")'"
set defaultBrowserID to text 5 thru -1 of defaultBrowserID
--set defaultBrowserID to path to application defaultBrowserID as text

return defaultBrowserID

since all I want is the name, not the path.

Suggestions?

Hey Jim,

Try running this:

Execute a Shell Script.kmactions (765 B)

NOTE — the explicit path to the macOS default Perl in line 2.

If that doesn’t' work then try setting PERL5LIB to an empty string.

Don't export it to the environment — just set it anywhere before line 2.

PERL5LIB=''

You might also ask Shane if this can be done easily (and more quickly) with AppleScriptObjC.

-Chris

Thanks, Chris. That worked without having to set the PER5LIB.

Hey Folks,

A little Mac consulting firm called HamSoft has created several useful Unix command-line executables.

Amongst them is DefaultApplication, which returns the default application used for a given file extension or URL-type.

$> stands for the command-line prompt.

By URL scheme:

$> DefaultApplication -url http:
/Applications/Safari.app

By file extension:

$> DefaultApplication -ext txt
/Applications/Applications_Chris/Text_Editors/BBEdit/BBEdit.app

By feeding it a path to an actual file:

$> DefaultApplication -path ~/Downloads/diode-template.zip
/System/Library/CoreServices/Applications/Archive Utility.app

This critter is very, very fast compared to the only alternative method I’m currently aware of. (The Perl script above.)

-Chris

1 Like

Thanks, Chris. It is always good to have fast options.

For my immediate purposes, compatibility with end user systems is more important than speed. So, I need to stick with the Perl solution for now. It seems fast enough. It runs in 0.08 sec on my iMac-27 Late 2015. :+1:

Even on the first run?

On my system it takes over 3 seconds to run the first time…

Subsequent runs are fast, but there’s a time-out of some kind where first-run behavior is reinitiated.

-Chris

Yep. I even created a new macro and:

  • Ran it
  • Restarted the KM Engine, and ran it

Same results:

Perl Script by @ccstone

export VERSIONER_PERL_PREFER_32_BIT=yes;
/usr/bin/perl -MMac::InternetConfig -le 'print (GetICHelper "http")' \
| sed -E 's!^.{4}!!'
1 Like