Openning a HTML file in the default browser

Hi,

I wish to open a locally stored HTML file (.html) in to the machine's default browser.

If browser is already up and running it should open the HTML file in the new tab and it should be in the focus.

I am not aware of how to find which is default browser and how to open a new tab.

Note:
The HTML file is created in the run time and I want this to be opened by my script in the end of its execution.

MacBook Pro, Mac OS X (10.5.8)

Posted on May 4, 2010 5:21 AM

Reply
12 replies

May 4, 2010 9:50 AM in response to Mowri

You set your default application for opening HTML files to TextEdit. Most users won't do that. Most will have Safari (the default for OS X), or FireFox.

You can set the default from within Finder: click on the file, and then press command-I to see the info pane. Where it says "Open with:" in the pane, select the app, and click the Change All... button to apply it to all files of that type.

You can also access the launch services property list to do the same thing programmatically.

May 6, 2010 10:02 AM in response to J D McIninch

Thanks

But I am not seeing Textedit. Please review the below which is from my machine.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSHandlerContentType</key>
<string>public.html</string>
<key>LSHandlerRoleAll</key>
<string>org.mozilla.firefox</string>
<key>LSHandlerRoleViewer</key>
<string>com.google.chrome</string>
</dict>
</plist>



- Mowri

May 9, 2010 11:55 PM in response to Mowri

Hello Mowri,

You may use 'open location' command in Standard Additions to open local html file in default browser.
Something like this.

--SCRIPT
set a to choose file -- choose local html file
set p to a's POSIX path
--set furl to "file://localhost" & p -- w/o percent escape for uri (OK ?) [1]
--set furl to "file:" & p -- w/o percent escape for uri (OK ?) [1]
set furl to uri_file(p) -- w/ percent escape for uri
open location furl
on uri_file(p)
set sh to "/bin/echo -n " & quoted form of p & " | perl -MURI::file -lne 'print URI::file->new($_);'"
do shell script sh
end uri_file
(*
[1] If unescaped version of file uri is accepted by 'open location', you'd not need uri_file() handler.
*)
--END OF SCRIPT


As for opening new tab, it would be done by browser's setting for opening new url, I'd guess.

Cheers,
H

May 10, 2010 1:14 AM in response to Hiroto

Hi Hiroto,

It is opening the file in TextEdit only.


_Events Log:_

tell current application
choose file
alias "Mowri:MyFolder:ONS.html"
do shell script "/bin/echo -n '/MyFolder/ONS.html' | perl -MURI::file -lne 'print URI::file->new($_);'"
"file:///MyFolder/ONS.html"
open location "file:///MyFolder/ONS.html"
end tell

May 11, 2010 5:08 AM in response to Mowri

Hello Mowri,

Apparently html files are associated with TextEdit.app in your environment.
It means your default browser IS TextEdit in a sense...

Does TextEdit.app has such setting in its preferences?
Or are you using something like RCDefaultApp [1] that modifies standard associations between file and application?

If there's no apparent reason for such association, I'd rebuild launch services database by using Onyx [2] or lsregister command explained in, e.g., [3].

Of course you may open a file by specified application by using open(1) command with -a or -b option [4] (provided that you know the application).
However, fixing the undesired association is the way to go, I'd think.

Good luck,
H

cf.
[1] RCDefaultApp
http://www.rubicode.com/Software/RCDefaultApp/index.html

[2] Onyx
http://www.titanium.free.fr/pgs/english/apps.html

[3] How to rebuild the LaunchServices database
http://www.macosxhints.com/article.php?story=20031215144430486

[4] open(1)
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/open.1.h tml

May 11, 2010 11:57 AM in response to Mowri

This script opens a local page before printing the page.

on open dropped_items
-- Write a message into the event log.
log " --- Starting on " & ((current date) as string) & " --- "
-- set see to alias "Macintosh-HD:Users:mac:Desktop:app.html"
-- set dropped_items to {see}

set the_app to "Safari"


repeat with dropped_item_ref in dropped_items
--set UrlBase to "xzzx FILE://" & POSIX path of dropped_item_ref
--display dialog UrlBase

-- Activate
activate application the_app

-- Open the URL
tell application the_app
open dropped_item_ref
-- print dropped_item_ref
tell application "System Events"
tell process the_app
delay 3
-- click menu item "Print..." of menu "File" of menu bar 1
keystroke "p" using command down
delay 1
keystroke return
delay 10
keystroke "w" using command down

end tell
end tell

--quit
end tell

end repeat
end open

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Openning a HTML file in the default browser

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.