I 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.
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?
Thanks for your comments and information!
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!
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
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
I’ve added updated versions for Chrome and Safari https://gist.github.com/3232063 and https://gist.github.com/3232318 Thanks again!
Thank you so much!
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.