Use Templates with placeholders in TextWrangler

Hello.

I have been annoyed that I couldn’t have at least some of the behaviour of placeholders
in BBEDit, when I use TextWrangler.

I came up with a solution, that involves a Template placed in the stationary folder
of TextWrangler. The advantage of this, is that you can set up the document-template with with markers, and they will persist into the newly document.

The outline is basically, to make an new document from a template in TextWrangler, then perform substiutions of any user defined placeholders, like #TODAY# or #PLACEHOLDER1#
for that matter.

Try putting the fragment below into a file named DemoTemplate.txt into the stationary folder of TextWrangler:

Here is a script that creates a new document in TextWrangler and fills in the details, after you have stored the text file as described above.


set template_file to (((path to application support from user domain) as text) & "TextWrangler:Stationery:DemoTemplate.txt") as alias
# stuff to fill into placeholders of The template.
# you'd like to have a dialog show up here, or somewhere else for all I know.
set thisDay to text 1 thru word -2 of (current date)'s date string -- StefanK
set adresse to "Vera"

tell application "TextWrangler"
	
	open template_file with «class OSta» -- So we open a template.
	set theDoc to document of window 1
	replace "#TODAY#" using thisDay searching in text 1 of theDoc options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	select insertion point before line 1 of window 1
	
	replace "#PLACEHOLDER1#" using adresse searching in text 1 of theDoc options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	select insertion point before line 1 of window 1
	(*
	tell theDoc
		try
			save
		on error e
			close theDoc
			return
			
		end try
	end tell
*)
end tell

Here is the result, which is shown in the new document:

Enjoy!