AppleScript to view HTML in a browser with TextWrangler

TextWranglerI really like TextWrangler, the free Mac text editor from Barebones Software, who make BBEdit.

I’ve just worked out how to set up TextWrangler so you can view HTML files in one or more browsers — simultaneously if necessary, and with keyboard shortcuts.

Mike Piontek at Junecloud gives instructions on how to do this in Safari:

tell application "TextWrangler" to set theFile to file of document 1
tell application "Finder" to open theFile using (path to application "Safari")

Unfortunately, this doesn’t seem to work for Google Chrome.

So I did a bit of hacking (with help from a comment by someone called Rob on TUAW) and came up with this:

tell application "TextWrangler" to set theFile to file of document 1

set appleScriptPath to theFile as text
set currentURL to POSIX path of appleScriptPath
set currentURL to ("file://" & currentURL)

tell application "Google Chrome"
activate
if (exists window 1) and (URL of active tab of window 1 is "chrome://newtab/") then
tell window 1 to set URL of active tab to currentURL
else
open location currentURL
end if
end tell

It may not be elegant, but it works.

Advertisements

About Sam Dutton

I am a Developer Advocate for Google Chrome. I grew up in rural South Australia, went to university in Sydney, and have lived since 1986 in London, England. Twitter: @SW12
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

8 Responses to AppleScript to view HTML in a browser with TextWrangler

  1. This is exactly what I was looking for! Well almost. The one thing I would wish for is to reload in the same tab (or the front tab) so that I don’t get an endless number of new tabs in preview. Any suggestions?

  2. So one way, maybe not the best or smartest would be to make one change to your script:

    if (exists window 1) and (URL of active tab of window 1 is “file:///Users/”) then

    this reloads the file in the current tab if it is coming from the local machine. There may be a more elegant way but at least this seems to work.

    Thanks for the help!

  3. After more experimentation, the following seems to be an improvement:

    if (exists window 1) and (URL of active tab of window 1 contains “file:///”) then

  4. Ok here’s my current version (hope someone finds this useful

    tell application “TextWrangler” to set theFile to file of document 1
    tell application “TextWrangler” to save document 1

    set appleScriptPath to theFile as text
    set currentURL to POSIX path of appleScriptPath
    set currentURL1 to POSIX path of appleScriptPath
    set currentURL to (“file://” & currentURL)

    — this script will get the tag from the web page for comparison below.
    set theTitle to do shell script “grep -i ” ” & currentURL1 & “| sed -e ‘s#]*>##g’ | sed ‘s/^[ ]*//;s/[ ]*$//'”

    tell application “Google Chrome”
    activate
    — using the tag above, we compare to the current open tab. If they are equal, replace the contents, otherwise, open a new tab.
    if (exists window 1) and (title of active tab of window 1 is theTitle) then
    tell window 1 to set URL of active tab to currentURL
    else
    open location currentURL
    end if
    end tell

  5. I’ve added updated versions for Chrome and Safari https://gist.github.com/3232063 and https://gist.github.com/3232318 Thanks again!

  6. Mitch Powell says:

    Thank you so much!

  7. Thanks for the script! I’m going to start using TextWrangler to write posts, now that I can preview HTML quickly.

    Here’s a version of the script that activates a file tab if it’s already open in Chrome (avoiding lots of duplicate tabs). The script replaces filename spaces with ‘%20’. Other special characters are not accounted for.

    tell application "TextWrangler" to set theFile to file of document 1
    
    set appleScriptPath to theFile as text
    set currentURL to POSIX path of appleScriptPath
    set currentURL to ("file://" & currentURL)
    
    set _atid to AppleScript's text item delimiters
    try
    	set AppleScript's text item delimiters to " "
    	set currentURL to text items of currentURL
    	set AppleScript's text item delimiters to "%20"
    	set currentURL to currentURL as text
    	set AppleScript's text item delimiters to _atid
    on error a number b
    	set AppleScript's text item delimiters to _atid
    end try
    
    tell application "Google Chrome"
    	
    	activate
    	
    	try
    		tell window 1
    			set _tabs to tabs
    			set _tabNumber to 0
    			
    			repeat with _tab in _tabs
    				set _tabNumber to _tabNumber + 1
    				if URL of _tab = currentURL then
    					set URL of _tab to currentURL
    					set active tab index to _tabNumber
    					return
    				end if
    			end repeat
    		end tell
    	end try
    	
    	-- If no tabs contain the URL:
    	open location currentURL
    	
    	activate
    	
    end tell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s