iOS 12 workflow 配合 AppleScript 实现 Mac 自动初始化

在 iOS 12 发布之后,最好玩的功能莫属集成到系统的捷径,也就是以前的 workflow 了,配合 workflow 能够完成一连串的操做,在这里主要演示下在手机上经过 Siri 语音控制 Mac 完成办公环境的初始化。shell

注意

  • Mac 的 ip 应该固定,若是不固定的话,没法实现语音控制,但后边的脚本能够在 Mac 本机手动执行
  • 执行过程当中,须要部分辅助功能的权限,刚开始几回的执行可能会失败。

2018.11.08 更新

最近又研究了下,发现能够更进一步,配合语音完成 Mac 的解锁并自动登陆微信,解锁 Mac 须要在 App Store 购买 Unlox(固然若是你有 Apple Watch 的话当我没说)服务器

AppleScript:

  1. 打开 Mac 自带的脚本编辑器,输入一下代码:微信

    -- active app
    on active(appName, isfull)
        if application appName is not running then
            tell application appName
                activate
            end tell
            if isfull is equal to 1 then
                fullScreen()
            end if
            if appName is equal to "iTerm" then
                initIterm()
            end if
            if appName is equal to "WeChat" then
             weChatLogin()
         end if
        end if
    end active
    
    -- init iTerm
    on initIterm()
        tell application "iTerm"
            tell current window
                create tab with default profile
                tell current tab
                    tell application "System Events"
                        keystroke "ssh user@ip"
                        keystroke return
                        delay 2
                        keystroke "password"
                        keystroke return
                    end tell
                end tell
            end tell
        end tell
    end initIterm
    
    -- full screen
    on fullScreen()
        delay 1
        tell application "System Events"
            keystroke "f" using {control down, command down}
        end tell
    end fullScreen
    
    -- init samba connect
    on initSamba()
        tell application "Finder"
            open location "smb://user:password@ip"
        end tell
    end initSamba
    
     -- WeChat Login
     on weChatLogin()
         tell application "System Events" to tell process "WeChat"
             set frontmost to true
             click at {640, 440}
         end tell
     end WeChatLogin
    
    -- main
    initSamba()
    active("iTerm", 0)
    active("Mail", 0)
    active("DingTalk", 0)
    active("Visual Studio Code", 1)
    active("Google Chrome", 1)
    active("WeChat", 0)

    说明:app

    • initSamba handler 挂载开发机的代码目录
    • active handler 有两个参数,第一个参数是要打开的 app,第二个参数是是否要全屏
    • 对于 iTerm 有特殊操做,须要自动新建 tab 并登录到开发机
    • fullScreen handler 有 1s 的延迟,是由于在打开某些 app 的时候,app 自己还在启动中,是不会响应全屏操做的
    • initIterm handler 有 2s 的延迟,是由于须要时间来链接远程服务器,这个时间能够根据实际状况调整
    • 能够去掉不须要的 app 和功能,添加本身的操做
    • Mac 平台的微信不是原生实现,并且也没找到对应的 script 字典,因此登陆的时候得模拟点击登陆按钮,weChatLogin handler 中的 {640, 440} 是微信在个人 Mac 上的登陆按钮的坐标,在不一样的屏幕上可能不太同样
  2. 保存以上代码到 /Users/username/Desktop/init.scpt,此时能够直接运行这个脚原本验证程序是否 OK,运行过程当中可能回须要容许一些辅助功能的权限

手机:

  1. 手机上购买安装 unlox ,在完成该 App 和 Mac 的配对后,进入 Settings -> Create URL schemes -> 选择本身的 Mac -> 选择 Unlock -> 选择 Copy URL schemes to clipboard,此时解锁的 scheme 会复制到剪切板
  2. 打开捷径 App,选择添加新的捷径,在搜索框中输入 url, 选择第一个,把操做一复制的 schemes 添加到 url 下的输入框
  3. 再次搜索 url, 选择 打开url
  4. 在搜索框中输入 ssh,选择搜索出来的选项
  5. 在对应的输入框中输入 Mac 的 ip,端口,用户名,密码
  6. 在密码下边的输入框中输入ssh

    osascript /Users/username/Desktop/init.scpt
  7. 点击搜索框选择应用 -> 打开应用 -> 点击右侧的选择按钮选择微信,这一步是为了让手机自动跳转到微信,咱们只须要在手机上容许微信登陆便可
  8. 添加捷径到 Siri 的语音控制中
  9. 用语音运行测试,期间可能弹出错误,要求 Mac 上容许辅助功能权限,容许便可

大功告成!编辑器

相关文章
相关标签/搜索