如何编写AppleScript获取Authy桌面端账号Token并拼接密码
Authy Desktop指定账号Token提取与密码拼接AppleScript实现方案
问题说明
需要编写AppleScript实现以下功能:
- 从Authy Desktop桌面端提取指定账号的动态验证Token
- 将提取到的Token与预设固定密码自动拼接
当前已定位到部分支持click操作的UI元素,但无法找到对应账号名称的标识元素。预期执行逻辑为:脚本自动点击目标账号条目、复制Token,在脚本内部完成Token与密码的拼接。
目前已使用的UI元素探查命令如下:
tell application "System Events" tell process "Authy Desktop" set visible to true return every UI element of front window end tell end tell
tell application "System Events" to return value of every attribute of window of application process "Authy Desktop"
实现步骤
前置权限配置
首先要给运行脚本的脚本编辑器/终端开启辅助功能权限,配置路径为「系统设置 > 隐私与安全性 > 辅助功能」,勾选对应应用的权限项,否则System Events无法读取、操作Authy的UI元素。
定位账号元素的正确探查方法
你之前使用的探查命令仅能获取窗口最外层的UI元素,没有递归遍历子元素层级,所以找不到账号名称对应的条目。使用下面的递归探查脚本,可以导出整个窗口的完整UI结构到剪贴板,搜索目标账号名就能快速定位对应元素:
-- 递归导出Authy所有UI元素结构 tell application "System Events" tell process "Authy Desktop" set frontmost to true set uiLog to "" my traverseUI(entire contents of front window, 0, uiLog) set the clipboard to uiLog return "完整UI结构已复制到剪贴板,可直接粘贴到文本编辑器搜索" end tell end tell on traverseUI(elementList, depth, logContent) set indentText to "" repeat depth times set indentText to indentText & " " end repeat repeat with singleElem in elementList try set elemRole to role of singleElem set elemName to name of singleElem set elemDesc to description of singleElem set elemValue to value of singleElem set logContent to logContent & indentText & "角色:" & elemRole & " | 名称:" & elemName & " | 描述:" & elemDesc & " | 值:" & elemValue & linefeed my traverseUI(UI elements of singleElem, depth + 1, logContent) end try end repeat end traverseUI
运行脚本后打开任意文本编辑器粘贴,搜索你的目标账号名称,就能找到对应元素的角色、层级位置。
完整功能脚本
定位到账号元素后,使用以下脚本即可实现自动点击、提取Token、拼接密码的全流程:
-- 替换为你的固定密码 set fixedPwd to "你的固定密码内容" -- 替换为目标账号的显示名称,例如「公司VPN」「GitHub」 set targetAccount to "你的目标账号名称" tell application "Authy Desktop" to activate delay 0.5 tell application "System Events" tell process "Authy Desktop" set frontmost to true delay 0.3 -- 遍历定位目标账号条目并点击 set allUIElem to entire contents of front window repeat with elem in allUIElem try if name of elem is targetAccount and role of elem is "static text" then -- 如果直接点击文本无响应,替换为点击上层容器:click first parent of elem whose role is "group" click elem exit repeat end if end try end repeat delay 0.8 -- 提取并清洗Token(Authy默认显示格式为3位+空格+3位) set tokenVal to "" repeat with elem in allUIElem try if description of elem contains "token" and value of elem is not missing value then set rawToken to value of elem as text set AppleScript's text item delimiters to " " set tokenVal to text items of rawToken as text set AppleScript's text item delimiters to "" exit repeat end if end try end repeat end tell end tell -- 拼接密码与Token,如需调整顺序直接修改下行参数顺序即可 set finalStr to fixedPwd & tokenVal -- 拼接结果写入剪贴板,可直接粘贴使用 set the clipboard to finalStr return "拼接完成,结果已复制到剪贴板"
常见问题处理
- 点击账号无响应:查看导出的UI结构日志,找到账号文本对应的上层可点击容器,通常为
group或button角色的父元素,替换脚本中的点击目标即可 - 无法提取到Token值:部分版本Authy的Token会拆分为两个独立的静态文本元素分别显示前后3位,分别取值后拼接即可
- 运行报权限错误:重新检查辅助功能权限配置,确保Authy Desktop、运行脚本的应用均已开启权限
内容的提问来源于stack exchange,提问作者Rodrigo Rios
相关产品推荐
相关产品推荐

