Search code examples
macosapplescriptbootsystem-startup

Run an Applescript application at launch


So I have an Applescript that's not very resource hungry so I always want it running. If I try to save it as an application, the options Show startup screen and Stay open after run handler can be ticked, but I don't think that's what I want. I am pretty sure that you can do this, since it's mentioned in this blogpost, quote:

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

I found no way of doing this online so every help would be appreciated.


Solution

  • Here is script to add your application to Startup Items programatically:

    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")
    

    NOTE: on the Catalina and Big Sur three properties of login item (kind, path and name) are read-only. It is Apple security changes. So, you should add login items using Users & Groups pane of System Preferences, manually. Requires authentication.