Wrap it! script

Ask questions about how to create a script or swap scripts with other users.
Post Reply
kbieb86
Posts: 41
Joined: 26 Aug 2016 13:49

Wrap it! script

Post by kbieb86 » 16 Sep 2016 19:15

This script opens up a dialog box with eight buttons, one each for "", '', (), {}, [], <>, /**/, <!---->. Pressing one wraps the selected text in that set of symbols. I have this assigned to a shortcut key and I "page through" the buttons with the arrow keys and hit return for the one I want.

Code: Select all

function wrapSelection(e) {
	myForm.Close;
	switch(e.Name) {
		case "doubleQuote": {
			Document.SelText = "\"" + Document.SelText + "\"";
			break;
		}
		case "singleQuote": {
			Document.SelText = "'" + Document.SelText + "'";
			break;
		}
		case "parentheses": {
			Document.SelText = "(" + Document.SelText + ")";
			break;
		}
		case "curlyBraces": {
			Document.SelText = "{" + Document.SelText + "}";
			break;
		}
		case "squareBracket": {
			Document.SelText = "[" + Document.SelText + "]";
			break;
		}
		case "angleBracket": {
			Document.SelText = "<" + Document.SelText + ">";
			break;
		}
		case "jsComment": {
			Document.SelText = "/*" + Document.SelText + "*/";
			break;
		}
		case "htmlComment": {
			Document.SelText = "<!--" + Document.SelText + "-->";
			break;
		}
	}
}
TForm myForm = new TForm(nil);
myForm.Caption = "Wrap It!";
myForm.BorderStyle = bsDialog;
myForm.Position = poScreenCenter;
var buttonTopIncrement = 35;
var buttonTop = 5;
var buttonLeft = 5;
var buttonWidth = 90;
myForm.Width = buttonWidth + 25;
TButton wrapWithDoubleQuote = TButton.Create(myForm);
wrapWithDoubleQuote.Parent = myForm;
wrapWithDoubleQuote.Name = "doubleQuote";
wrapWithDoubleQuote.Caption = "\"selection\"";
wrapWithDoubleQuote.Width = buttonWidth;
wrapWithDoubleQuote.Top = buttonTop;
wrapWithDoubleQuote.Left = buttonLeft;
wrapWithDoubleQuote.OnClick = "wrapSelection";
TButton wrapWithSingleQuote = TButton.Create(myForm);
wrapWithSingleQuote.Parent = myForm;
wrapWithSingleQuote.Name = "singleQuote";
wrapWithSingleQuote.Caption = "'selection'";
wrapWithSingleQuote.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithSingleQuote.Top = buttonTop;
wrapWithSingleQuote.Left = buttonLeft;
wrapWithSingleQuote.OnClick = "wrapSelection";
TButton wrapWithParentheses = TButton.Create(myForm);
wrapWithParentheses.Parent = myForm;
wrapWithParentheses.Name = "parentheses";
wrapWithParentheses.Caption = "(selection)";
wrapWithParentheses.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithParentheses.Top = buttonTop;
wrapWithParentheses.Left = buttonLeft;
wrapWithParentheses.OnClick = "wrapSelection";
TButton wrapWithCurlyBraces = TButton.Create(myForm);
wrapWithCurlyBraces.Parent = myForm;
wrapWithCurlyBraces.Name = "curlyBraces";
wrapWithCurlyBraces.Caption = "{selection}";
wrapWithCurlyBraces.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithCurlyBraces.Top = buttonTop;
wrapWithCurlyBraces.Left = buttonLeft;
wrapWithCurlyBraces.OnClick = "wrapSelection";
TButton wrapWithSquareBracket = TButton.Create(myForm);
wrapWithSquareBracket.Parent = myForm;
wrapWithSquareBracket.Name = "squareBracket";
wrapWithSquareBracket.Caption = "[selection]";
wrapWithSquareBracket.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithSquareBracket.Top = buttonTop;
wrapWithSquareBracket.Left = buttonLeft;
wrapWithSquareBracket.OnClick = "wrapSelection";
TButton wrapWithAngleBracket = TButton.Create(myForm);
wrapWithAngleBracket.Parent = myForm;
wrapWithAngleBracket.Name = "angleBracket";
wrapWithAngleBracket.Caption = "<selection>";
wrapWithAngleBracket.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithAngleBracket.Top = buttonTop;
wrapWithAngleBracket.Left = buttonLeft;
wrapWithAngleBracket.OnClick = "wrapSelection";
TButton wrapWithJSComment = TButton.Create(myForm);
wrapWithJSComment.Parent = myForm;
wrapWithJSComment.Name = "jsComment";
wrapWithJSComment.Caption = "/*selection*/";
wrapWithJSComment.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithJSComment.Top = buttonTop;
wrapWithJSComment.Left = buttonLeft;
wrapWithJSComment.OnClick = "wrapSelection";
TButton wrapWithHTMLComment = TButton.Create(myForm);
wrapWithHTMLComment.Parent = myForm;
wrapWithHTMLComment.Name = "htmlComment";
wrapWithHTMLComment.Caption = "<!--selection-->";
wrapWithHTMLComment.Width = buttonWidth;
buttonTop = buttonTop + buttonTopIncrement;
wrapWithHTMLComment.Top = buttonTop;
wrapWithHTMLComment.Left = buttonLeft;
wrapWithHTMLComment.OnClick = "wrapSelection";
myForm.Height = buttonTop + 65;
myForm.ShowModal;
myForm.Free;
09/20/2016 edit: added comment wrapping
10/11/2016 edit: minor coding edit, no functionality change

Nierewa
Posts: 3
Joined: 21 Feb 2019 09:37

Re: Wrap it! script

Post by Nierewa » 13 Mar 2019 10:01

How can I add this script to Rj TextEd?
I can't figure out how to work with scripts.

User avatar
Rickard Johansson
Site Admin
Posts: 5781
Joined: 19 Jul 2006 14:29

Re: Wrap it! script

Post by Rickard Johansson » 14 Mar 2019 15:39

Copy and paste the code to a new file and save it as e.g. "Wrap It.jsfs".

Add the script as a tool (Tools -> Configure tools...). Just remember to select script in the drop down box. The default is "Executable (.exe|.com|.bat)".

Nierewa
Posts: 3
Joined: 21 Feb 2019 09:37

Re: Wrap it! script

Post by Nierewa » 25 Mar 2019 14:24

Cool.
Thanks :-D

And this can be done the same way for all scripts?

Post Reply