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

AppleScript进度条改造:Mac杀毒软件切换场景添加重启按钮

解决方案

方案1:复用现有进度条组件,修改Stop按钮触发逻辑(实现成本最低,无需调整UI样式)

AppleScript 原生进度条的默认Stop按钮无法直接修改文本,但我们可以通过捕获点击Stop按钮抛出的user canceled错误,将原有取消逻辑替换为立即重启逻辑,同时可以在进度条描述里补充提示说明该按钮作用,适配管理层用户的使用认知。

修改后的完整代码如下:

-- 进度条 - 重启计时器
use scripting additions

try
    progress_timer("03:00:00", "IT系统配置更新提示") -- 传入倒计时时长和窗口标题
on error number -128 -- 捕获用户点击Stop按钮的取消事件
    -- 点击后立即执行重启
    tell application "System Events"
        restart
    end tell
end try
return result

------------------------------------------
-- 子程序按字母顺序排序 --
------------------------------------------

-- 将HH:MM:SS格式的时间转换为秒数
on getTimeConversion(HMS_Time)
    set HMSlist to the words of HMS_Time
    set theHours to item 1 of HMSlist
    set theMinutes to item 2 of HMSlist
    set theSeconds to item 3 of HMSlist
    return round (theHours * (60 ^ 2) + theMinutes * 60 + theSeconds)
end getTimeConversion

-- 显示带倒计时的进度条
on progress_timer(HMS_Time, timerLabel)
    set theTimeSec to getTimeConversion(HMS_Time)
    set progress total steps to theTimeSec
    set progress completed steps to 0
    
    set startTime to (current date)
    repeat with i from 0 to theTimeSec
        set HMS_ToGo to TimetoText(theTimeSec - i)
        set progress completed steps to 0
        set progress description to "
IT部门需要为您的Mac完成杀毒软件切换配置更新。

本次更新需要重启设备才能生效,您可以选择:
1. 随时点击下方「Stop」按钮立即重启
2. 等待倒计时结束后设备自动重启

剩余倒计时:" & HMS_ToGo
        
        set progress additional description to ""
        set progress completed steps to i
        set elapsedTime to (current date) - startTime
        set lagAdjust to elapsedTime - i
        delay 1 - lagAdjust
    end repeat
    -- 倒计时结束自动执行重启
    tell application "System Events"
        restart
    end tell
    return null
end progress_timer

-- 将秒数转换为HH:MM:SS格式文本
on TimetoText(theTime)
    if (class of theTime) as text is "integer" then
        set TimeString to 1000000 + 10000 * (theTime mod days div hours)
        set TimeString to TimeString + 100 * (theTime mod hours div minutes)
        set TimeString to (TimeString + (theTime mod minutes)) as text
        tell TimeString to set theTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
    end if
    return theTime
end TimetoText

方案2:自定义按钮文本为「立即重启」(体验更友好)

如果需要修改按钮文本消除用户认知歧义,可以通过AppleScriptObjC调用Cocoa组件自定义进度窗口,完全自定义按钮文本、样式和交互逻辑:

  • 用NSWindow创建自定义浮动窗口,层级高于普通窗口避免被遮挡
  • 添加NSProgressIndicator控件展示倒计时进度
  • 添加自定义NSButton按钮,标题设为「立即重启」,点击后触发重启逻辑
  • 后台运行倒计时线程,实时更新进度和剩余时间提示

补充注意事项

  • 部署时建议通过企业MDM工具下发脚本,授予脚本必要的执行权限,避免系统权限拦截
  • 可根据需求在重启前新增二次确认弹窗,避免用户误触
  • 未登录场景的自动重启可以通过launchd守护进程实现,检测到用户10分钟以上未登录时直接执行重启命令

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 04:42:01