[TxMt] open Safari Source in TM

Fred B. fredb7 at starflam.com
Thu Mar 3 18:33:04 UTC 2005


Hi,

I made a script that opens the source of the frontmost Safari document  
in TextMate, with syntax-coloring.
I used a similar one for BBEdit a long time ago. (or was it only 5  
months? ;)

With Applescript, it always look quite easy when you start, then you  
end up spending hours to get it to work properly.
AS is as easy to read as it's hard to write. But it works!
You can see I'm a little AS challenged, so advices are welcome.

Basically, it takes the source and the title of the frontmost Safari  
window (warns you if there is none, or if it's blank),
remove "http://" and replace any "/" in the title with ":", makes a  
file in /tmp with the title as name (adding ".html" if it's not already  
there) and opens it in TM.

Should I put it on the wiki or in the repository?

Hope this can be useful to someone.

--
Fred

Download it here: http://osxgeek.org/tm/Source2TM.zip or copy/paste it  
Script Editor.

======================================================================== 
=
tell application "Safari"
	if not (exists document 1) then
		display dialog "You need to open a web location first!" buttons  
{"OK"} default button 1
		return
	end if
	set mySource to the source of front document as text
	if (length of mySource is 0) then
		display dialog "You need to open a web location first!" buttons  
{"OK"} default button 1
		return
	end if
	set myName to name of front document as text
end tell

try
	tell application "TextMate" to activate
	
	set myName to replace_chars(myName, "http://", "")
	set myName to replace_chars(myName, "/", ":")
	
	if (myName ends with ".html") or (myName ends with ".htm") then
		set myPath to "/tmp/" & myName
	else
		set myPath to "/tmp/" & myName & ".html"
	end if
	
	do shell script "rm -f " & quoted form of myPath
	do shell script "echo " & quoted form of mySource & " >> " & quoted  
form of myPath
	do shell script "open -a TextMate " & quoted form of myPath
end try

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars
======================================================================== 
=




More information about the textmate mailing list