Description
The Application class provides methods to manage applications in the AgentBay cloud environment. These methods include listing installed applications, starting applications, and stopping running processes.
Method | Description | Environment | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| Gets a list of installed applications. Supports filtering by Start menu and desktop shortcuts. System applications can be excluded. | Not supported | Supported | Not supported | Supported | Not supported |
| Lists all applications that have visible windows. | Not supported | Supported | Not supported | Not supported | Not supported |
| Starts a specified application. Supports setting a working directory. On the Android platform, it supports specifying an Activity. | Not supported | Supported | Not supported | Supported | Not supported |
| Stops an application by its process name. | Not supported | Supported | Not supported | Not supported | Not supported |
| Stops an application by its process ID. | Not supported | Supported | Not supported | Not supported | Not supported |
| Stops an application process by a command. | Not supported | Supported | Not supported | Supported | Not supported |
Properties
Property | Description |
Name | The application name. |
StartCmd | The start command. |
StopCmd | The stop command (optional). |
WorkDirectory | The working directory (optional). |
PName | The process name. |
PID | The process ID. |
CmdLine | The command-line parameters (optional). |
Methods
GetInstalledApps - Get installed applications
Golang
func (am *ApplicationManager) GetInstalledApps(includeSystemApps bool, includeStoreApps bool, includeDesktopApps bool) (*InstalledAppsResult, error)Parameters:
includeSystemApps (bool): Specifies whether to include system applications.
includeStoreApps (bool): Specifies whether to include store applications.
includeDesktopApps (bool): Specifies whether to include desktop applications.
Return values:
*InstalledAppsResult: A result object that contains the list of installed applications and the request ID.error: An error message is returned if the operation fails.
InstalledAppsResult struct:
type InstalledAppsResult struct {
RequestID string // A unique request identifier for debugging.
Apps []*InstalledApp // An array of installed applications.
}
type InstalledApp struct {
Name string // The application name.
Path string // The application path.
Version string // The application version.
Description string // The application description.
}Python
def get_installed_apps(
self,
include_system_apps: bool = True,
include_store_apps: bool = False,
include_desktop_apps: bool = True
) -> List[InstalledApp]:Parameters:
include_system_apps (bool, optional): Specifies whether to include system applications. The default is
True.include_store_apps (bool, optional): Specifies whether to include store applications. The default is
False.include_desktop_apps (bool, optional): Specifies whether to include desktop applications. The default is
True.
Return value:
List[InstalledApp]: A list of installed applications.
Exceptions:
ApplicationError: Raised if retrieving the list of installed applications fails.
TypeScript
async getInstalledApps(
includeSystemApps: boolean = true,
includeStoreApps: boolean = false,
includeDesktopApps: boolean = true
): Promise<InstalledApp[]>Parameters:
includeSystemApps (boolean, optional): Specifies whether to include system applications. The default is
true.includeStoreApps (boolean, optional): Specifies whether to include store applications. The default is
false.includeDesktopApps (boolean, optional): Specifies whether to include desktop applications. The default is
true.
Return value:
Promise<InstalledApp[]>: A Promise that resolves to a list of installed applications.
Exceptions:
APIError: Thrown if retrieving the list of installed applications fails.
StartApp - Start an application
Golang
func (am *ApplicationManager) StartApp(startCmd string, workDirectory string) (string, error)Parameters:
startCmd (string): The command to start the application.
workDirectory (string): The working directory of the application.
Return values:
string: A JSON string that contains the list of started processes.error: An error message is returned if the operation fails.
Python
def start_app(self, start_cmd: str, work_directory: str = "") -> List[Process]:Parameters:
start_cmd (str): The command to start the application.
work_directory (str, optional): The working directory of the application. The default is an empty string.
Return value:
List[Process]: A list of started processes.
Exceptions:
ApplicationError: Raised if starting the application fails.
TypeScript
async startApp(startCmd: string, workDirectory: string = ""): Promise<Process[]>Parameters:
startCmd (string): The command to start the application.
workDirectory (string, optional): The working directory of the application. The default is an empty string.
Return value:
Promise<Process[]>: A Promise that resolves to a list of started processes.
Exceptions:
APIError: Thrown if starting the application fails.
StopAppByPName - Stop an application by process name
Golang
func (am *ApplicationManager) StopAppByPName(pname string) (string, error)Parameters:
pname (string): The name of the process to stop.
Return values:
string: A message indicating that the operation succeeded.error: An error message is returned if the operation fails.
Python
def stop_app_by_pname(self, pname: str) -> bool:Parameters:
pname (str): The name of the process to stop.
Return value:
bool: ReturnsTrueif the operation succeeds. Otherwise, returnsFalse.
Exceptions:
ApplicationError: Raised if stopping the application fails.
TypeScript
async stopAppByPName(pname: string): Promise<boolean>Parameters:
pname (string): The name of the process to stop.
Return value:
Promise<boolean>: A Promise that resolves totrueif the operation succeeds, orfalseotherwise.
Exceptions:
APIError: Thrown if stopping the application fails.
StopAppByPID - Stop an application by process ID
Golang
func (am *ApplicationManager) StopAppByPID(pid int) (string, error)Parameters:
pid (int): The ID of the process to stop.
Return values:
string: A message indicating that the operation succeeded.error: An error message is returned if the operation fails.
Python
def stop_app_by_pid(self, pid: int) -> bool:Parameters:
pid (int): The ID of the process to stop.
Return value:
bool: ReturnsTrueif the operation succeeds. Otherwise, returnsFalse.
Exceptions:
ApplicationError: Raised if stopping the application fails.
TypeScript
async stopAppByPid(pid: number): Promise<boolean>Parameters:
pid (number): The ID of the process to stop.
Return value:
Promise<boolean>: A Promise that resolves totrueif the operation succeeds, orfalseotherwise.
Exceptions:
APIError: Thrown if stopping the application fails.
StopAppByCmd - Stop an application by stop command
Golang
func (am *ApplicationManager) StopAppByCmd(stopCmd string) (string, error)Parameters:
stopCmd (string): The command used to stop the application.
Return values:
string: A message indicating that the operation succeeded.error: An error message is returned if the operation fails.
Python
def stop_app_by_cmd(self, stop_cmd: str) -> bool:Parameters:
stop_cmd (str): The command used to stop the application.
Return value:
bool: ReturnsTrueif the operation succeeds. Otherwise, returnsFalse.
Exceptions:
ApplicationError: Raised if stopping the application fails.
TypeScript
async stopAppByCmd(stopCmd: string): Promise<boolean>Parameters:
stopCmd (string): The command used to stop the application.
Return value:
Promise<boolean>: A Promise that resolves totrueif the operation succeeds, orfalseotherwise.
Exceptions:
APIError: Thrown if stopping the application fails.
ListVisibleApps - List visible applications
Golang
func (am *ApplicationManager) ListVisibleApps() (*VisibleAppsResult, error)Return values:
*VisibleAppsResult: A result object that contains the list of visible processes and the request ID.error: An error message is returned if the operation fails.
VisibleAppsResult struct:
type VisibleAppsResult struct {
RequestID string // A unique request identifier for debugging.
Processes []*Process // An array of visible processes.
}
type Process struct {
PName string // The process name.
PID int // The process ID.
}Python
def list_visible_apps(self) -> List[Process]:Return value:
List[Process]: A list of visible processes.
Exceptions:
ApplicationError: Raised if listing visible applications fails.
TypeScript
async listVisibleApps(): Promise<Process[]>Return value:
Promise<Process[]>: A Promise that resolves to a list of visible processes.
Exceptions:
APIError: Thrown if listing visible applications fails.
Application management examples
Golang
package main
import (
"fmt"
"log"
)
func main() {
// Create a session
agentBay := agentbay.NewAgentBay("your-api-key")
sessionResult, err := agentBay.Create(nil)
if err != nil {
log.Fatal(err)
}
session := sessionResult.Session
// Get installed applications
appsResult, err := session.Application.GetInstalledApps(true, false, true)
if err != nil {
log.Printf("Error getting installed applications: %v", err)
} else {
for _, app := range appsResult.Apps {
fmt.Printf("Application: %s\n", app.Name)
}
}
// Start an application
processesResult, err := session.Application.StartApp("/usr/bin/google-chrome-stable", "")
if err != nil {
log.Printf("Error starting application: %v", err)
} else {
var processes []Process
_ = json.Unmarshal([]byte(processesResult), &processes)
for _, process := range processes {
fmt.Printf("Started process: %s (PID: %d)\n", process.PName, process.PID)
}
}
// List visible applications
visibleResult, err := session.Application.ListVisibleApps()
if err != nil {
log.Printf("Error listing visible applications: %v", err)
} else {
for _, app := range visibleResult.Processes {
fmt.Printf("Visible application: %s (PID: %d)\n", app.PName, app.PID)
}
}
// Stop the application by PID
if len(processes) > 0 {
success, err := session.Application.StopAppByPID(processes[0].PID)
if err != nil {
log.Printf("Error stopping application: %v", err)
} else {
fmt.Printf("Application stopped: %s\n", success)
}
}
}TypeScript
import { AgentBay } from 'wuying-agentbay-sdk';
// Initialize the SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });
// Get installed applications
async function getApplications() {
try {
const apps = await agentBay.application.getInstalledApps();
console.log(`Found ${apps.length} applications:`);
apps.forEach(app => {
console.log(`Name: ${app.name}, Start Cmd: ${app.start_cmd}`);
});
} catch (error) {
console.error('Error retrieving applications:', error);
}
}
// Start an application
async function startApplication() {
try {
const processes = await agentBay.application.startApp('/usr/bin/google-chrome-stable');
console.log(`Started ${processes.length} processes:`);
processes.forEach(p => {
console.log(`Process Name: ${p.pname}, PID: ${p.pid}`);
});
} catch (error) {
console.error('Error starting application:', error);
}
}
// Stop an application by PID
async function stopApplicationByPID() {
try {
const success = await agentBay.application.stopAppByPid(12345);
if (success) {
console.log('Application stopped successfully.');
} else {
console.log('Failed to stop application.');
}
} catch (error) {
console.error('Error stopping application:', error);
}
}
// List visible applications
async function listVisibleApplications() {
try {
const visibleApps = await agentBay.application.listVisibleApps();
console.log(`Visible applications: ${visibleApps.length}`);
visibleApps.forEach(app => {
console.log(`Name: ${app.pname}, PID: ${app.pid}`);
});
} catch (error) {
console.error('Error listing visible applications:', error);
}
}