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

如何让运行无限循环的AppleScript应用点击图标时弹出对话框

问题根因

你当前的代码存在两个核心问题导致无法响应二次点击:

  • 无限循环没有加任何延时,完全占满主线程资源,系统没有空闲时间处理App的交互事件
  • 没有实现AppleScript专属的on reopen事件处理器,这个处理器就是用来响应「App已经处于运行状态时,用户再次点击图标」的场景的
修复后可直接运行的代码
global isSoundEnabled, isNotifEnabled

on run
    -- 初始化状态
    set isSoundEnabled to true
    set isNotifEnabled to true
    -- 首次启动弹一次设置框
    reopen()
    -- 启动计时循环
    timeLoop()
end run

-- 响应二次点击图标的事件
on reopen
    set theDialogText to "时间播报设置,当前时间:" & (current date) & "."
    display dialog theDialogText buttons {"暂停", "开启通知", "关闭通知"} default button "开启通知" cancel button "暂停" with icon caution
    set selectBtn to button returned of result
    if selectBtn = "开启通知" then
        set isSoundEnabled to true
        set isNotifEnabled to true
    else if selectBtn = "关闭通知" then
        set isSoundEnabled to false
        set isNotifEnabled to false
    end if
end reopen

on timeLoop()
    repeat while true
        set min to getMin() as integer
        -- 每5分钟触发一次播报
        if (min mod 5) = 0 then
            set timee to getTimeInHoursAndMinutes()
            if isNotifEnabled then
                display notification timee
            end if
            if isSoundEnabled then
                say timee
            end if
            -- 触发后延时60秒,避免同一分钟内重复播报
            delay 60
        end if
        -- 核心:加1秒延时,释放主线程资源响应交互,可根据需求调整时长
        delay 1
    end repeat
end timeLoop

-- 你可自行替换为自己的实现
on getMin()
    return minutes of (current date)
end getMin

on getTimeInHoursAndMinutes()
    set curDate to current date
    return (hours of curDate as string) & "点" & (minutes of curDate as string) & "分"
end getTimeInHoursAndMinutes
注意事项
  • 导出App时请确认勾选「运行处理程序后保持打开」(stay open after run handler)选项,避免App执行完初始化逻辑后自动退出
  • 循环内的delay 1是必须的,调整为更长的时长也可,只要不为0就不会占满CPU影响交互
  • 全局开关会实时生效,不需要重启App就能随时切换播报状态,满足7×24运行要求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 08:06:00