You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

注意事项

  1. 将apiEndpoint替换为Soundtrack实际的API地址
  2. 若API需要认证,在curl命令中添加对应的头部(如-H "Authorization: Bearer 你的token")
  3. 先在终端中测试curl命令是否能成功执行,再复制到AppleScript中(注意转义双引号)

不推荐控制Postman的原因

Postman并非为自动化控制设计,依赖应用处于前台/特定状态,容易出现不稳定情况,直接调用API是更可靠的方案。

内容的提问来源于stack exchange,提问作者swanny8777

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 12:48:25