utils.shellexecute

Executes a Windows "shell" command

Prototype

ok, error = utils.shellexecute (filename, params, defdir, operation, show_command)

Description

Only the first argument (filename) is required, the rest are optional.


filename: The file to open or print or the folder to open or explore.
The function can open an executable file or a document file.
The function can print a document file.

params: A string that specifies the parameters to be passed to the application.
If filename specifies a document file, params should be nil or "".

defdir: A string that specifies the default directory.

operation: The following operation strings are valid:
"open": The function opens the file specified by the filename parameter.
The file can be an executable file or a document file. It can also be a folder.
"print": The function prints the file specified by filename.
The file should be a document file.
If the file is an executable file, the function opens the file,
as if "open" had been specified.
"explore": The function explores the folder specified by filename.

This parameter can be nil or "".
In that case, the function opens the file specified by filename.

show_command: If filename specifies an executable file, show_command specifies how the application is to be shown when it is opened.

This parameter can be nil in which case it defaults to 1 - the recommended default.

This parameter can be one of the following values:

0: Hides the window and activates another window.
1: Activates and displays a window.
If the window is minimized or maximized, Windows restores it to
its original size and position.
An application should specify this flag when displaying the window for the first time.
2: Activates the window and displays it as a minimized window.
3: Activates the window and displays it as a maximized window.
4: Displays a window in its most recent size and position.
The active window remains active.
5: Activates the window and displays it in its current size and position.
6: Minimizes the specified window and activates the next top-level window in the z-order.
7: Displays the window as a minimized window.
The active window remains active.
8: Displays the window in its current state.
The active window remains active.
9: Activates and displays the window.
If the window is minimized or maximized, Windows restores it to its original
size and position.
An application should specify this flag when restoring a minimized window.
10: Sets the show state based on the SW_ flag specified in the STARTUPINFO structure
passed to theCreateProcess function by the program that started the application.
An application should call ShowWindow with this flag to set the initial show
state of its main window.


If sucessful, the function returns true.

If not, it returns nil followed by an error message. You could use "assert" to test for failure.


Examples:

assert (utils.shellexecute ("c:/mushclient/worlds/SMAUG.MCL"))-- document
assert (utils.shellexecute ("http://www.gammon.com.au/"))     -- web page
assert (utils.shellexecute ("mailto:someone@somewhere.com"))  -- open mail client
assert (utils.shellexecute ("c:/", nil, nil, "explore"))      -- explore disk
assert (utils.shellexecute ("c:/readme.txt", nil, nil, "print"))  -- print a file
Warning: If you plan to redirect output (eg. capture a directory listing to a file) you may need to invoke the "cmd" shell. For example:

utils.shellexecute ("cmd", "/C dir > test.txt", GetInfo (68))
You may also want to pass 0 as the fifth argument (hide the window) in order to stop a command window flickering into view and then disappearing, for example:

utils.shellexecute ("cmd", "/C dir > test.txt", GetInfo (68), "open", 0)

Lua functions

Topics