UNPKG

appium-mac2-driver

Version:

XCTest-based Appium driver for macOS apps automation

82 lines 3.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.macosExecAppleScript = macosExecAppleScript; const support_1 = require("appium/support"); const teen_process_1 = require("teen_process"); const node_path_1 = __importDefault(require("node:path")); const OSASCRIPT = 'osascript'; const APPLE_SCRIPT_FEATURE = 'apple_script'; /** * Executes the given AppleScript command or a whole script based on the * given options. Either of these options must be provided. If both are provided * then the `command` one gets the priority. * Note that AppleScript command cannot contain line breaks. Consider making it * to a script in such case. * Note that by default AppleScript engine blocks commands/scripts execution if your script * is trying to access some private entities, like cameras or the desktop screen * and no permissions to do it are given to the parent (for example, Appium or Terminal) * process in System Preferences -> Privacy list. * * @param script - A valid AppleScript to execute * @param language - Overrides the scripting language. Basically, sets * the value of `-l` command line argument of `osascript` tool. * If unset the AppleScript language is assumed. * @param command - A valid AppleScript as a single command (no line breaks) to execute * @param cwd - The path to an existing folder, which is going to be set as * the working directory for the command/script being executed. * @param timeout - The number of seconds to wait until a long-running command is finished. * An error is thrown if the command is still running after this timeout expires. * @returns The actual stdout of the given command/script * @throws {Error} If the exit code of the given command/script is not zero. * The actual stderr output is set to the error message value. */ async function macosExecAppleScript(script, language, command, cwd, timeout) { this.assertFeatureEnabled(APPLE_SCRIPT_FEATURE); if (!script && !command) { throw this.log.errorWithException('AppleScript script/command must not be empty'); } if (command && /\n/.test(command)) { throw this.log.errorWithException('AppleScript commands cannot contain line breaks'); } // 'command' has priority over 'script' const shouldRunScript = !command; const args = []; if (language) { args.push('-l', language); } let tmpRoot; try { if (shouldRunScript) { if (!script) { throw this.log.errorWithException('AppleScript script must be provided'); } tmpRoot = await support_1.tempDir.openDir(); const tmpScriptPath = node_path_1.default.resolve(tmpRoot, 'appium_script.scpt'); await support_1.fs.writeFile(tmpScriptPath, script, 'utf8'); args.push(tmpScriptPath); } else { if (!command) { throw this.log.errorWithException('AppleScript command must be provided'); } args.push('-e', command); } this.log.info(`Running ${OSASCRIPT} with arguments: ${support_1.util.quote(args)}`); try { const { stdout } = await (0, teen_process_1.exec)(OSASCRIPT, args, { cwd, timeout }); return stdout; } catch (e) { throw new Error(e.stderr || e.message); } } finally { if (tmpRoot) { await support_1.fs.rimraf(tmpRoot); } } } //# sourceMappingURL=applescript.js.map