Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created February 22, 2010 03:18
Show Gist options
  • Save cowboy/310742 to your computer and use it in GitHub Desktop.
Save cowboy/310742 to your computer and use it in GitHub Desktop.
Safari View Source in Textmate
(*
* Safari View Source in Textmate - v1.0 - 2/21/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* Recommended for use with FastScripts script menu,
* with the cmd-u hotkey bound to it, for easy access!
*)
tell application "Safari"
set src to source of document 1
set location to URL of document 1
end tell
set file_name to do shell script "echo " & quoted form of location & " | perl -pe 's/^.*\\/\\/[^\\/]+.*\\/([^?#]*).*$/$1/'"
set file_base to do shell script "echo " & quoted form of file_name & " | perl -pe 's/\\.[^.]*$//'"
set file_ext to do shell script "echo " & quoted form of file_name & " | perl -pe 's/^(?:[^.]*\\.?|.*\\.(.*))$/$1/'"
if file_base is "" then set file_base to "file"
set extensions_to_keep to "js css txt"
if (" " & extensions_to_keep & " ") does not contain (" " & file_ext & " ") then set file_ext to "html"
set tmp to (do shell script "mktemp -d /tmp/textmate.XXXXXX") & "/" & file_base & "." & file_ext
try
set handle to POSIX file tmp
open for access handle with write permission
write src to handle
close access handle
on error
do shell script "curl " & quoted form of location & " -o " & quoted form of tmp
end try
tell application "TextMate"
activate
open tmp
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment