运行 启动时的 Applescript 应用程序

Run an Applescript application at launch

所以我有一个不太占用资源的 Applescript,所以我一直想要它 运行。如果我尝试将其另存为应用程序,可以勾选 Show startup screenStay open after run handler 选项,但我认为这不是我想要的。我很确定你可以做到这一点,因为它在 this blogpost 中提到过,引用:

I found myself writing a piece of applescript to ... whenever the computer booted up.

我找不到在线执行此操作的方法,因此我们将不胜感激。

使用on idle 处理程序并将脚本保存为保持打开的应用程序是通常(且正确)的方法。但是,正如您所说,您希望按原样启动脚本,并保留它 运行。您可以使用 无限循环 :

包装脚本
repeat
-- your script here
end repeat

注意:您应该在脚本中添加一些条件,以便在需要时退出脚本。

这是以编程方式将您的应用程序添加到启动项的脚本:

on addAppToLoginItemsAfterCheckingInOnes(appName)
    
    try
        set appPath to (path to application appName)
    on error
        display notification "Application with this name NOT founded" sound name "Frog"
        return
    end try
    
    tell application "System Events"
        try
            set allLoginItems to name of every login item
        on error
            set allLoginItems to {}
        end try
        if {appName} is not in allLoginItems then tell application "System Events" to make login item at end with properties {path:appPath, hidden:false}
    end tell
    
end addAppToLoginItemsAfterCheckingInOnes

my addAppToLoginItemsAfterCheckingInOnes("BBEdit")

注意: 在 Catalina 和 Big Sur 上,登录项的三个属性(种类、路径和名称)是只读的。这是 Apple 安全更改。因此,您应该使用系统偏好设置的用户和组窗格手动添加登录项。需要身份验证。