applescript_execute
Automate macOS tasks by executing AppleScript code to control applications and system functions.
Instructions
Run AppleScript (macOS)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code_snippet | Yes | ||
| timeout | No |
Implementation Reference
- src/tools/applescript_tools.js:21-104 (handler)Core handler function executeAppleScript() that writes AppleScript code to a temp file and executes it via osascript. Handles macOS check, timeout, temp file cleanup, and error cases.
async function executeAppleScript(scriptCode, timeout = 60) { try { if (!scriptCode || typeof scriptCode !== 'string') { return { success: false, message: 'No AppleScript code provided' }; } // Check if we're on macOS if (os.platform() !== 'darwin') { return { success: false, message: 'AppleScript is only available on macOS' }; } logger.debug(`Executing AppleScript with timeout of ${timeout} seconds`); // Create a temporary file for the script const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'applescript-')); const scriptPath = path.join(tempDir, 'script.scpt'); // Write the script to the temporary file await fs.writeFile(scriptPath, scriptCode, 'utf8'); try { // Execute the AppleScript using osascript const { stdout, stderr } = await execAsync( `osascript "${scriptPath}"`, { timeout: timeout * 1000, maxBuffer: 10 * 1024 * 1024 // 10MB buffer } ); // Clean up the temporary file await fs.rm(tempDir, { recursive: true, force: true }); if (stderr && stderr.trim()) { logger.warn(`AppleScript stderr: ${stderr}`); } return { success: true, output: stdout.trim(), error: stderr ? stderr.trim() : null, executionTime: new Date().toISOString() }; } catch (error) { // Clean up the temporary file on error await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {}); if (error.killed && error.signal === 'SIGTERM') { return { success: false, message: `AppleScript execution timed out after ${timeout} seconds`, error: error.message }; } // Parse AppleScript errors const errorMessage = error.message || error.toString(); const errorOutput = error.stderr || ''; return { success: false, message: 'AppleScript execution failed', error: errorMessage, details: errorOutput, code: error.code }; } } catch (error) { logger.error(`Error executing AppleScript: ${error.message}`); return { success: false, message: `Error executing AppleScript: ${error.message}`, error: error.stack }; } } - src/mcp/server.js:300-301 (registration)Registration/call dispatch: case 'applescript_execute' in the tools/call handler that invokes applescriptTools.executeAppleScript() with args.code_snippet and timeout.
// AppleScript (macOS only) case 'applescript_execute': data = await applescriptTools.executeAppleScript(args.code_snippet, args.timeout || 60); break; - src/mcp/server.js:124-124 (schema)Input schema definition for 'applescript_execute' tool: requires code_snippet (string), optional timeout (number).
{ name:'applescript_execute', description:'Run AppleScript (macOS)', inputSchema:{ type:'object', properties:{ code_snippet:{type:'string'}, timeout:{type:'number'} }, required:['code_snippet'] } } - Module exports: executeAppleScript, executeTemplate (pre-built templates like getNotes, createNote, etc.), getAvailableTemplates, and templates object.
module.exports = { executeAppleScript, executeTemplate, getAvailableTemplates, templates