public
Last active

Preview HTML from TextWrangler in Safari

  • Download Gist
gistfile1.applescript
AppleScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
-- 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.