如何编写AppleScript实现两个alert弹窗同时在不同位置弹出?
问题原因
- AppleScript 中的
display alert是同步阻塞指令,必须等待用户手动关闭当前弹窗后,才会继续执行后续代码,所以你连续写两条display alert语句,也只会按顺序依次弹出,无法实现同时展示的效果。
解决方法
将每个弹窗的执行逻辑封装为独立的后台异步进程,同时给每个弹窗指定不同的坐标位置即可实现预期效果,修改后的代码如下:
repeat display alert "Don't you dare close this button" buttons {"ok"} if the button returned of the result is "ok" then display notification with title ":))))))))" subtitle "muahahahahahhh" sound name "Frog" -- 第一个弹窗,坐标可自定义 do shell script "osascript -e 'display alert \"Don't you dare close this button\" buttons {\"ok\"} at {100, 100}' > /dev/null 2>&1 &" -- 第二个弹窗,坐标可自定义 do shell script "osascript -e 'display alert \"Don't you dare close this button\" buttons {\"ok\"} at {800, 600}' > /dev/null 2>&1 &" end if end repeat
代码说明
- 末尾的
> /dev/null 2>&1 &作用是让弹窗指令在后台异步执行,不会阻塞主进程运行,两条指令会同时触发 at {x, y}参数用于指定弹窗左上角的坐标位置,你可以根据自己的屏幕分辨率调整数值,修改两个弹窗的展示位置
内容的提问来源于stack exchange,提问作者ABYSS
相关产品推荐
相关产品推荐

