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.4   6.5   7.0   7.1   7.2   7.3   7.4   7.5   8.0   8.1    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) ]

Query python version:

Set Variable [ $shell ; Value: MBS( "Shell.New" ) ]
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/python"; "-E"; "-c"; "import sys;print(sys.version_info);") ]
Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 1) ]
Set Variable [ $error ; Value: MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ]
Set Variable [ $result ; Value: MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ]
Set Field [ Shell::Error ; MBS( "Text.ReplaceNewline"; $error; 1) ]
Set Field [ Shell::Output ; MBS( "Text.ReplaceNewline"; $result; 1) ]
Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ]

Expand tar archive:

Set Variable [ $DestFolder ; Value: "/Users/cs/Desktop" ]
Set Variable [ $TarFile ; Value: "/Users/cs/Desktop/FNFTES.tar " ]
# run tar
Set Variable [ $shell ; Value: MBS( "Shell.New" ) ]
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/tar"; "-xv"; "-C"; $DestFolder; "-f"; $TarFile) ]
Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 1) ]
Set Variable [ $error ; Value: MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ]
Set Variable [ $result ; Value: MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ]
Set Field [ Shell::Error ; MBS( "Text.ReplaceNewline"; $error; 1) ]
Set Field [ Shell::Output ; MBS( "Text.ReplaceNewline"; $result; 1) ]
Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ]

Run a python script:

Set Variable [ $shell ; Value: MBS("Shell.New") ]
Set Variable [ $s ; Value: MBS("Shell.Execute"; $shell; "/usr/local/bin/python3"; "/Users/morkus/MBSTest/MBS.py"; PythonMBSTest::Input) ]
Set Variable [ $s ; Value: MBS("Shell.Wait"; $shell; 1) ]
Set Variable [ $error ; Value: MBS("Shell.ReadErrorText"; $shell; "UTF-8") ]
Set Variable [ $result ; Value: MBS("Shell.ReadOutputText"; $shell ; "UTF-8") ]
Set Field [ PythonMBSTest::output ; $result ]

Run defaults to change preferences for Dock application:

Set Variable [ $error ; Value: "" ]
Set Variable [ $result ; Value: "" ]
Set Variable [ $shell ; Value: MBS( "Shell.New" ) ]
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/defaults"; "write"; "com.apple.dock"; "persistent-apps"; "-array-add"; "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Applications/Font Book.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>") ]
Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 2) ]
Set Variable [ $error ; Value: $error & MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ]
Set Variable [ $result ; Value: $result & MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ]
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/bin/killall"; "Dock") ]
Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 2) ]
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) ]
Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ]

Start VPN on Mac:

Set Variable [ $shell ; Value: MBS( "Shell.New" ) ]
Set Variable [ $s ; Value: MBS( "Shell.Execute"; $shell; "/usr/sbin/scutil"; "--nc"; "start"; "VPN Name") ]
Set Variable [ $error ; Value: "" ]
Set Variable [ $result ; Value: "" ]
If [ MBS("IsError") ]
    Show Custom Dialog [ "Failed to run" ; $s ]
Else
    Set Variable [ $s ; Value: MBS( "Shell.Wait"; $shell; 2) ]
    Set Variable [ $error ; Value: MBS( "Shell.ReadErrorText"; $shell; "UTF-8") ]
    Set Variable [ $result ; Value: MBS( "Shell.ReadOutputText"; $shell; "UTF-8") ]
End If
Set Variable [ $r ; Value: MBS("Shell.Release"; $shell) ]
If [ Length ( $error ) > 0 ]
    Show Custom Dialog [ "VPN Start" ; $error & ¶ & $result ]
End If

See also

Example Databases


Shell.ClearEnvironment   -   Shell.ExitCode

Feedback: Report problem or ask question.




Links
MBS Xojo PDF Plugins