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

如何修改AppleScript,仅启动24小时内未打开的自定义清理类应用?

实现AppleScript仅启动24小时内未打开的清理应用

要实现仅当目标应用在过去24小时内未被打开时才启动的功能,我们可以通过获取应用的最后使用时间并与当前时间对比来判断是否需要启动。以下是修改后的完整代码:

-- 检查应用是否需启动:过去24小时未打开则返回true
on shouldLaunchApp(appPath)
    -- 用mdls命令获取应用最后使用时间(依赖Spotlight索引)
    set lastUsedDateStr to do shell script "mdls -raw -name kMDItemLastUsedDate " & quoted form of appPath
    if lastUsedDateStr is "" then
        -- 从未被打开过,直接启动
        return true
    end if
    -- 转换为AppleScript日期对象
    set lastUsedDate to date lastUsedDateStr
    set twentyFourHoursAgo to (current date) - (24 * hours)
    -- 对比时间:最后使用时间早于24小时前则启动
    return lastUsedDate < twentyFourHoursAgo
end shouldLaunchApp

-- 定义需要检查的应用列表
set targetApps to {"clean-exports.app", "clean-queue.app", "clean-dropbox.app"}
set appsFolder to path to applications folder as text

tell application "Finder"
    repeat with appName in targetApps
        set fullAppPath to appsFolder & appName
        if shouldLaunchApp(fullAppPath) then
            open application file appName of folder "Applications" of startup disk
        end if
    end repeat
end tell

关键逻辑说明

  • shouldLaunchApp辅助函数:
    1. 通过mdls命令调用Spotlight获取应用的kMDItemLastUsedDate属性(即最后打开时间);
    2. 如果返回空字符串,说明应用从未被打开过,直接判定为需要启动;
    3. 将获取到的时间字符串转换为AppleScript可识别的日期对象,与当前时间减去24小时的时间点对比,若最后使用时间更早则返回true。
  • 循环处理应用:遍历目标应用列表,逐个调用判断函数,符合条件则启动。

注意事项

  • 该逻辑依赖Spotlight对应用的索引,如果应用未被Spotlight索引,mdls会返回空值,此时脚本会视为应用从未打开过并启动它;
  • 确保应用存放在默认的Applications文件夹中,若路径不同需修改代码中的路径部分。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 19:40:02