使用 AppleScript 执行 API GraphQL 突变

问题描述 投票:0回答:1

我正在尝试使用AppleScript来控制一个名为Soundtrack的应用程序(类似于Spotify,但用于企业帐户)。

通常,我通过 AppleScript 控制 Spotify,AppleScript 是使用名为 ProPresenter(一种演示软件)的应用程序通过 MIDI 音符激活的。

我无法让 Soundtrack 响应任何 AppleScript(激活除外),所以我认为这是因为应用程序开发人员尚未为 Soundtrack 开发 AppleScript 字典?

我确实可以访问 Soundtrack API,并设法使用 Postman 生成一些脚本来控制应用程序。

我正在尝试将上面引用的脚本实现为一些可以由 ProPresenter 触发的 AppleScript。

我的背景不是脚本编写; Google 过去提供了很多帮助,这次我使用 ChatGPT 来帮助找到解决方案,但我遇到了困难。我有一个 AppleScript 看起来应该可以工作,但我需要一些帮助来确定我是否做错了什么,或者试图做一些不可能的事情。

我当前尝试使用的AppleScript如下:

-- Replace "your_sound_zone_id_here" with your actual Sound Zone ID
set soundZoneId to "your_sound_zone_id_here"

-- Replace "your_api_key_here" with your actual API Key
set apiKey to "your_api_key_here"

-- GraphQL query to retrieve sound zone information with variables
set soundZoneQuery to "mutation SetVolume($soundZoneId: ID!) {
  setVolume(input: { soundZone: $soundZoneId, volume: 8 }) {
    soundZone
    status
    volume
  }
}"

-- Build the GraphQL request
set graphQLRequest to "json='{\"query\": \"" & soundZoneQuery & "\"}'"

-- Execute the GraphQL request using osascript with Basic Auth
do shell script "curl -X POST 'https://api.soundtrackyourbrand.com/v2' -H 'Authorization: Basic " & apiKey & "' -H 'Content-Type: application/json' -d @- <<< " & quoted form of graphQLRequest

在 Postman 中使用相同的 GraphQL 查询/突变会产生将应用程序音量设置为 8 的预期结果。但是,使用此 AppleScript 不会产生任何结果,只是一组空引号,并且 Soundtrack 应用程序中没有任何更改。

如果我试图做一些 Soundtrack API 不可能或不允许的事情,我的另一个选择是让 Postman 在后台运行并创建一个 AppleScript 来控制它,但我还没有做到这一点。

任何帮助将不胜感激。

graphql applescript
1个回答
0
投票

您需要在查询定义中正确引用和转义 soundZoneId 变量。

-- Replace "your_sound_zone_id_here" with your actual Sound Zone ID
set soundZoneId to "your_sound_zone_id_here"

-- Replace "your_api_key_here" with your actual API Key
set apiKey to "your_api_key_here"

-- GraphQL query to retrieve sound zone information with variables
set soundZoneQuery to "mutation SetVolume($soundZoneId: ID = \\\"" & $soundZoneId & "\\\") {
  setVolume(input: { soundZone: $soundZoneId, volume: 8 }) {
    soundZone
    status
    volume
  }
}"

-- Build the GraphQL request
set graphQLRequest to "{\"query\": \"" & soundZoneQuery & "\"}"

-- Execute the GraphQL request using osascript with Basic Auth
do shell script "curl -X POST 'https://api.soundtrackyourbrand.com/v2' -H 'Authorization: Basic " & apiKey & "' -H 'Content-Type: application/json' -d @- <<< " & quoted form of graphQLRequest
© www.soinside.com 2019 - 2024. All rights reserved.