使用AppleScript调用Soundtrack API执行GraphQL突变遇阻求助
解决方案:通过AppleScript调用Soundtrack GraphQL API控制音量
问题核心
Soundtrack未提供AppleScript字典支持,无法通过标准AppleScript命令控制;但官方GraphQL API可通过Postman正常执行音量设置操作,AppleScript调用无效果通常是因为HTTP请求构造不完整。
排查要点
- 确认API端点URL是否正确(通常是本地地址,如
http://localhost:端口号/graphql) - 检查请求头部是否包含
Content-Type: application/json,这是GraphQL API的必要要求 - 核实GraphQL mutation语句和变量是否与Postman中成功的请求完全一致
- 确保请求包含Soundtrack API要求的认证信息(如token类头部,若有)
可行方案:直接用AppleScript调用curl执行API请求
无需控制Postman,直接在AppleScript中通过do shell script调用curl命令,复刻Postman的成功请求即可。
示例代码
假设你在Postman中使用的mutation如下:
mutation SetVolume($volume: Int!) { setVolume(volume: $volume) { success } }
变量为{"volume": 8},API端点为http://localhost:1234/graphql,对应的AppleScript代码:
try set apiEndpoint to "http://localhost:1234/graphql" set mutationQuery to "mutation SetVolume($volume: Int!) { setVolume(volume: $volume) { success } }" set variables to "{\"volume\": 8}" set curlCommand to "curl -X POST -H \"Content-Type: application/json\" -d '{\"query\":\"" & mutationQuery & "\",\"variables\":" & variables & "}' " & apiEndpoint do shell script curlCommand display dialog "音量设置请求已发送" on error errMsg display dialog "请求失败:" & errMsg end try
注意事项
- 将
apiEndpoint替换为Soundtrack实际的API地址 - 若API需要认证,在curl命令中添加对应的头部(如
-H "Authorization: Bearer 你的token") - 先在终端中测试curl命令是否能成功执行,再复制到AppleScript中(注意转义双引号)
不推荐控制Postman的原因
Postman并非为自动化控制设计,依赖应用处于前台/特定状态,容易出现不稳定情况,直接调用API是更可靠的方案。
内容的提问来源于stack exchange,提问作者swanny8777
相关产品推荐
相关产品推荐

