Skip to content

Instantly share code, notes, and snippets.

@mbierman
Created August 2, 2012 01:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbierman/3232318 to your computer and use it in GitHub Desktop.
Save mbierman/3232318 to your computer and use it in GitHub Desktop.
Preview HTML from TextWrangler in Safari
-- 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.
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"
activate
-- 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!"
else
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