SRL Archive - Other Scripts

an other SCAR notepad, lol


Awkwardsaw

2009-07-28 02:03:58

i wanted to learn about filemenus, and saving to files, so i decided to make a little SCAR notepad :p it might not be usefull, and basic, but i feel like i'v learned a good amount from this, and its some-what cool ;)

thanks to Da 0wner, he helped me on msn, although hes bant :)

he also told me to post the notepad here lol

program New;

var
// Initform variables.
EndInitform : Boolean;
notepad : TForm;
Memo1 : TMemo;
Button1, Button2, Button3 : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;
filemenu : TMainMenu;
Title, Title1, Option : TMenuItem;

procedure LoadFile(sender: TObject);
begin
OpenDialog1 := TOpenDialog.Create(notepad);
OpenDialog1.InitialDir := AppPath;
OpenDialog1.Options := [ofFileMustExist, ofReadOnly];
OpenDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if(OpenDialog1.Execute)then
begin
LoadFromFile(Memo1, OpenDialog1.FileName);
end else
WriteLn('User pressed cancel!');
OpenDialog1.Free;
end;

procedure SaveFile(sender: TObject);
begin
SaveDialog1 := TSaveDialog.Create(nil);
SaveDialog1.InitialDir := AppPath;
SaveDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if SaveDialog1.Execute then
savetoFile(Memo1, Savedialog1.FileName);
SaveDialog1.Free;

end;

procedure clear(sender : tobject);
begin
memo1.text:= '';
end;

procedure InitformOnClose(Sender : TObject; var Action : TCloseAction);
begin
if (not(notepad.ModalResult = 1)) then
EndInitform := True;
end;

procedure Initform;
var
TimeInitform : Integer;
hotkey : TMenuAutoFlag;
begin
TimeInitform := GetSystemTime;
notepad := CreateForm;
with notepad do
begin
OnClose := @InitformOnClose;
Position := poScreenCenter;
BorderStyle := bsSingle;
BorderIcons := [biMinimize,biSystemMenu];
ClientWidth := 600;
ClientHeight := 600;
Caption := 'Akwardsaw''s Note Pad';
Color := clblack;
Font.Color := clWindowText;
Font.Height := -11;
Font.Name := 'MS Sans Serif';
Font.Style := [];
PixelsPerInch := 96;
end;
Memo1 := TMemo.Create(notepad);
with Memo1 do
begin
Parent := notepad;
Left := 2;
Top := 2;
Width := 596;
Height := 578;
Memo1.ScrollBars := ssVertical;
with Lines do
begin
Add('Memo1');
end;
text:= '';
TabOrder := 8;
end;
FileMenu := TMainMenu.Create(notepad);
Title := TMenuItem.Create(notepad);
Title.Caption := 'File';
FileMenu.Items.Add(Title);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Clear';
Option.OnClick := @clear;
FileMenu.Items.Items[0].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Load';
Option.OnClick := @LoadFile;
FileMenu.Items.Items[0].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Save';
Option.OnClick := @SaveFile;
FileMenu.Items.Items[0].Add(Option);

WriteLn('Initform compiled in ' + IntToStr(GetSystemTime - TimeInitform) + ' milliseconds!');
end;

procedure SafeInitform;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('Initform', v);
end;

procedure ShowInitformModal;
begin
notepad.ShowModal;
end;

procedure SafeShowInitformModal;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowInitformModal', v);
end;

procedure MainInitform;
begin
try
SafeInitform;
SafeShowInitformModal;
finally
FreeForm(notepad);
except
WriteLn('An error seems to have occurred in: Initform');
end;
end;

begin
ClearDebug;
GetSelf.WindowState := wsMinimized;
MainInitform;
GetSelf.WindowState := wsNormal;
if (EndInitform) then
TerminateScript;
end.

ima try to add an edit menu later, if you guys think i should :p

Dan Cardin

2009-07-28 16:55:31

GetSelf.WindowState := wsNormal;
thats kind of inconvenient. something like
        var a: TWindowState;
begin
ClearDebug;
a := GetSelf.WindowState;
GetSelf.WindowState := wsMinimized;
MainInitform;
GetSelf.WindowState := a;
would be better. Small thing, but kind of annoying without it.

Otherwise, nice and simple, yet apparently being your point, so good job.

Awkwardsaw

