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?
<key>StartInterval</key> <integer>60</integer>
in the Launch Agent – markhunte Oct 5 '13 at 14:50