You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API编程远程控制OS Sierra及以上版本的Mac?

Remote Control macOS Sierra+ via APIs: Solutions for Your Use Cases

Absolutely, you can programmatically control macOS Sierra and later versions remotely using Apple's native tools and scripting frameworks. Below are targeted solutions for your specific requirements, with practical code examples and implementation details:

Core Tools & Frameworks to Use

  • AppleScript/JXA: macOS's built-in scripting languages that let you directly interact with system components and apps. Perfect for UI automation tasks.
  • SSH + osascript: Execute AppleScript commands remotely over SSH (enabled via System Preferences > Sharing > Remote Login).
  • Apple Remote Desktop (ARD): For more robust remote management (screen control, batch script execution), built into macOS (optional paid features for advanced use).

Use Case 1: Remote Browser Automation (Safari/Chrome)

Here’s an AppleScript example that launches Safari, navigates to a URL, fills in login credentials, and browses to additional pages. You can run this remotely via SSH.

-- Safari automation script
tell application "Safari"
    activate
    -- Open new window and navigate to login page
    make new document with properties {URL:"https://your-login-url.com"}
    delay 2 -- Wait for page to load
    
    -- Fill username and password (adjust element identifiers as needed for your site)
    tell current tab of window 1
        do JavaScript "document.getElementById('username').value = 'your-username';"
        do JavaScript "document.getElementById('password').value = 'your-password';"
        do JavaScript "document.getElementById('login-button').click();"
    end tell
    delay 3 -- Wait for login redirect
    
    -- Navigate to additional pages
    set URL of current tab of window 1 to "https://your-target-page-1.com"
    delay 2
    set URL of current tab of window 1 to "https://your-target-page-2.com"
end tell

To run this remotely over SSH:

ssh your-mac-username@remote-mac-ip 'osascript -e "tell application \"Safari\"" -e "activate" -e "make new document with properties {URL:\"https://your-login-url.com\"}" -e "delay 2" -e "tell current tab of window 1" -e "do JavaScript \"document.getElementById(\'username\').value = \'your-username\';\"" -e "do JavaScript \"document.getElementById(\'password\').value = \'your-password\';\"" -e "do JavaScript \"document.getElementById(\'login-button\').click();\"" -e "end tell" -e "delay 3" -e "set URL of current tab of window 1 to \"https://your-target-page-1.com\"" -e "end tell"'

Note: For Chrome, replace Safari with Google Chrome in the script. You’ll also need to enable JavaScript execution permissions in the browser, and grant "Accessibility" permissions to ssh or Terminal in System Preferences > Privacy & Security > Accessibility.


Use Case 2: Remote Keynote Automation

This AppleScript creates a new Keynote presentation with the White Theme, adds text to a slide, and saves it locally.

-- Keynote automation script
tell application "Keynote"
    activate
    -- Create new presentation with White Theme
    set newPres to make new document with properties {theme name:"White"}
    delay 1
    
    -- Add text to the first slide
    tell slide 1 of newPres
        set textItem to make new text item at end with properties {text:"Hello, Remote Control!"}
        set position of textItem to {200, 200} -- Adjust position as needed
        set size of text of textItem to 24
    end tell
    
    -- Save the presentation to local path
    save newPres in file "Macintosh HD:Users:your-username:Documents:Remote-Keynote.pages"
end tell

Run this remotely via SSH:

ssh your-mac-username@remote-mac-ip 'osascript -e "tell application \"Keynote\"" -e "activate" -e "set newPres to make new document with properties {theme name:\"White\"}" -e "delay 1" -e "tell slide 1 of newPres" -e "set textItem to make new text item at end with properties {text:\"Hello, Remote Control!\"}" -e "set position of textItem to {200, 200}" -e "set size of text of textItem to 24" -e "end tell" -e "save newPres in file \"Macintosh HD:Users:your-username:Documents:Remote-Keynote.pages\"" -e "end tell"'

Note: Ensure Keynote is installed on the remote Mac, and grant accessibility permissions to ssh/Terminal as mentioned earlier.


Key Considerations

  • Permissions: macOS requires explicit permission for apps/scripts to control other apps. Go to System Preferences > Privacy & Security > Accessibility and add ssh (located at /usr/bin/ssh) or Terminal to the allowed list.
  • Delay Timing: Adjust delay values based on the remote Mac’s performance and internet speed to ensure pages/apps load fully before executing next steps.
  • JXA Alternative: If you prefer JavaScript over AppleScript, you can write equivalent scripts using JXA (run via osascript -l JavaScript).

内容的提问来源于stack exchange,提问作者Bryan C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:17:47