Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Preview HTML from TextWrangler in Safari
-- Based on Sam Dutton's script
-- which was based on Mike Piontek's script
-- this is Safari specific
-- see for a Chrome Script.
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 <title> tag from the web page for comparison below.
set theTitle to do shell script "grep -i '<title>' " & currentURL1 & "| sed -e 's#<[^>]*>##g' | sed 's/^[ ]*//;s/[ ]*$//'"
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.
set currentTab to current tab of front window
set currentName to currentTab's name
set currentTab to current tab of front window
if (exists window 1) and (currentName is theTitle) then
set currentURL to currentURL
tell application "System Events"
tell process "Safari"
keystroke "r" using {command down}
end tell
end tell
-- display dialog "currentURL is: " & currentURL & " exists!"
open location currentURL
-- display dialog "does not exist!"
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment