How to run ANY console or Windows application from within an UE script/macro?

Help with setting up and configuring custom user tools in UltraEdit (based on command line input)

How to run ANY console or Windows application from within an UE script/macro?

Postby Mofi » Fri Apr 16, 2010 6:18 am

Some users have the requirement to run ANY tool from within a script or macro, but it is not possible to execute any command from within a macro or script directly. Here is a solution for that problem.

Be aware that with this solution you could also execute commands like rd /s /q C:\ which is surely not what you want. Dynamically creating and executing commands is a high security risk.



First you have to configure a user tool. Open Advanced - Tool Configuration and press the button Insert. Configure the user tool as follows:

On tab Command:

Menu Item Name: Any Console tool (for example)
Command Line: %sel%
Working Directory: (let it empty)
Toolbar bitmap/icon: (let it empty or browse to a BMP or ICO file)

On tab Options:

Program Type: Dos Program
Save Active File: unchecked
Save all files first: unchecked

On tab Output:

Command Output: Output to List Box
Show DOS Box: unchecked
Capture Output: checked
Replace selected text with: Captured Output

That user tool is for all 16-bit DOS and 32-bit console applications with no user interactions during execution. When a user must enter something during execution of the tool, option Show DOS Box would be needed. As configured the output of the called tool is captured and replaces the current selection in the active file.



To call any Windows application without the need that the macro or script waits until the called Windows application terminates, configure a user tool as follows:

On tab Command:

Menu Item Name: Any Windows tool (for example)
Command Line: %sel%
Working Directory: (let it empty)
Toolbar bitmap/icon: (let it empty or browse to a BMP or ICO file)

On tab Options:

Program Type: Windows Program
Save Active File: unchecked
Save all files first: unchecked

On tab Output:

Command Output: Append to Existing
Show DOS Box: unchecked
Capture Output: unchecked
Replace selected text with: No Replace

Windows applications do not write to standard output and therefore nothing can be captured. Also Windows applications are started always as new task. So by default the Windows command line interpreter continues immediately resulting here with terminating itself immediately after calling the Windows application and UltraEdit/UEStudio continues execution of macro/script.



When you want to run any Windows application, and you want that your macro/script executing the application should wait until the Windows application terminates itself, configure a user tool as follows:

On tab Command:

Menu Item Name: Any Windows tool (for example)
Command Line: start "UE Tool Execution" /wait %sel%
Working Directory: (let it empty)
Toolbar bitmap/icon: (let it empty or browse to a BMP or ICO file)

On tab Options:

Program Type: Dos Program
Save Active File: unchecked
Save all files first: unchecked

On tab Output:

Command Output: Append to Existing
Show DOS Box: unchecked
Capture Output: unchecked
Replace selected text with: No Replace

Be aware that the Windows application is called via the Windows standard command start which is important to know when parameters must be passed to the Windows application, too.



None of the 3 user tools saves files before execution. Do that from within the script or macro when needed before calling the tool. Take into account that a new file could be opened which require the Save As command.



Now you can use scripts and macros to call any tool using the 3 user tools.

Script template:

Code: Select all
var nActDocIndex = UltraEdit.activeDocumentIdx; // Requires UE v16.00 or later.
// For an alternate method working for UE prior v16.00 see http://www.ultraedit.com/forums/viewtopic.php?f=52&t=4571#p16586
// or just use the advanced function GetFileIndex posted at http://www.ultraedit.com/forums/viewtopic.php?f=52&t=4596#p26710
UltraEdit.newFile();
UltraEdit.activeDocument.unicodeToASCII();
UltraEdit.activeDocument.write("dir \"C:\\Program Files\"");
UltraEdit.activeDocument.selectToTop();
UltraEdit.runTool("Any Console tool");
// Evaluate the output when needed.
UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
UltraEdit.document[nActDocIndex].setActive();

Macro template:

Code: Select all
NewFile
UnicodeToASCII
"dir "C:\Program Files""
SelectToTop
RunTool "Any Console tool"
// Evaluate the output when needed.
CloseFile NoSave

It's up to you what the macro or script does before and after calling the tool.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: How to run ANY console or Windows application from within an UE script/macro?

Postby 3JB » Fri Apr 16, 2010 9:27 am

Mofi wrote:

When you want to run any Windows application, and you want that your macro/script executing the application should wait until the Windows application terminates itself, configure a user tool as follows:

On tab Command:

Menu Item Name: Any Windows tool (for example)
Command Line: start "UE Tool Execution" /wait %sel%
Working Directory: (let it empty)
Toolbar bitmap/icon: (let it empty or browse to a BMP or ICO file)


Thanks!

I ended up using the above quoted option, except I change the "Command Line:" option to:
start "UE Tool Execution" %sel%

This gave me the desired results, different from the other options given.
3JB
Newbie
 
Posts: 7
Joined: Thu May 21, 2009 8:23 am


Return to Custom User Tools/Tool Configuration