Skip to content

Instantly share code, notes, and snippets.

@Leask
Created July 19, 2013 11:10
Show Gist options
  • Save Leask/6038406 to your computer and use it in GitHub Desktop.
Save Leask/6038406 to your computer and use it in GitHub Desktop.
View Safari's web page source in TextMate
(*
* Safari View Source in TextMate - v1.0 - 2/21/2010
* http://benalman.com/projects/safari-view-source-in-textmate/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*)
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