Tuesday, September 17, 2019

#1 2008-11-15 02:08:16 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Get user default browser

How can I get user default browser (the one set on Safari general preferences) as text?
I need something like:

set default browser as myBrowser
tell application myBrowser ...


I suppose is a value on some .plist file
Thanks in advance.

P.S. "open location" don't solve my need.


Filed under: defaults, browser, plist

Offline

 

#2 2008-11-15 03:20:49 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11595
Website

Re: Get user default browser

Buongiorno,

I have a handler for this purpose, it depends on the OS version.
The handler returns the bundle identifier of the default browser


Applescript:


on default_Browser()
   tell (system attribute "sysv") to set MacOS_version to it mod 4096 div 16
   if MacOS_version is 5 then
       set {a1, a2} to {1, 2}
   else
       set {a1, a2} to {2, 1}
   end if
   set pListpath to (path to preferences as Unicode text) & "com.apple.LaunchServices.plist"
   tell application "System Events"
       repeat with i in property list items of property list item 1 of contents of property list file pListpath
           if value of property list item a2 of i is "http" then
               return value of property list item a1 of i
           end if
       end repeat
       return "com.apple.Safari"
   end tell
end default_Browser


regards

Stefan


Filed under: System

Offline

 

#3 2008-11-15 03:20:58 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Re: Get user default browser

Hi crgand,

Here on my Mac (10.5.5) this information is stored in the «com.apple.LaunchServices» plist file. But retrieving this information can be a tedious task, so I wrote myself a little Obj-C tool that displays the default browser on the command line. The tool uses the Launch Services of Mac OS X and the (most simple) source can be studied here. If you want to use it in your own scripts, then download the utility, unzip it and call/use it like shown below:

Applescript:


set toolpath to ((path to desktop) as Unicode text) & "defbrowser"
-- set toolpath to ((path to me) as Unicode text) & "Contents:Resources:defbrowser"
set output to do shell script (quoted form of POSIX path of toolpath)
tell application id output
   activate
end tell


Plonum - Convert images to iWork Numbers documents

Offline

 

#4 2008-11-15 08:59:01 am

Bruce Phillips
Administrator
Registered: 2004-07-16
Posts: 2649

Re: Get user default browser

Martin Michel wrote:

I wrote myself a little Obj-C tool that displays the default browser on the command line.


Some people don't open .html files in browsers… (I open them in TextMate myself.)

Offline

 

#5 2008-11-15 09:22:11 am

Martin Michel
Administrator
From:: Berlin, Germany
Registered: 2008-03-03
Posts: 701
Website

Re: Get user default browser

Bruce Phillips wrote:
Martin Michel wrote:

I wrote myself a little Obj-C tool that displays the default browser on the command line.


Some people don't open .html files in browsers… (I open them in TextMate myself.)


Thanks for pointing my attention to this problem, Bruce! I modified the utility, it now returns the default handler/app bundle for the «http» URL scheme.


Plonum - Convert images to iWork Numbers documents

Offline

 

#6 2008-11-15 09:44:05 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5003

Re: Get user default browser

crgand wrote:

How can I get user default browser (the one set on Safari general preferences) as text?
I need something like:

set default browser as myBrowser
tell application myBrowser ...


Don't forget that even when you've identified the default browser, there's no guarantee that it'll be able to understand the code in your 'tell' block. In fact, it's unlikely that you'll be able to compile the code anyway with a variable in the 'tell' line.


NG

Offline

 

#7 2008-11-15 02:27:09 pm

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

First, thanks all for your effort in your answer.
I forgot to say that I'm on AppleScript Studio, but I think that this doesen't matter.

I need this to avoid a double handler (one for the default browser and one for the user choice), but seems that solutions are longer than the problem.
I'll use [open location "myUrl"] for the default browser and [do shell script "open -a" & myBrowser & "myUrl"] for the user choice.

Nigel Garvey wrote:

Don't forget that even when you've identified the default browser, there's no guarantee that it'll be able to understand the code in your 'tell' block. In fact, it's unlikely that you'll be able to compile the code anyway with a variable in the 'tell' line.


This work:

Applescript:

set myBrowser to "Camino" as text
tell application myBrowser
   open location "[url]http://www.example.com/[/url]"
end tell

Although this open browser window behind my application window, instead using [open location] or [do shell script] without tell statement, browser open in front of my application; this is behavior I need.
Just curious to know if [tell application myBrowser] could open window as frontmost one?

Thanks again.

Offline

 

#8 2008-11-15 02:32:33 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11595
Website

Re: Get user default browser

crgand wrote:

Just curious to know if [tell application myBrowser] could open window as frontmost one?


Yes, just add activate

Applescript:


set myBrowser to "Camino" -- no coercion, a literal string IS text
tell application myBrowser
   activate
   open location "[url]http://www.example.com/[/url]"
end tell


regards

Stefan

Offline

 

#9 2008-11-15 02:43:40 pm

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

StefanK wrote:

Yes, just add activate


smilebig_smilecool

Offline

 

#10 2009-01-31 06:23:26 pm

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

Today I read this handler by Christopher Masto via John Gruber:

Applescript:

on GetDefaultWebBrowser()
   set _scpt to "perl -MMac::InternetConfig -le " & ¬
       "'print +(GetICHelper \"http\")[1]'"
   return do shell script _scpt
end GetDefaultWebBrowser

Then I can:

Applescript:

tell application GetDefaultWebBrowser()
   activate
   open location "[url]http://www.example.com/[/url]"
end tell


Filed under: default, browser

Offline

 

#11 2009-12-01 03:06:40 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

crgand wrote:

Today I read this handler by Christopher Masto via John Gruber:

Applescript:

on GetDefaultWebBrowser()
   set _scpt to "perl -MMac::InternetConfig -le " & ¬
       "'print +(GetICHelper \"http\")[1]'"
   return do shell script _scpt
end GetDefaultWebBrowser


This perl script works fine with 10.5 but I get this AppleScript Error running on 10.6.2:

Applescript:

Can't load '/System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/auto/MacPerl/MacPerl.bundle' for module MacPerl: /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/auto/MacPerl/MacPerl.bundle: no appropriate 64-bit architecture (see "man perl" for running in 32-bit mode) at /System/Library/Perl/5.10.0/darwin-thread-multi-2level/DynaLoader.pm line 207.
at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/Types.pm line 18.
Compilation failed in require at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/InternetConfig.pm line 431.
BEGIN failed--compilation aborted at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/Mac/InternetConfig.pm line 431.
Compilation failed in require.
BEGIN failed--compilation aborted. (2)

Any Idea how to resolve?
I'm running it on a MacBook 2Ghz Core2Duo.
Thanks.

Offline

 

#12 2009-12-01 03:36:39 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

Reading man perl I've find a solution adding before the perl scirp execution:

Applescript:

do shell script "defaults write com.apple.versioner.perl Prefer-32-Bit -bool yes"

and after:

Applescript:

do shell script "defaults write com.apple.versioner.perl Prefer-32-Bit -bool no"

Perhaps there is a more clean solution.

P.S. Running pert5.8.8 instead of perl give me same error.

Offline

 

#13 2009-12-01 09:54:04 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: Get user default browser

Tom_X wrote:

if you place a .webloc file in the Resources folder, this should work...


Thanks, but this is part of an application that open urls on different browsers, one of it is the default one.

Offline

 

#14 2011-08-23 03:58:10 am

yiam-jin-qui
Member
Registered: 2010-10-10
Posts: 34

Re: Get user default browser

FWIW another approach to the 32 bit issue:

Applescript:

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


天地不仁, 仁者人也

Houthakker / RobTrew

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)