Script to open template, with cursor position

Ask questions about how to create a script or swap scripts with other users.
Post Reply
jerry1970
Posts: 226
Joined: 25 Jul 2006 07:41
Location: Netherlands

Script to open template, with cursor position

Post by jerry1970 »

Hi all,

This is a script I made to create a new file based on a template.

First there is a base script:

Code: Select all

procedure CreateNewFile( extension: WideString );
var
   strFilename: WideString;
   strOriginal: WideString;
   
   i: Integer;
   iLineCount: Integer;
   iPos: Integer;
   strOriginalLine: WideString;
   strNew1, strNew2: WideString;
   strNewLine: WideString;
   
begin
   strFilename := MainApp.AppDataPath;
   strFilename := strFilename + '\Templates\';
   extension := Lowercase( extension );
   
   if extension = '' then extension := 'txt';
   if extension = 'htm' then extension := 'html';
   
   if extension = 'txt' then
      strFilename := strFilename + 'TXT\Empty.txt';
   if extension = 'php' then
      strFilename := strFilename + 'PHP\Empty.php';
   if extension = 'html' then
      strFilename := strFilename + 'Html\Empty.html';
   
   if not MainApp.OpenFile( strFilename ) then
   begin
      ShowMessage( 'Could not open template "strFilename"!' );
   end
   else
   begin
      MainApp.OpenFile( strFilename );
      strOriginal := Document.Text;
      Document.Close();
      MainApp.NewDocument( '.' +  extension );
      Document.Text := strOriginal;
      iLineCount := Document.LineCount;
      for i := 0 to iLineCount do
      begin
         strOriginalLine := Document.Lines(i);
         iPos := Pos( '^!', strOriginalLine );
         if iPos > 0 then
         begin
            strNew1 := '';
            if iPos > 1 then
               strNew1 := Copy( strOriginalLine, 1, iPos - 1 );
            strNew2 := '';
            if Length( strOriginalLine ) > iPos + 2 then
               strNew2 := Copy( strOriginalLine, iPos + 2, Length( strOriginalLine ) + iPos - 2 );
            strNewLine := strNew1 + strNew2;
            Document.Lines(i) := strNewLine;
            Document.CursorX := iPos-1;
            Document.CursorY := i;
            Document.Modified := False;
         end;
      end;
   end;
end;
begin
end.
Save this as CreateNewFile.pas in the scripts folder.

Then copy and paste the following into a new script file and save it as CreateNewFilePhp.pas:

Code: Select all

uses 'CreateNewFile.pas';
begin
   CreateNewFile( 'php' );
end.
My template looks like this:

Code: Select all

<?php
^!
?>
The ^! is where the cursor is positioned after creating the new file.

The only drawback is that the template file is put in the list of Recently Used Files.

I hope the scripts will get a MainApp.NewDocumentByTemplate function, or that the MainApp.NewDocument function gets a second parameter for the template...

Note! While working in the Script Editor, the line

Code: Select all

uses 'CreateNewFile.pas';
should be:

Code: Select all

uses '..\..\..\scripts\CreateNewFile.pas';
This is a bug, hopefully corrected in 4.50! :)

Jerry
Post Reply