Last active
August 10, 2025 14:35
-
-
Save tobemaster56/15ee009cca6de05c10ff7fae0592c3cb to your computer and use it in GitHub Desktop.
toggle-apple-id-zone script in raycast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/osascript | |
| # Required parameters: | |
| # @raycast.schemaVersion 1 | |
| # @raycast.title toggle apple id zone | |
| # @raycast.mode fullOutput | |
| # Optional parameters: | |
| # @raycast.icon | |
| # @raycast.argument1 { "type": "text", "placeholder": "zone: zh or us" } | |
| # @raycast.packageName toggle-apple-id-zone | |
| # Documentation: | |
| # @raycast.author tobemaster | |
| # @raycast.authorURL https://github.com/tobemaster56 | |
| on run argv | |
| # step 1:start ------------------ 捕获传参,设置菜单项的相关变量,设置对应的账号和密码 ------------------ | |
| # 将 Apple id 地区参数赋值给变量 zone, zh 代表国区, us 代表美区 | |
| set zone to item 1 of argv | |
| # 获取系统语言环境,赋值给变量 lang | |
| set lang to user locale of (get system info) | |
| # 不同语言环境下,菜单栏的 Store 和 Sign In/Sign Out 的名称不同,这里仅仅适配了中文和英文,其他语言环境暂不支持 | |
| 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 | |
| # 根据不同的区域,设置不同的账号和密码,需要注意,如果密码中有特殊字符,需要转义,比如双引号 " 需要写成 \" | |
| if zone is equal to "zh" then | |
| set account to "xxx" | |
| set pwd to "xxx" | |
| end if | |
| if zone is equal to "us" then | |
| set account to "xxx" | |
| set pwd to "xxx" | |
| end if | |
| # step 1:end | |
| # step 2:start ------------------ 打开激活应用程序 App Store,捕获应用窗口 ------------------ | |
| activate application "App Store" | |
| tell application "System Events" | |
| tell process "App Store" | |
| # 将 App Store 窗口置于最前 | |
| set frontmost to true | |
| # 获取 App Store 窗口,赋值给变量 appStoreWindow | |
| set appStoreWindow to window 1 | |
| # step 2:end | |
| # step 3:start ------------------ 点击菜单,唤出登录的小弹窗 ------------------ | |
| try | |
| # 先尝试,模拟点击菜单栏的 Store -> Sign In 如果成功,说明是未登陆状态,一步到位 | |
| click menu item signInMenuItem of menu 1 of menu bar item menuNameOfStore of menu bar 1 | |
| on error | |
| # 如果模拟点击菜单栏的 Store -> Sign In失败,说明是已登陆状态,没有 Sign In 菜单, | |
| # 再尝试模拟点击菜单栏的 Store -> Sign Out | |
| click menu item signOutMenuItem of menu 1 of menu bar item menuNameOfStore of menu bar 1 | |
| # 等待 Sign Out 完成,需要一定的时间,等待 2 秒 | |
| delay 2 | |
| # 再次模拟点击菜单栏的 Store -> Sign In | |
| click menu item signInMenuItem of menu 1 of menu bar item menuNameOfStore of menu bar 1 | |
| end try | |
| delay 2 | |
| # step 3:end | |
| # step 4:start ------------------ 模拟输入账户和密码 ------------------ | |
| # 先模拟点击弹窗中的账户输入框,输入账户名,然后回车,等待 2 秒 | |
| set value of text field 1 of sheet 1 of sheet 1 of appStoreWindow to account | |
| keystroke return | |
| delay 2 | |
| # 这里有个不起眼的坑,账户名变成了 text field 2,密码变成了 text field 1,所以,需要分别模拟点击账户输入框和密码输入框,然后输入账户名和密码,然后回车 | |
| set value of text field 2 of sheet 1 of sheet 1 of appStoreWindow to account | |
| set value of text field 1 of sheet 1 of sheet 1 of appStoreWindow to pwd | |
| keystroke return | |
| # step 4:end | |
| # step 5: ------------------ 如果开启 2 步验证,需要手动完成 ------------------ | |
| end tell | |
| end tell | |
| end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment