The objective of this script is to show what an html file looks like when displayed in Safari, by just hitting command-R or a short cut key of your preference.
It works on the active document in TextWrangler and if the html file is already displayed in Safari it will just be refreshed. If the current tab of Safari doesn’t contain the same file then it will be opened in Safari.
Ignoring any other tabs in Safari is a feature and not a bug since you sometimes want to compare the result of css etc.
You install the script in the script folder of TextWrangler, and set the short cut key from TextWranglers preferences, the list item that contains “menu”.
It is convenient to open the window - pallette at the same time, since it displays the chosen short cut immediately, which the latest version of TextWrangler don’t.
TextWrangler is the greatest value for money anyways - free which is pretty unbelievable!
I found the script via a blog post at CNET, gave it some thought concerning my own needs, and ended up with a general solution, after I had updated it according to my own taste.
Enjoy!
-- Based on Sam Dutton's http://goo.gl/27XlZ script
-- which was based on Mike Piontek's script http://goo.gl/SBkkL
-- this is Safari specific
-- see https://gist.github.com/3232063 for a Chrome Script.
-- Rewritten by McUsr based on: https://gist.github.com/mbierman/3232318
-- http://macscripter.net/viewtopic.php?pid=178129#p178129
to removeSegmentIdentifer(aUrl)
tell (a reference to my text item delimiters)
set {astid, contents of it} to {contents of it, "#"}
set urlparts to text item 1 of aUrl
set contents of it to ""
set cleanedUrl to urlparts as text
set contents of it to astid
end tell
return cleanedUrl
end removeSegmentIdentifer
tell application "TextWrangler"
if (count text documents) = 0 then
display alert "No documents was open in TextWrangler."
return
else
tell document 1
try
save
set activeFile to file of it as alias
set currentURL to URL
on error
return
end try
end tell
end if
end tell
tell (a reference to my text item delimiters)
set {astid, contents of it} to {contents of it, "localhost"}
set urlparts to text items of currentURL
set contents of it to ""
set currentURL to urlparts as text
set contents of it to astid
end tell
if currentURL does not end with "html" then
display alert "The text document 1 of TextWrangler isn't a web page"
else
tell application "TextWrangler"
find "<title>([^<]*)" searching in text 1 of text document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} without selecting match
if found of result = true then
set theTitle to grep substitution of "\\1"
else
set theTitle to missing value
end if
end tell
if theTitle is missing value then
display alert "There were no title defined in text document 1 of TextWrangler"
else
set refreshSuccess to false -- So We'll open it if we didn't find it.
do shell script "open -b com.apple.Safari"
tell application "Safari"
-- using the <title> tag above, we compare to the current open tab. If they are equal, replace the contents, otherwise, open a new tab.
if (count (windows whose visible = true)) > 0 then
set foundTab to false
try
tell current tab of front window
set {currentTabName, currentTabUrl, foundTab} to {its name, its URL, true}
end tell
end try
-- checks if the the current tab url is undefined
try
set probe to currentTabUrl
on error
set currentTabUrl to space
end try
if currentTabUrl is not space then
set currentTabUrl to my removeSegmentIdentifer(currentTabUrl)
else
set {currentTabUrl, currentTabName} to {currentURL, theTitle}
-- So we reuse an empty tab
end if
if foundTab and (currentTabName is theTitle) and (currentTabUrl is currentURL) then
set URL of current tab of front window to currentURL
set refreshSuccess to true
end if
end if
if not refreshSuccess then open location currentURL
end tell
end if
end if