gorn.ch

Tobias Ebnöther's personal blog

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 & "'"
Related Entries:
TextMate Presentation
Comments (5)  Permalink

Comments

christian @ 09.03.2008 23:59 CET
I regret to have to inform you that all your work was futile.

Activate the debug menu in Safari (Google for it...), and you'll get a new context menu point named 'Element Information'. Boom. Colored source code and other goodies. Also fun for CSS development.
ebi @ 10.03.2008 00:12 CET
Well I forgot a little about that one (does that mean my code worked on safari on the first hit, latetly? :-o), but at the end it's not exactly the same that's why I'm using it on firefox too.

One thing I really like is that I can switch between the windows in TextMate and don't have to tab between different apps. For CSS tweaking I'm currently trying out CSSEdit which was part of the MacHeist II bundle but I'm not yet sure if I like it more or less than trying out inside the browser (usually with firebug) or directly on the source and do massive refreshes :)
christian @ 11.03.2008 00:16 CET
Wellll, you know that you have a very cool preview function in CSS Edit? No need to do refreshes.

Know your foes & tools. :-)
ebi @ 11.03.2008 08:34 CET
Sure thing, without that one CSSEdit would be completely useless since I really don't like the editor as it is :)
Ulrich Zehetner @ 06.02.2009 19:09 CET
With a slight change the script transfers the Safari page to Word!

property editor : "Microsoft Word"
property newline : ASCII character 10

set timestamp to do shell script ("date +%s") & (random number from 1 to 1024) as text
set tmpfile to (path to "temp" as Unicode text) & "view_source" & (timestamp) & ".html "
set tmpfileSpec to tmpfile 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 editor
activate
open tmpfileSpec
end tell

do shell script "rm -f -- '" & POSIX path of tmpfile & "'"
No new comments allowed (anymore) on this post.
Powered by Flux CMS