is there a "ShellExecute" funtion or similar inside javascript macros?

Posted by dreftymac on 2009-03-05 14:20

Problem: I am looking for a way to run an external process from within a komodo macro. The reason I am trying to do this from a macro instead of from a command is because I need a way to pass dynamic parameters to the command in a way that does not use the traditional %(ask) parameter provided by commands.

For example, I'd like a macro to be able to do something like this:

var random_filename1 = my_generate_random_filename();
var random_argument2 = my_generate_random_argument();

ShellExecute("myapp.exe "+random_filename1+" "+random_argument2+"");

Question: if there is not a way to do this in javascript, can someone post a stripped-down equivalent version in python using python's system calls?

random_filename1 = my_generate_random_filename();
random_argument2 = my_generate_random_argument();
subprocess.Popen (["myapp.exe " ... ])

half_a_cup | Thu, 2009-03-05 16:03

http://docs.activestate.com/komodo/5.0/komodo-js-api.html has some useful functions, but I found something more useful by looking in the Komodo XPCOM interfaces. In javascript, the "run" variable seems to implement the following idl interface (taken from lib/sdk/idl/koIRunService.idl in the Komodo directory):

interface koIRunService : nsISupports {
    // Encode/decode a command (and all options) into a string for use
    // in an "Recent Commands" MRU list.
    wstring Encode(in wstring command, in wstring cwd, in wstring env,
                   in boolean insertOutput, in boolean operateOnSelection,
                   in boolean doNotOpenOutputWindow, in string runIn,
                   in boolean parseOutput, in wstring parseRegex,
                   in boolean showParsedOutputList);
    void Decode(in wstring encoded, out wstring command,
                out wstring cwd, out wstring env,
                out boolean insertOutput, out boolean operateOnSelection,
                out boolean doNotOpenOutputWindow, out string runIn,
                out boolean parseOutput, out wstring parseRegex,
                out boolean showParsedOutputList);

        /**
         * Run the given command in the given terminal. A terminal is like the
         * Komodo Command Output window or Komodo's debugging terminals, it is
         * *not* like an xterm or windows command prompt.
         *
         * @param command {string}  The command string to run.
         * @param cwd {string}  The dir in which to run the command ('' or null
         *                      to inherit from parent).
         * @param env {string}  A string representing environment changes to
         *                      make for the child process, these are to be
         *                      merged in with the parent environment. The
         *                      format is: VAR1=value1\nVAR2=value2\n...
         * @param terminal {Components.interfaces.koITreeOutputHandler}  The
         *                 Komodo terminal widget handling the io.
         * @param termListener {Components.interfaces.koIRunTerminationListener}
         *                 Process terminalination listener.
         * @param input {string}  If supplied, then the command is run via
         *                        "command < input", where the input is placed
         *                        in a temporary file and redirected to stdin.
         * @returns {Components.interfaces.koIRunProcess}  The process handle.
         */
    koIRunProcess RunInTerminal(in wstring command, in wstring cwd, in
                                wstring env, in koITreeOutputHandler terminal,
                                in koIRunTerminationListener termListener,
                                in wstring input);

        /**
         * Run will start the process running and then return. Run has the
         * ability to run the command in a new (console) window, such as an
         * xterm or windows command prompt. Any input passed to this function
         * will be redirected using a temporary file.
         *
         * @param command {string}  The command string to run.
         * @param cwd {string}  The dir in which to run the command ('' or null
         *                      to inherit from parent).
         * @param env {string}  A string representing environment changes to
         *                      make for the child process, these are to be
         *                      merged in with the parent environment. The
         *                      format is: VAR1=value1\nVAR2=value2\n...
         * @param console {boolean} indicating whether the child should be
         *                          spawned with a new console window or not.
         * @param input {string}  If supplied, then the command is run via
         *                        "command < input", where the input is placed
         *                        in a temporary file and redirected to stdin.
         */
    void Run(in wstring command, in wstring cwd, in wstring env,
             in boolean console, in wstring input);

