在启动时运行 Applescript 应用程序

时间:2021-06-05 22:46:40

标签: macos applescript boot system-startup

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

<块引用>

我发现自己在写一段 Applescript 以...每当计算机启动时。

我在网上找不到这样做的方法,因此我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

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

repeat
-- your script here
end repeat

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

答案 1 :(得分:0)

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

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 上,登录项的三个属性(种类、路径和名称)是只读的。这是苹果的安全变化。因此,您应该使用系统偏好设置的用户和组面板手动添加登录项。需要身份验证。

相关问题