Back

CreateSysProc

JS Py
Hello World
Content:
- Properties
- Methods

Creates a shell SystemProcess (ie. “sh”, “su” if root or “busybox” if installed) which can be reused throughout the program.

sys = app.CreateSysProc( cmd, env?, dir?, options? ) → app object: SysProc

If env or dir are not provided HOME and TMDDIR are set by the component.

Example - Basic

function OnStart()
{
    lay = app.CreateLayout( "linear" );

    txt = app.CreateText( "", 1, 1, "Log,Monospace,autoscale" );
    lay.AddChild( txt );

    app.AddLayout( lay );

    sys = app.CreateSysProc( "sh" );
    sys.Out( "netstat\n" );
    sys.SetOnInput( sys_OnInput );
    sys.SetOnError( sys_OnError );
    sys.Out( "netstoat\n" );
}

function sys_OnInput( msg )
{
    txt.Log( msg );
}

function sys_OnError( msg )
{
    txt.Log( msg );
}
from native import app

def OnStart():
    global txt
    lay = app.CreateLayout( "linear" )

    txt = app.CreateText( "", 1, 1, "Log,Monospace,autoscale" )
    lay.AddChild( txt )

    app.AddLayout( lay )

    sys = app.CreateSysProc( "sh" )
    sys.Out( "netstat\n" )
    sys.SetOnInput( sys_OnInput )
    sys.SetOnError( sys_OnError )
    sys.Out( "netstoat\n" )

def sys_OnInput( msg ):
    txt.Log( msg )

def sys_OnError( msg ):
    txt.Log( msg )
Copy All       Run      

Example - Colored

function OnStart()
{
    lay = app.CreateLayout( "linear" );

    scr = app.CreateScroller( 1, 1, "horizontal" );
    lay.AddChild( scr );

    txt = app.CreateText( "", 1, -1, "monospace,log" );
    txt.SetTextSize( 8 );
    txt.SetLog( 1000 );
    scr.AddChild( txt );

    app.AddLayout( lay );

    sys = app.CreateSysProc( "sh" );
    sys.SetOnInput( sys_OnInput );
    sys.SetOnError( sys_OnError );

    Exec( "netstat\n" );

    // filter files containing 'D' in /sdcard/ and forward to stderr
    Exec( "ls -al /sdcard/ | grep D >&2\n" );

}

function Exec( cmd )
{
    sys.Out( cmd );
    txt.Log( cmd, "green" );
    scr.ScrollTo( 0, txt.GetHeight() );
}

function sys_OnInput( msg )
{
    txt.Log( msg );
    scr.ScrollTo( 0, txt.GetHeight() );
}

function sys_OnError( msg )
{
    txt.Log( msg, "red" );
    scr.ScrollTo( 0, txt.GetHeight() );
}
from native import app

def OnStart():
    global scr, txt, sys
    lay = app.CreateLayout( "linear" )

    scr = app.CreateScroller( 1, 1, "horizontal" )
    lay.AddChild( scr )

    txt = app.CreateText( "", 1, -1, "monospace,log" )
    txt.SetTextSize( 8 )
    txt.SetLog( 1000 )
    scr.AddChild( txt )

    app.AddLayout( lay )

    sys = app.CreateSysProc( "sh" )
    sys.SetOnInput( sys_OnInput )
    sys.SetOnError( sys_OnError )

    Exec( "netstat\n" )

    # filter files containing 'D' in /sdcard/ and forward to stderr
    Exec( "ls -al /sdcard/ | grep D >&2\n" )

def Exec( cmd ):
    sys.Out( cmd )
    txt.Log( cmd, "green" )
    scr.ScrollTo( 0, txt.GetHeight() )

def sys_OnInput( msg ):
    txt.Log( msg )
    scr.ScrollTo( 0, txt.GetHeight() )

def sys_OnError( msg ):
    txt.Log( msg, "red" )
    scr.ScrollTo( 0, txt.GetHeight() )
Copy All       Run      

Properties

The following properties are available on the SysProc object:

dataObject: { key, value }

Methods

The following methods are available on the SysProc object:

GetType() → String: “SysProc”
Method( name, types?, p1?, p2?, p3?, p4? ) → all types
ReadFileAsByte( file ) → Number: bytes
all types
String
Number: integer
String: path to folder ( “/absolute/...” or “relative/...” )
String: path to file ( “/absolute/...” or “relative/...” )
String: program name: “sh” or “su” or “busybox”
String: comma “,” separated: combine or builder
String: comma “,” separated: nowait
String: comma “,” separated: “boolean”, “char”, “byte”, “short”, “int”, “long”, “float”, “double”, “String”, “CharSequence”, “...”
Object: { command: args }
function( data )
sys.Batch
Batch method calls to be able to set all object's properties at once.
Note that you need to specify each parameter (use “” or null to leave some out)
Inherited methods can be called by appending an underscore to the function name (ie. txt.Batch({ SetBackColor_: [“red”] })
sys.data
An object for saving individual extra properties.
sys.Err
Read data from stderr
sys.GetType
Returns the control class name.
sys.In
Read data from stdin
sys.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
sys.Out
Writes a command to stdout. A trailing linebreak will execute it.
sys.ReadFileAsByte
Returns the first byte of a file.
sys.SetOnError
Define a callback function which is called when something was written to stderr
sys.SetOnInput
Define a callback function which is called when something was written to stdout
sys.WriteToFile
Write a binary string to a file.