2009-07-28 17:12:01

if it has to do with the form, its Ron's fault ;) but i see what you mean

i'm also attempting to add in an edit menu to the menu bar, which should be fun haha

Janilabo

2009-07-28 19:31:51

Wow, great job, Awk! :D

It's really nice to see people getting more and more into building form-based scripts (utilities and such).. :p Keep working on it, mate.

1 suggestion.. When user closes the form, and if the current text has been changed, make it ask if user wants to exit without or with saving. :)

Awkwardsaw

2009-07-28 20:45:30

From: Janilabo
Wow, great job, Awk! :D

It's really nice to see people getting more and more into building form-based scripts (utilities and such).. :p Keep working on it, mate.

1 suggestion.. When user closes the form, and if the current text has been changed, make it ask if user wants to exit without or with saving. :)


thats a good idea :) i'll add that tonight if i can remember

Awkwardsaw

2009-08-06 02:57:50

i decided to update this thing out of boredness :p

program New;

var
// Initform variables.
EndInitform : Boolean;
notepad : TForm;
Memo1 : TMemo;
Button1, Button2, Button3 : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;
filemenu : TMainMenu;
Title, Title1, Option : TMenuItem;
t : string;

procedure LoadFile(sender: TObject);
begin
OpenDialog1 := TOpenDialog.Create(notepad);
OpenDialog1.InitialDir := AppPath;
OpenDialog1.Options := [ofFileMustExist, ofReadOnly];
OpenDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if(OpenDialog1.Execute)then
begin
LoadFromFile(Memo1, OpenDialog1.FileName);
end else
WriteLn('User pressed cancel!');
OpenDialog1.Free;
t:= memo1.Text;
end;

procedure SaveTehFile;
begin
SaveDialog1 := TSaveDialog.Create(nil);
SaveDialog1.InitialDir := AppPath;
SaveDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if SaveDialog1.Execute then
savetoFile(Memo1, Savedialog1.FileName);
SaveDialog1.Free;
end;

procedure SaveFile(sender: TObject);
begin
SaveTehFile;
t:= memo1.Text;
end;

procedure KeyBoredComand(letter : char);
begin
keydown(vk_control);
KeyDown(getkeycode(letter));
wait(10);
keyup(vk_control);
Keyup(getkeycode(letter));
end;

procedure copy(sender : tobject);
begin
KeyBoredComand('c');
end;

procedure paste(sender : tobject);
begin
KeyBoredComand('v');
end;

procedure cut(sender : tobject);
begin
KeyBoredComand('x');
end;

procedure Undo(sender : tobject);
begin
KeyBoredComand('z');
end;

procedure Redo(sender : tobject);
begin
KeyBoredComand('y');
end;

procedure clear(sender : tobject);
begin
memo1.text:= '';
end;

procedure InitformOnClose(Sender : TObject; var Action : TCloseAction);
begin
if memo1.text <> t then
if GetApplication.MessageBox('Text has been modified! Do you wish to save?', 'Save?', 4) = mryes then
SaveTehFile;
if (not(notepad.ModalResult = 1)) then
EndInitform := True;
end;

procedure Initform;
var
TimeInitform : Integer;
hotkey : TMenuAutoFlag;
begin
TimeInitform := GetSystemTime;
notepad := CreateForm;
with notepad do
begin
OnClose := @InitformOnClose;
Position := poScreenCenter;
BorderStyle := bsSingle;
BorderIcons := [biMinimize,biSystemMenu];
ClientWidth := 600;
ClientHeight := 600;
Caption := 'Akwardsaw''s Note Pad';
Color := clblack;
Font.Color := clWindowText;
Font.Height := -11;
Font.Name := 'MS Sans Serif';
Font.Style := [];
PixelsPerInch := 96;
end;
Memo1 := TMemo.Create(notepad);
with Memo1 do
begin
Parent := notepad;
Left := 2;
Top := 2;
Width := 596;
Height := 578;
Memo1.ScrollBars := ssVertical;
with Lines do
begin
Add('Memo1');
end;
text:= '';
TabOrder := 8;
end;
FileMenu := TMainMenu.Create(notepad);
Title := TMenuItem.Create(notepad);
Title.Caption := 'File';
FileMenu.Items.Add(Title);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Clear';
Option.OnClick := @clear;
FileMenu.Items.Items[0].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Load';
Option.OnClick := @LoadFile;
FileMenu.Items.Items[0].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Save';
Option.OnClick := @SaveFile;
FileMenu.Items.Items[0].Add(Option);

