Friday, January 19, 2018

#1 2015-01-20 05:09:40 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

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 #TODAY#

Dear #PLACEHOLDER1#.


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.

Applescript:


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:

Here onsdag 21. januar

Dear Vera.


Enjoy!

Last edited by McUsrII (2015-01-20 05:10:49 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)