如何在AppleScript中无弹窗短暂显示图形作为Webhook调用反馈?
问题描述
我有两个保存为应用的AppleScript,通过循环调用Webhook控制Bose Series 12音响音量——每个脚本会弹出对话框询问音量增减刻度数,之后执行循环调用。
背景信息:
- 在Raspberry Pi上编写了
pi_bose程序,通过GPIO端口连接的导线天线在28MHz频段向音响发送指令 - Node-RED接收Webhook调用并运行该脚本
目前存在的问题/故障可能:
- 天线因Pi被碰撞松动
- Node-RED未运行
- 程序存在小内存泄漏(使用约6个月后出现问题)
- 背景干扰导致部分传输失败(或可更换更长天线解决)
- 有时音响播放内容音量过低,难以察觉变化
- 有时Webhook调用响应缓慢,音量变化需20-30秒才显现
需求:
虽可在Pi端直接循环执行,但希望在Mac端查看执行进度,需要非音频的反馈提示,比如AppleScript应用图标上的红点,或屏幕角落短暂显示的图形,告知每次Webhook调用已执行。想知道如何实现?AppleScript能否不显示对话框而展示这类提示?
解决方案
一、给AppleScript应用添加图标红点(Dock徽章)
AppleScript可以通过调用System Events给Dock中的应用图标添加数字徽章,每次调用Webhook后更新计数,循环结束后清除,全程无需额外弹窗。
示例代码:
-- 获取当前应用名称 set appName to name of current application -- 询问音量增减刻度 set stepCount to text returned of (display dialog "请输入音量增减刻度数:" default answer "10") -- 初始化徽章计数 set badgeNumber to 0 repeat with i from 1 to stepCount as integer -- 调用Webhook(替换为你的实际Webhook地址) do shell script "curl -s 'https://your-webhook-url-here'" -- 更新Dock徽章计数 set badgeNumber to badgeNumber + 1 tell application "System Events" set badge number of application process appName to badgeNumber end tell -- 自定义调用间隔(根据需求调整) delay 1 end repeat -- 循环结束后清除徽章 tell application "System Events" set badge number of application process appName to 0 end tell
二、屏幕角落显示临时提示
如果需要更醒目的反馈,可以通过以下两种方式实现无交互的临时提示:
方式1:通知中心临时提示
每次调用Webhook后发送一条系统通知,几秒后自动消失,不会打断当前操作:
set stepCount to text returned of (display dialog "请输入音量增减刻度数:" default answer "10") repeat with i from 1 to stepCount as integer -- 调用Webhook do shell script "curl -s 'https://your-webhook-url-here'" -- 发送静音通知(可去掉sound name参数添加提示音) display notification "已完成第" & i & "次音量调整调用" with title "Bose音量控制" delay 1 end repeat
方式2:屏幕临时图形提示
生成一个简单的红色方块图形,在屏幕中心短暂显示后自动关闭,完全无需用户交互:
-- 创建临时红色提示图 set tempImagePath to (path to temporary items folder as text) & "volume_tick.png" tell application "Image Events" launch set img to make new image with properties {size:{20, 20}} fill img with color {65535, 0, 0} -- 红色RGB值 save img in file tempImagePath as PNG close img end tell set stepCount to text returned of (display dialog "请输入音量增减刻度数:" default answer "10") repeat with i from 1 to stepCount as integer -- 调用Webhook do shell script "curl -s 'https://your-webhook-url-here'" -- 显示图形提示,2秒后自动关闭 tell application "System Events" display dialog "" with icon file tempImagePath without buttons giving up after 2 end tell delay 1 end repeat -- 清理临时图片 do shell script "rm '" & tempImagePath & "'"
补充优化建议
- 可以在Webhook调用后检查返回状态码,仅在调用成功时更新提示,避免误反馈:
set curlResult to do shell script "curl -s -w '%{http_code}' 'https://your-webhook-url-here'" set httpCode to text -3 thru -1 of curlResult if httpCode is "200" then -- 执行提示更新逻辑 end if - 针对Node-RED未运行、天线松动等故障,可以让Webhook返回状态信息,AppleScript根据返回值触发错误提示(比如显示红色警告通知)。
内容的提问来源于stack exchange,提问作者hepcat72
相关产品推荐
相关产品推荐