Title1 := TMenuItem.Create(notepad);
Title1.Caption := 'Edit';
FileMenu.Items.Add(Title1);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Undo';
Option.OnClick := @Undo;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Redo';
Option.OnClick := @Redo;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := '-';
Option.Enabled := false;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Cut';
Option.OnClick := @cut;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Copy';
Option.OnClick := @copy;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := 'Paste';
Option.OnClick := @paste;
FileMenu.Items.Items[1].Add(Option);

Option := TMenuItem.Create(notepad);
Option.Caption := '-';
Option.Enabled := false;
FileMenu.Items.Items[1].Add(Option);

WriteLn('Initform compiled in ' + IntToStr(GetSystemTime - TimeInitform) + ' milliseconds!');
end;

procedure SafeInitform;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('Initform', v);
end;

procedure ShowInitformModal;
begin
notepad.ShowModal;
end;

procedure SafeShowInitformModal;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowInitformModal', v);
end;

procedure MainInitform;
begin
try
SafeInitform;
SafeShowInitformModal;
finally
FreeForm(notepad);
except
WriteLn('An error seems to have occurred in: Initform');
end;
end;

begin
ClearDebug;
MainInitform;
GetSelf.WindowState := wsNormal;
t:= memo1.Text;
if (EndInitform) then
TerminateScript;
end.

i added:
edit menu
it now checks if you want to save your text if you changed it

also, how would i make the edit commands into a case? i dont think you can use parameters in senders

Ron

2009-08-07 21:48:03

Nice notepad script, very simple. :) I ran pretty smoothly in SCAR 3.12 on XP.

I can't get the Redo feature to work at all. However, I did make other modifications that you may like and that you can learn from.


program AwkwardsawsNotePad;

var
// Initform variables.
notepad : TForm;
Memo1 : TMemo;
t : string;

procedure LoadFile(sender: TObject);
var
OpenDialog1 : TOpenDialog;
begin
OpenDialog1 := TOpenDialog.Create(notepad);
OpenDialog1.InitialDir := AppPath;
OpenDialog1.Options := [ofFileMustExist, ofReadOnly];
OpenDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if(OpenDialog1.Execute)then
LoadFromFile(Memo1, OpenDialog1.FileName);
OpenDialog1.Free;
t:= memo1.Text;
end;

procedure SaveTehFile;
var
fileName : String;
SaveDialog1 : TSaveDialog;
begin
SaveDialog1 := TSaveDialog.Create(nil);
SaveDialog1.InitialDir := AppPath;
SaveDialog1.Filter := 'Text Documents (*.txt)|*.txt|' +
'All Files|*|';
if SaveDialog1.Execute then
begin
fileName := SaveDialog1.FileName;
if not(Lowercase(ExtractFileExt(fileName)) = '.txt') then
fileName := fileName + '.txt';
SaveToFile(Memo1, fileName);
end;
SaveDialog1.Free;
end;

procedure SaveFile(sender: TObject);
begin
SaveTehFile;
t := memo1.Text;
end;

procedure KeyboardCommand(letter : char);
begin
keydown(vk_control);
KeyDown(getkeycode(letter));
wait(10);
keyup(vk_control);
Keyup(getkeycode(letter));
end;

procedure Copy(sender : tobject);
begin
KeyboardCommand('c');
end;

procedure Paste(sender : tobject);
begin
KeyboardCommand('v');
end;

procedure Cut(sender : tobject);
begin
KeyboardCommand('x');
end;

procedure Undo(sender : tobject);
begin
KeyboardCommand('z');
end;

procedure Redo(sender : tobject);
begin
KeyboardCommand('y');
end;

procedure Clear(sender : tobject);
begin
memo1.text := '';
end;

procedure InitformOnClose(Sender : TObject; var Action : TCloseAction);
begin
if memo1.text <> t then
if GetApplication.MessageBox('Text has been modified! Do you wish to save?', 'Save?', 4) = mryes then
SaveTehFile;
end;

procedure InitformOnResize(Sender : TObject);
begin
// subtract 5 to get rid of the form's scrollbars
memo1.Width := notepad.ClientWidth - 5;
memo1.Height := notepad.ClientHeight - 5;
end;

