Cscript-hosted VBScript Execution: Another technique

Usage tips, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Alex K. Angelopoulos
Posts: 7
Joined: Sun Feb 01, 2004 3:44 pm

Cscript-hosted VBScript Execution: Another technique

Post by Alex K. Angelopoulos »

Below is a general walkthrough of setting up TextPad for flexible scripting use. It varies slightly from the previously posted Tip on how to Run/Debug VBScript code from Textpad in that it does the following:
  • Allows supplying arbitrary arguments as selected code
    Allows jumping to error lines
1. ADD CSCRIPT AS A TOOL
In TextPad, go to Configure | Preferences. In the left-hand pane of the
Preferences window, click on "Tools". You will see an "Add" button to the
right; click that, and choose "Program" from the dropdown. When the browsing window opens, navigate to cscript.exe (usually in your windows directory or its system32 subdirectory). Select it, click open, then when the browsing window goes and you are back in "Preferences", click the "Apply" button in the lower right.

2. FIND THE TOOL IN THE LIST
Then in the big pane on the left of the Preferences window click on "Tools"
again to make sure it is selected and expanded. You should see "cscript" as a choice under "Tools"; go down to it and click it.

3. MODIFY THE TOOL TO RUN THE CURRENT FILE
If you look to the right now, you will see several options for configuring
this tool.

The topmost one, Command, will have cscript.exe in it; that's the way you
want it.

Beneath that is "Parameters". In there, insert this:

Code: Select all

//nologo "$File" $SEL
+ The //nologo suppresses logo display in execution.
+ The "$File" is TextPad's macro for the current file name, quoted to
ensure that files with spaces run OK.
+ The $SEL allows you to select some text that you may have in the file
in a comment line and have it appended as arguments.

Initial Folder: That defaults to $FileDir, which is what you want.

4. SET UP OUTPUT CAPTURE AND ERROR REGEX
At this point cscript can be launched to run a script, but some more details
give us some nice additional features.

Going down to the middle, you may want to check "Save all documents first". I do this so I can just edit and run.

You might want to check "Prompt for Parameters". I don't bother; the $SEL macro lets me keep parameters in the script in a comment line, and I can run them just by selecting them.

If you want to capture the console output (necessary for jumping to errors) select "Capture Output". I know, I know, you already guessed that... :)

Where it says "Regular expression to capture output" below the checkboxes, insert this:

