用 AppleScript 自动登录appleId,自动切换 appleId

2,139 阅读3分钟

我有一个中国区的 appleId,还有一个美国区的 appleId,时不时的得切换一下,切换起来,无非就是先退出,再登录,不是难事,就是鼠标多点几下。 后来,无意中发现了 Mac 上有App Store 登录器这种东西,原来是可以实现自动登录的,正好看过不少介绍 AppleScript 实现自动化的。就自己研究,捣鼓了一下。

因为,我每天都在用 Alfred,这是个 Mac 上的效率神器。最终的使用效果,就是激活 Alfred,输入 al zh, 会自动登录我的中国区账号,如果输入al us, 就会自动登录我的美区账号。

image.png

先想一想,如果不用程序来做,咱们自己来,可能得按照下面的步骤来(我的系统是 11,bigSur)

  1. 打开 App store
  2. 点击登录

image.png 3. 先输入 appleId,回车,或者点击登录,出现密码输入框

image.png 4. 输入密码,然后回车,或者点击登录,等待之后,就登录成功。

image.png

用 appleScript 写自动化,基本上也是在模拟这个操作过程,不过在第二步点击登录的时候,不是点击截图中的登录,而是点击菜单栏里的商店

image.png

image.png

下面直接贴代码吧,这个版本,已经是改过很多次之后的版本了,修复了一些 bug,不过仍然可能有 bug,用 appleScript 搞 GUI 编程,实在有不少坑。

(* 这个只是演示自动登录 *)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

(* Get the locale used by the current system *)
set lang to user locale of (get system info)


if lang is equal to "zh_CN" then
    set signInMenuItem to "登录"
    set signOutMenuItem to "退出登录"
    set menuNameOfStore to "商店"
else
    set signInMenuItem to "Sign In"
    set signOutMenuItem to "Sign Out"
    set menuNameOfStore to "store"
end if

(* 得改成真实的 appleId *)
set account to "your appleid account"
set pwd to "your appleid password"

tell application "App Store" to activate
tell application "System Events"
    tell process "App Store"
        set frontmost to true
        try
            click menu item signInMenuItem of menu menuNameOfStore of menu bar 1
        on error
            click menu item signOutMenuItem of menu menuNameOfStore of menu bar 1
            delay 2
            click menu item signInMenuItem of menu menuNameOfStore of menu bar 1
        end try
        delay 2
        set value of text field 1 of sheet 1 of sheet 1 of window "App Store" to account
        keystroke return
        delay 2
        set value of text field 2 of sheet 1 of sheet 1 of window "App Store" to account
        set value of text field 1 of sheet 1 of sheet 1 of window "App Store" to pwd
        keystroke return
	end tell
end tell

如何在 alfred 中切换 appleId,得在 alfred 中创建一个 workflow,比较简单就是一个关键字的触发器,和一个运行脚本的动作就好了。

image.png

关键字,大家可以自己自定义,我的就是 al,需要接受参数,title给个辅助的提示参数是 zh or us,因为切换账号也不是经常做的事情,会忘记参数是啥

image.png

动作就是执行脚本,

image.png

代码如下

on run argv
  set q to item 1 of argv
  set lang to user locale of (get system info)
  if lang is equal to "zh_CN"
    set signInMenuItem to "登录"
    set signOutMenuItem to "退出登录"
    set menuNameOfStore to "商店"
  else
    set signInMenuItem to "Sign In"
    set signOutMenuItem to "Sign Out"
    set menuNameOfStore to "store"
  end if


  if q is equal to "zh" then
    set account to "your apple account 1"
    set pwd to "xxx"
  end if
  if q is equal to "us" then
    set account to "your another account"
    set pwd to "xxxx"
  end if

  tell application "App Store" to activate
  tell application "System Events"
    tell process "App Store"
      set frontmost to true
      try
        click menu item signInMenuItem of menu menuNameOfStore of menu bar 1
      on error
        click menu item signOutMenuItem of menu menuNameOfStore of menu bar 1
        delay 2
        click menu item signInMenuItem of menu menuNameOfStore of menu bar 1
      end try
      delay 2
      set value of text field 1 of sheet 1 of sheet 1 of window "App Store"  to account
      keystroke return
      delay 2
      set value of text field 2 of sheet 1 of sheet 1 of window "App Store"  to account
      set value of text field 1 of sheet 1 of sheet 1 of window "App Store"  to pwd
      keystroke return
    end tell
  end tell
end run

当然如果你不用 alfred,也可以用别的可以执行 appleScript 的工具,比如,betterAndBetter,这个工具是国人开发的,也比较厉害。还有老牌的 keyborad maestro 这种工具。

以上脚本我已经收录在我的 github 仓库中,github.com/tobemaster5…. 大家有问题,可以去 github 提 issue,欢迎 star。