procedure Initform;
var
TimeInitform : Integer;
filemenu : TMainMenu;
Title, Option : TMenuItem;
menuNames : Array of String;
menuItems : Array of Array of String;
row, col, numRow, numCol : Integer;
begin
TimeInitform := GetSystemTime;
notepad := CreateForm;
with notepad do
begin
OnClose := @InitformOnClose;
Position := poScreenCenter;
BorderStyle := bsSizeable;
BorderIcons := [biMinimize, biMaximize, biSystemMenu];
ClientWidth := 600;
ClientHeight := 600;
Caption := 'Awkwardsaw''s Note Pad';
//Color := clblack;
OnResize := @InitformOnResize;
end;
Memo1 := TMemo.Create(notepad);
with Memo1 do
begin
Parent := notepad;
Left := 2;
Top := 2;
//Width := 596;
//Height := 578;
Memo1.ScrollBars := ssVertical;
TabOrder := 1;
end;
FileMenu := TMainMenu.Create(notepad);
menuNames := ['File', 'Edit'];
numRow := GetArrayLength(menuNames);
SetArrayLength(menuItems, numRow);
// under file
menuItems[ 0 ] := ['Clear', 'Load', 'Save'];
// under edit
menuItems[ 1 ] := ['Undo', 'Redo', '-', 'Cut', 'Copy', 'Paste'];

for row := 0 to numRow - 1 do
begin
Title := TMenuItem.Create(notepad);
Title.Caption := menuNames[ row ];
FileMenu.Items.Add(Title);
numCol := GetArrayLength( menuItems[ row ] );
for col := 0 to numCol - 1 do
begin
Option := TMenuItem.Create(notepad);
Option.Caption := menuItems[ row ][ col ];
FileMenu.Items.Items[ row ].Add(Option);
end;
end;

with FileMenu.Items do
begin
// file
Items[ 0 ].Items[ 0 ].OnClick := @clear;
Items[ 0 ].Items[ 1 ].OnClick := @LoadFile;
Items[ 0 ].Items[ 2 ].OnClick := @SaveFile;
// edit
Items[ 1 ].Items[ 0 ].OnClick := @Undo;
Items[ 1 ].Items[ 1 ].OnClick := @Redo;
Items[ 1 ].Items[ 2 ].Enabled := false; // the dash
Items[ 1 ].Items[ 3 ].OnClick := @cut;
Items[ 1 ].Items[ 4 ].OnClick := @copy;
Items[ 1 ].Items[ 5 ].OnClick := @paste;
end;

WriteLn('Initform compiled in ' + IntToStr(GetSystemTime - TimeInitform) + ' milliseconds!');
end;

procedure SafeInitform;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('Initform', v);
end;

procedure ShowInitformModal;
begin
notepad.ShowModal;
end;

procedure SafeShowInitformModal;
var
v : TVariantArray;
begin
SetArrayLength(v, 0);
ThreadSafeCall('ShowInitformModal', v);
end;

procedure MainInitform;
begin
try
SafeInitform;
SafeShowInitformModal;
finally
FreeForm(notepad);
except
WriteLn('An error seems to have occurred in: Initform');
end;
end;

begin
ClearDebug;
GetSelf.WindowState := wsMinimized;
MainInitform;
GetSelf.WindowState := wsNormal;
end.

You spelt your name wrong in the title lol. I just had to fix the code when I saw that.

~Ron

Awkwardsaw

2009-08-07 21:59:03

:) thanks ron, i just got off the cpu that has SCAR on it, so i cant take a decent look at it, although i do know the changes. i'll take more of a detailed look later :D

EGM

2010-11-16 09:09:27

Ron, when I change the .ShowModal to .Show the .OnResize event causes an "access violation". What causes that?

TomTuff

2010-11-16 09:56:07

From: EGM
Ron, when I change the .ShowModal to .Show the .OnResize event causes an "access violation". What causes that?


The fact that you bumped a 1 year+ old thread caused it to break.

Awkwardsaw

2010-11-16 12:12:03

people still use SCAR?

Wizzup?

2010-11-16 13:56:54

From: Awkwardsaw
people still use SCAR?


He seems new, so he might not even know about Simba, considering SRL has many (old) resources that only mention SCAR.

