0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
gurkensaas
  • 793
  • 1
  • 5
  • 29

2 Answers2

0

Using on idle handler and saving the script as stay-open application is usual (and correct) approach. But, as you say, you want launch script as is, and keep it running. You can wrap your script with infinite loop:

repeat
-- your script here
end repeat

Note: you should put some condition in your script to quit script when need.

Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • I know about infinite loops, but is there any way to do this without having to run the application every time my machine boots up? – gurkensaas Jun 05 '21 at 23:25
  • Save your script (with on idle handler) as stay-open application. Then add it to StartupItems folder of Library folder of root directory. – Robert Kniazidis Jun 05 '21 at 23:47
0

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.

Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • 1
    Why 'as string'? Won't that allow for false positives? – Mockman Jun 06 '21 at 07:16
  • Yes, 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. – Robert Kniazidis Jun 06 '21 at 15:28
  • Replying to @Mockman: why as string? Because **name of every login item** is **list** with 2 and more items. So, the statement **if appName is not in allLoginItems** will give wrong comparsion without **as string coercion**. But we can omit **as string** if we write if staement other way: **if {appName} is not in allLoginItems...** – Robert Kniazidis Jun 06 '21 at 15:54
  • 1
    @RobertKniazidis 'As string' will concatenate all login item names into a single string e.g. "iTunesHelperEyeTV Helper". An app named 'iTunes' would match because you are asking if the one string is contained within the other. If the test is 'as list', then the list items will be compared individually and 'itunes' would not match. Of course, I am on Sierra so perhaps the result of getting login items has changed. – Mockman Jun 06 '21 at 15:57
  • 1
    You are right, it is better using {appName} and no as string coercion. I updated the answer. – Robert Kniazidis Jun 06 '21 at 16:02