Script to open template, with cursor position
Posted: 28 May 2008 15:13
Hi all,
This is a script I made to create a new file based on a template.
First there is a base script:
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:
My template looks like this:
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
should be:
This is a bug, hopefully corrected in 4.50! 
Jerry
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.
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.
Code: Select all
<?php
^!
?>
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';
Code: Select all
uses '..\..\..\scripts\CreateNewFile.pas';

Jerry