Sandstorm

2010-11-16 14:03:46

From: Awkwardsaw
people still use SCAR?


I tried Simba and everything looked weird :(. Plus some of the stuff I tried didn't work, heh.

Camaro'

2010-11-16 15:16:30

From: Awkwardsaw
people still use SCAR?


I do.

Awkwardsaw

2010-11-16 16:12:26

From: Wizzup?
He seems new, so he might not even know about Simba, considering SRL has many (old) resources that only mention SCAR.


ah that's true, i haven't noticed he's new

EGM

2010-11-17 01:31:33

Wizzup? and Ron:

I use Simba exclusively since discovering it, and [sometimes painstakingly] convert whatever information I find – which is usually Scar related – over to it. Would you prefer I perform a search first and post to an old thread, or just keep writing new threads?

Can't wait till the Simba docs are completed! (Examples would be nice, too.. *hint*)

What I was trying to do is create a window to write messages to.
program ActivityLog;
const
CR = #$0D;
LF = #$0A;
CRLF = CR + LF;

var
ActivityLog: TForm;
LogMemo: TMemo;
btnCopy, btnClose: TButton;

procedure CopyMemoToClipboard(Sender: TObject);
begin
LogMemo.SELECTALL;
LogMemo.COPYTOCLIPBOARD;
end;

procedure ActivityLog_OnResize(Sender: TObject);
begin
with ActivityLog do
begin
//First set a minimum height and width:
if ClientWidth < 250 then ClientWidth := 250;
if ClientHeight < 60 then ClientHeight := 60;
//Then change the other controls:
LogMemo.Width := ClientWidth - 5;
LogMemo.Height := ClientHeight - 35;
btnCopy.Top := ClientHeight - 32;
btnClose.Top := ClientHeight - 32;
btnClose.Left := ClientWidth - 124;
end;
end;

procedure ActivityLog_Init;
begin
ActivityLog := TForm.Create(ActivityLog);
with ActivityLog do
begin
Width := 800;
Height := 120;
Top := 610;
Left := 60;
Caption := 'Activity Log:';
OnResize := @ActivityLog_OnResize;
end;
LogMemo := TMemo.Create(ActivityLog);
with LogMemo do
begin
Parent := ActivityLog;
Top := 2;
Left := 2;
Height := ActivityLog.Height - 30;
Width := ActivityLog.Width - 4;
Color := ClWhite;
Scrollbars := ssBoth;
FONT.Name := 'Lucida Console';
end;
btnCopy := TButton.Create(ActivityLog);
with btnCopy do
begin
Parent := ActivityLog;
Top := ActivityLog.Height- 27;
Left := 2;
Width := 120;
Height := 25;
Caption := 'Copy to Clipboard';
Hint := 'Copy the activity log to the Windows Clipboard.';
ShowHint := True;
OnClick := @CopyMemoToClipboard;
end;
btnClose := TButton.Create(ActivityLog);
with btnClose do
begin
Parent := ActivityLog;
Top := ActivityLog.Height - 27;
Left := ActivityLog.Width - 124;
Width := 120;
Height := 25;
Caption := 'Close';
Hint := 'Close this window.';
ShowHint := True;
Enabled := False;
end;
end;

procedure ActivityLog_SafeInit;
var
v: TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ActivityLog_Init', v);
end;

procedure ActivityLog_Show;
begin
ActivityLog.ShowModal;
end;

procedure ActivityLog_SafeShow;
var
v: TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ActivityLog_Show', v);
end;

procedure WriteLog(LogEntry: string);
var
h, m, s, ms: word;
begin
DecodeTime(Now(), h, m, s, ms);
LogMemo.Text := LogMemo.Text + format('[%d:%d:%d.%d] %s',
[h, m, s, ms, LogEntry]);
end;

procedure WriteLnLog(LogEntry: string);
var
h, m, s, ms: word;
begin
DecodeTime(Now(), h, m, s, ms);
LogMemo.Text := LogMemo.Text + format('[%d:%d:%d.%d] %s%s',
[h, m, s, ms, LogEntry, CRLF]);
end;

begin
ActivityLog_SafeInit;
WriteLnLog('Activity Log Started.');
ActivityLog_SafeShow;
end.

Do I get my answer regarding Show/ShowModal now? :D