Code: Select all

 ^\(.+\)(\([0-9]+\)\, \([0-9]+\)
I am not certain how well this works if you change from the default regex
style in TextPad, but this actually captures 3 subexpressions: everything
from the beginning of the line up to just before a ( character; then
numerals following that up to a non-captured comma and space; then the
numerals after that. This means that if the output looks like this:

Code: Select all

C:\bin\scripts\win32shutdown.wsf(69, 11) SWbemObjectSet: Generic failure
Then the first capture will be C:\bin\scripts\win32shutdown.wsf, the second will be 69, and the third will be 11.

We just need to set that up in the registers. For the File register, make
sure you have 1; for the Line register, put 2; and for the Column, put 3.
They may have defaulted to that anyway...

Press OK, and you're done.

5. RUNNING THE TOOL
You can select it from the "Tools" menu. If it's one of the first tools you
have installed, it will also have a Ctrl+n (where n is a number) shown next
to it for a keyboard shortcut.

To supply arbitrary commandline parameters to the script, you can simply insert as a commented line in the script, then select the text argument data you want to use. For example, if you want to run a script with the commandline arguments //X to start the debugger and with "C:\tmp\text file.txt" as a normal script-processed argument, you would insert a line of VBScript code with a preceding ' to comment it out like this:

Code: Select all

' //X "C:\tmp\text file.txt"
And then immediately before running the tool select the argument portion, //X "C:\tmp\text file.txt"
amissico
Posts: 3
Joined: Sat May 29, 2004 8:20 pm

Enabling &Keyboard Navigation

Post by amissico »

For me (v4.7.1) there is no Ctrl+<number> in the Tool menu. I could assign a new shortcut key in Preferences, yet that adds another step. The easier and simpler, the better, so I thought I would mention how configure the shortcut key while you are setting up the item.

1. ADD CSCRIPT AS A TOOL [modification]
"...choose "Program" from the dropdown." Instead, choose "DOS Command". Type in the name of the menu item you want. Place the & character in front of the letter you want to use as the keyboard navigation key. For instance, I used "&Windows Script Host (WSH)". This will allow me to type Alt+T, W to execute the command. I recommend you select a key that doesn't conflict with the other keys used in the Tools menu. Continue by clicking the "Apply" button in the lower right.

3. MODIFY THE TOOL TO RUN THE CURRENT FILE [modification]
Change the "Parameters" from "//nologo "$File" $SEL" to "cscript //nologo "$File" $SEL".

That it.

You can use either cscript or wscript, just remember that wscript doesn't provide output that can be captured by TextPad.
amissico
Posts: 3
Joined: Sat May 29, 2004 8:20 pm

Regular Expression [odd behavior]

Post by amissico »

Experimenting with the behavior of different regular expressions for capturing output, it seems you can use the Default Syntax regardless of you POSIX setting.

I believe the syntax you use at the time you are specifying "Regular expression to match output" must match your current syntax setting. For instance, I use POSIX. When I use one of the regular expressions listed below under Default Syntax when creating the Tool menu item, I get the error "Unmatched '( or {' when attempting the jump to the error in the source file. I have no problems when I switch to using TextPad's default syntax for regular expressions then create the menu item. I can even switch back to using POSIX without introducing the error. From the behavior observed, I would guess that TextPad parses the regular expression once and stores this information for later use. Allowing us to change the regular expression syntax without effecting the menu item. [Which is a good implementation on TextPad's part, because it allows users to change syntax when needed without effecting existing marcros, tools, and other items that rely on regular expressions.] After experimenting, I changed back to POSIX, and I am using the first Default Syntax listed below to run the script without the debugger, and the POSIX syntax with the debugger.

Default Syntax:

Code: Select all

^\(.+\)(\([0-9]+\)\, \([0-9]+\)
^\([^(]+\)(\([0-9]+\),[[:space:]]*\([0-9]+\))
Either one can be use.

POSIX:

Code: Select all

^([^(]+)\(([0-9]+),[[:space:]]*([0-9]+)\)
From my experience with v4.7.1, you must close and restart TextPad after changing between the two regular expression syntaxes.

In addition, I would like to thank Alex K. Angelopoulos for all his effort in documenting how to enable executing Windows Script Host from the Tools menu. Thank you!

Regards,
Anthony Missico, Jr.
Anthony Missico, Jr.
amissico
Posts: 3
Joined: Sat May 29, 2004 8:20 pm

Re: Cscript-hosted VBScript Execution: Another technique

Post by amissico »

Alex K. Angelopoulos wrote:Going down to the middle, you may want to check "Save all documents first". I do this so I can just edit and run.
For people who may not want to "save all documents" there is an option. The last note of TextPad's help topic Tool Parameter Macros states:
If a tool uses any of the macros $File, $FileName, $UNIXFile or $DOSFile, the active document will be saved prior to running the tool.
Anthony Missico, Jr.
Alex K. Angelopoulos
Posts: 7
Joined: Sun Feb 01, 2004 3:44 pm

Re: Cscript-hosted VBScript Execution: Another technique

Post by Alex K. Angelopoulos »

amissico wrote:
Alex K. Angelopoulos wrote:Going down to the middle, you may want to check "Save all documents first". I do this so I can just edit and run.
For people who may not want to "save all documents" there is an option. The last note of TextPad's help topic Tool Parameter Macros states:
If a tool uses any of the macros $File, $FileName, $UNIXFile or $DOSFile, the active document will be saved prior to running the tool.
This is a VERY handy addition. :D
Post Reply