0

I have the following applescript bundled as .app (using Platypus) and starting on user logon (12.4 MB for each user)

#!/usr/bin/osascript

-- INICIO DAS FUNCOES EXTRAS
set app_path to path to current application
set app_name to get name of me
set myPath to path to me
tell application "Finder" to set myFolder to (container of myPath) as string
set commonScript to load script alias ((myFolder) & "FuncoesExtras.scpt")
-- FIM DAS FUNCOES EXTRAS

set WhiteList to {app_name, "App Store", "iTunes", "FecharProgramas", "Finder", "Mail"}

repeat

    tell application "System Events"
        repeat with this_app in (get processes whose background only is false and windows is {})
            set NomeDoApp to the name of this_app
            if NomeDoApp is not in WhiteList then
                try
                    tell NomeDoApp to quit
                    log_event("App " & NomeDoApp & " encerrado com sucesso", app_name) of commonScript
                on error
                    do shell script "killall " & quoted form of NomeDoApp
                    log_event("Forcando interrupcao do App " & NomeDoApp, app_name) of commonScript
                end try
            end if
        end repeat
    end tell

    tell application "System Events" to set myPID to (unix id of processes whose name is app_name)
    do shell script ("/usr/bin/renice 18 " & myPID)

    delay 60

end repeat

How can i make it a service, so it only run 1 instance and when the system starts, not at user logon?

Any other suggestions about my code?


EDIT

Here is my current (working) .plist located in /Library/LaunchAgents

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>EnableGlobbing</key>
    <false/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>ram.ramon.FecharProgramas</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>Program</key>
    <string>Applications/FecharProgramas.app/Contents/MacOS/FecharProgramas</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

When i move it to /Library/LaunchDaemons the app doesn't work anymore.

05/10/13 10:43:24,375 FecharProgramas[90]: 3891612: (connect_and_check) Untrusted apps are not allowed to connect to or launch Window Server before login.
05/10/13 10:43:24,376 FecharProgramas[90]: kCGErrorFailure: This user is not allowed access to the window system right now.
05/10/13 10:43:24,376 FecharProgramas[90]: _RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.
05/10/13 10:43:24,382 FecharProgramas[90]: kCGErrorInvalidConnection: CGSGetEventPort: Invalid connection

Do i need to trust my app? If so, how?

2
  • 1
    Try saving the code as a Applescript TEXT file i.e foo.applescript Making sure you follow my blog posts using the chmod command to make it executable. You most likely can also take out the repeat loop that runs ever 60 seconds and use <key>StartInterval</key> <integer>60</integer> in the Launch Agent – markhunte Oct 5 '13 at 14:50
  • @markhunte nice! much better than the repeat loop. – RASG Oct 5 '13 at 18:46
1

You can use launchd.plist or use a program like Lingon (which is easier to use to create a launchd plist).

0
1

Have a look at my blog post kill-one-application-if-another-is-not-running-applescript

This will give you an idea of how to write you script saved as a text file (.applescript) and make it into a shell script.

And show you how to create your own launch agent that will run every #n seconds.

Then there is the applescript-quit-or-launch-application-script-revised post that show a way of adapting the code to run for multiple app. ( which you do already any way)

0
1

Save a plist like this as /Library/LaunchAgents/some_label.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>some_label</string>
  <key>ProgramArguments</key>
  <array>
    <string>osascript</string>
    <string>/path/to/script.scpt</string>
  </array>
  <key>RunAtLoad</key>
  <true/> <!-- run at login -->
  <key>KeepAlive</key>
  <true/> <!-- run the program again if it terminates -->
</dict>
</plist>

You can load the plist with launchctl load /Library/LaunchAgents/some_label.plist or by logging out and back in. See man launchd and man launchd.plist for more information.

You might also replace the AppleScript with a shell command like this:

kill $(osascript -e 'tell application "System Events" to id of processes where frontmost is false and background only is false and windows is {} and name is not "App Store" and name is not "iTunes" and name is not "FecharProgramas" and name is not "Finder" and name is not "Mail"' | tr -d ,); renice 18 $(osascript -e 'tell application "System Events" to id of (process 1 where frontmost is true)')

2
  • 1
    Daemons are not allowed to connect to the window server. See the Daemons and Agents tech note. Can't you just use an agent (or run the program at login in the Aqua session type)? – Lri Oct 5 '13 at 16:17
  • that's what i'm currently doing. the problem is that i get 2 apps running when i switch users. my goal is to have only 1 (system wide, user independent) – RASG Oct 5 '13 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .