Options
All
  • Public
  • Public/Protected
  • All
Menu

Module shell

Shell is Unix shell, which provides a set of commands for interacting with the operating system. Many programs can use shell to execute shell commands, such as terminal emulator.

Auto.js Pro 9 also includes a terminal emulator for executing npm, node commands. This module creates a Shell object or executes a shell command using exec function, which supports node and npm commands unless you modify the PATH environment variable.

This module provides functions createShell to create a new Shell object and exec function to execute a shell command once.

Index

Type aliases

ExitResult

ExitResult: string | number

PrivilegeType

PrivilegeType: "root" | "adb"

StandardOutputType

StandardOutputType: "stderr" | "stdout"

Functions

InputText

  • InputText(text: string): Promise<void>
  • Use default shell instance to execute input text command, simulate inputting text.

    Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

    These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

    example
    "nodejs";
    const { InputText } = require("shell");
    async function main() {
        await InputText("Hello, World");
    }
    main();
    

    Parameters

    • text: string

      Text to input, only support ASCII characters

    Returns Promise<void>

SendKey

  • SendKey(key: number | string): Promise<void>
  • Use default shell instance to execute input keyevent key command, simulate sending key.

    Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

    These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

    example
    "nodejs";
    const { SendKey, setDefaultShellOptions } = require("shell");
    
    async function main() {
        setDefaultShellOptions({
           adb: true,
        });
        await SendKey("HOME");
    }
    main();
    

    Parameters

    • key: number | string

      Key name or key code, like "HOME" or 3, see KeyEvent

    Returns Promise<void>

Swipe

  • Swipe(x1: number, y1: number, x2: number, y2: number, duration?: number): Promise<void>
  • Use default shell instance to execute input swipe x1 y1 x2 y2 duration command, simulate sliding from position (x1, y1) to position (x2, y2).

    Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

    These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

    example
    "nodejs";
    const { Swipe } = require("shell");
    async function main() {
        await Swipe(800, 100, 800, 1000);
    }
    main();
    

    Parameters

    • x1: number
    • y1: number
    • x2: number
    • y2: number
    • Optional duration: number

      Swipe duration, unit is millisecond

    Returns Promise<void>

Tap

  • Tap(x: number, y: number): Promise<void>
  • Use default shell instance to execute input tab command, simulate clicking position (x, y).

    Default shell instance will be created automatically when using Tap, Swipe, SendKey, InputText etc. If you want to use adb permission or specific shell options, you should use setDefaultShellOptions before using these functions.

    These functions start with uppercase letter, which means they require root or adb permission, otherwise they only work in this app.

    example
    "nodejs";
    const { Tap } = require("shell");
    async function main() {
        await Tap(100, 100);
    }
    main();
    

    Parameters

    • x: number
    • y: number

    Returns Promise<void>

checkAccess

  • Check if there is a specific privilege, such as whether there is root permission.

    example
    "nodejs";
    
    const { checkAccess } = require("shell");
    async function main() {
       const hasRoot = await checkAccess("root");
       const hasAdb = await checkAccess("adb");
       console.log(`hasRoot: ${hasRoot}, hasAdb: ${hasAdb}`);
    }
    main();
    

    Parameters

    Returns Promise<boolean>

    Whether there is a specific privilege

createShell

  • Create a Shell instance.

    We usually use exec function to execute a single command and get the result, but if there are multiple commands to execute, the efficiency of Shell object is higher. This is because we don't need to create a new shell process every time we execute.

    We can also listen to Shell's output by using Shell object.

    example
    "nodejs";
    const { createShell } = require("shell");
    const shell = createShell();
    shell.on("line", (line) => {
        console.log(line);
    });
    shell.exec("ls");
    
    const id = $autojs.keepRunning();
    shell.exit().then(() => $autojs.cancelKeepRunning(id));
    
    

    Parameters

    • Optional options: ShellOptions

      Shell options, will override default options

    Returns Shell

    New shell instance

exec

  • Create a new shell process, and execute a command, return the result asynchronously.

    example
    "nodejs";
    const { exec, isRootAvailable } = require("shell");
    async function main() {
        console.log(await exec("npm"));
        if (await isRootAvailable()) {
           console.log(await exec("ls /data", { root: true }));
        }
    }
    main();
    

    Parameters

    • cmd: string

      Command to execute

    • Optional options: ShellOptions

      Shell options, will override default options

    Returns Promise<ExecutionResult>

    Promise of execution result

getDefaultShellOptions

isRootAvailable

  • isRootAvailable(): Promise<boolean>
  • Check if device is rooted. Note that device is rooted does not mean this app has root permission.

    example
    "nodejs";
    
    const { isRootAvailable } = require("shell");
    async function main() {
       const rootAvailable = await isRootAvailable();
       console.log(`rootAvailable: ${rootAvailable}`);
    }
    main();
    

    Returns Promise<boolean>

setDefaultShellOptions

  • Set default options for Shell. These options include whether to use Root permission, adb permission, environment variables, etc. They will be used when creating new Shell or RootAutomator instance.

    Parameters

    Returns void