Skip to content

OS

The OS class provides static methods to execute OS commands. It supports both synchronous and asynchronous execution, allowing users to run external programs with optional arguments.

Methods:

  • exec(program: string, ...args: string[])
    Executes a program or command synchronously. This method runs the specified program with the provided arguments and waits for it to complete before returning control to the caller.

    • program: The name or path of the program/command to execute.
    • args: (Optional) A list of arguments to pass to the program/command.
  • execAsync(program: string, ...args: string[]): Promise<void>
    Executes a program or command asynchronously. This method returns a Promise that resolves when the program completes its execution, allowing other tasks to continue while the program runs. Although javascript is single threaded, this method will execute external programs in a separate thread.

    • program: The name or path of the program/command to execute.
    • args: (Optional) A list of arguments to pass to the program/command.
Example 1
// Synchronously execute a program with arguments
OS.exec('notepad.exe', 'C:/example.txt');
Example 2
OS.execAsync('notepad.exe', 'C:/example.txt')
    .then(()=> console.log('Program finished running'))
    .catch(err => console.error('Error running program:', err));