Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3   7.4   7.5    Statistic  

Shell.Execute

Executes a child process.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
Shell 7.5 Yes Yes Yes Yes No

MBS( "Shell.Execute"; ShellRef; Executable { ; Additional Arguments } )

Parameters

Parameter Description Example value
ShellRef The reference number from the shell. $shell
Executable The path to the executable.
Should be full path to executable.
On Windows, the system also searches path if you just specify a file name.
"/bin/ls"
Additional Arguments Optional
Pass additional arguments to send.
"-v"

Result

Returns OK or error.

Description

Executes a child process.
Parameters set by SetArgument* functions are passed first followed by any parameters you pass here.

On macOS, we use NSTask.
https://developer.apple.com/documentation/foundation/nstask

On Linux we use execve function.
https://www.unix.com/man-page/linux/3/execve/

On Windows we use CreateProcess function and we pass in a command line with the Executable parameter followed by all the arguments. If needed, we put them in quotes.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

If username is given for Windows, we use CreateProcessWithLogon function instead to provide login as a local user.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682431(v=vs.85).aspx

If you just need to launch a file, please use Files.LaunchFile or Files.Launch.

Examples

Run ffmpeg in shell to convert video to mp3 file:

Set Variable [ $shell ; Value: MBS( "Shell.New" ) ]
Set Field [ Shell::Output ; "" ]
Set Field [ Shell::Error ; "" ]
Commit Records/Requests [ With dialog: Off ]
# Where the app is:
Set Variable [ $executable ; Value: "/Applications/ffmpegX.app/Contents/Resources/ffmpeg" ]
# option: overwrite file
Set Variable [ $s ; Value: MBS( "Shell.AddArgument"; $shell; "-y" ) ]
# option: input file
Set Variable [ $s ; Value: MBS( "Shell.AddArgument"; $shell; "-i" ) ]
# Path to input file
Set Variable [ $InputFile ; Value: "/Users/cs/Desktop/movie.m4v" ]
# and output file
Set Variable [ $OutputFile ; Value: "/Users/cs/Desktop/sound.mp3" ]
# Run it!
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; $executable; $InputFile; $OutputFile) ]
Set Variable [ $error ; Value: "" ]
Set Variable [ $result ; Value: "" ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Failed to run" ; $s ]
Else
    # Loop while app runs and collect messages
    Loop
        # Wait a second or till it quits
        Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 1) ]
        # And read output
        Set Variable [ $error ; Value: $error & MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ]
        Set Variable [ $result ; Value: $result & MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ]
        Set Field [ Shell::Error ; MBS( "Text.ReplaceNewline"; $error; 1) ]
        Set Field [ Shell::Output ; MBS( "Text.ReplaceNewline"; $result; 1) ]
        # exit when done
        Exit Loop If [ MBS( "Shell.IsRunning"; $shell) ≠ 1 ]
    End Loop
    # We are done
    Commit Records/Requests [ With dialog: Off ]
End If
Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ]

See also


Shell.ClearEnvironment   -   Shell.ExitCode

Feedback: Report problem or ask question.




Links
MBS Xojo PDF Plugins