View source in Safari with TextMate
Some days ago Lukas Smith asked me if it's possible to get source highlighting in Safari. For Firefox I used the ViewSourceWith extension for a long time to use TextMate. So now I wanted to get that in Safari too. Usually I use Safari to browse and Firefox to develop so it's not really necessary but a nice feature.
I found a cool plugin for Safari called SafariScript which ships a view source in TextMate script. But the script is pretty poor. It just fetches the URL via curl to a specified file, so you can only access public sites and only one page at time. The other scripts are very cool trough for example the "Open page in Firefox" script which I love when I opened the page in the wrong browser.
I came up with my own script which you can see bellow. It fetches the source directly from Safari instead of downloading the URL and it adds a timestamp and random number to the tempfile so it should be possible to open as much pages as you want. Just put it in place of the old script and add a cool keyboard shortcut (I use ⌘U as in Firefox).
property newline : ASCII character 10
set timestamp to do shell script ("date +%s") & (random number from 1 to 1024) as text
set tmpfile to POSIX path of ((path to "temp" as Unicode text) & "view_source" & (timestamp) & ".html ")
set tmpfileSpec to ((path to "temp" as Unicode text) & "view_source" & (timestamp) & ".html ") as file specification
tell application "Safari"
set htmlSource to source of document of front window as text
end tell
set fp to open for access tmpfileSpec with write permission
set eof of fp to 0
write (htmlSource) to fp starting at eof
close access fp
tell application "TextMate"
activate
open tmpfileSpec
end tell
do shell script "rm -f -- '" & tmpfile & "'"