        /**
         * Run and capture the output/error/retval from running the given
         * command. This call will block until the process has finished
         * before returning. It is suggested not to use this method unless you
         * are absolutely sure that the process will exit quickly, otherwise
         * if called in the main UI thread, then the whole Komodo UI will be
         * locked until the process is completed!
         *
         * @param command {string}  The command string to run.
         * @param cwd {string}  The dir in which to run the command ('' or null
         *                      to inherit from parent).
         * @param env {string}  A string representing environment changes to
         *                      make for the child process, these are to be
         *                      merged in with the parent environment. The
         *                      format is: VAR1=value1\nVAR2=value2\n...
         * @param input {string}  If supplied, then the command is run via
         *                        "command < input", where the input is placed
         *                        in a temporary file and redirected to stdin.
         * @param output {string}  The resulting stdout read from the process.
         * @param error {string}  The resulting stderr read from the process.
         * @returns {int}  The return code from the finished process.
         */
    PRUint32 RunAndCaptureOutput(in wstring command,
                                 in wstring cwd, in wstring env,
                                 in wstring input,
                                 out wstring output, out wstring error);

        /**
         * Starts running the given command and then returns immediately.
         * An observer (nsIObserver) notification will be sent when the process
         * finally terminates. The arguments to the notification will be:
         *   subject: {Components.interfaces.koIRunProcess} The process handle.
         *   topic:   "run_terminated"
         *   data:    The command {string} that was just run.
         *
         * @param command {string}  The command string to run.
         * @param cwd {string}  The dir in which to run the command ('' or null
         *                      to inherit from parent).
         * @param env {string}  A string representing environment changes to
         *                      make for the child process, these are to be
         *                      merged in with the parent environment. The
         *                      format is: VAR1=value1\nVAR2=value2\n...
         * @param input {string}  If supplied, then the command is run via
         *                        "command < input", where the input is placed
         *                        in a temporary file and redirected to stdin.
         * @returns {Components.interfaces.koIRunProcess}  The process handle.
         */
    koIRunProcess RunAndNotify(in wstring command, in wstring cwd,
                               in wstring env, in wstring input);
};

For example, if you want to call a shell command and wait no more than 10 seconds before returning, you can use:
run.RunAndNotify(command,path,"","").wait(10);

If you want to do it in python, check out http://docs.python.org/library/subprocess.html#module-subprocess. subprocess.check_call(*popenargs, **kwargs) looks useful.

Additionally you can use the older os.system(command)

toddw
ActiveState Staff
Thu, 2009-03-05 16:05

chaschev | Sat, 2009-03-14 14:34

Hi,

I have nearly the same question. But instead of running a single command I need to run a shell script like this

#!/usr/bin/env ruby

print "Enter you name, please: "
name = STDIN.gets
puts "Hi from ruby, #{name}!"

Is there any way to do this and also to have a possibility to pass something to STDIN? When I'm trying to pass the script as a command, I get return code 2, which means error.

The workaround is to call interpreter (i.e. ruby) and pass the program via STDIN and the input for the script via environment variable. But I don't think it's the best way for getting things done. :)

UPDATE: oops. environment variable is not an option, because it cannot be multiline.

half_a_cup | Sat, 2009-03-14 17:13

What does the command look like when you try to run the script? I suspect you tried something like scriptfile for the command, but you need ./scriptfile or the shell will look for the script in /usr/bin, /usr/local/bin, etc (or the command can be interpreter scriptfile).

Unless I misunderstood and your script is inside a variable and not a file, in which case I suggest you save the script in a temporary file. Check out "@activestate.com/koFileService;1".

chaschev | Sun, 2009-03-15 10:07

half_a_cup,

Thanks for reply. The script is indeed in a var. The idea is simple - I want to run some simple text opertation with some different language and interpreter. You can do this in TextMate with this ${`code for some external interpreter`}. You may check out that functionality on this screencast: http://drnicwilliams.com/2008/06/11/using-ruby-within-textmate-snippets-....