Install / uninstall a launchd agent to execute scheduled scripts


(*
		This script provides a simple way to install/uninstall a launchd agent to execute scheduled scripts.
		
		In this basic form the AppleScript (triggeredScriptName) will be executed
		every day at the time specified in the properties hr/mn.
		
		For futher schedule parameters read the man page of launchd.plist
		
		Thanks to Craig Smith for his launchd article on macscripter.net, which was extremely helpful
		
		Notes: Activating the agent can take some time. In any case a status message will be displayed.
		launchd works only on Tiger [edit march 2009: and later], the parameter StartCalendarIntervall doesn't work properly < 10.4.4 
			
		
*)

-------------------------
-- User parameters --
-------------------------

property pListLabel : "com.myJob.agent" -- name of the launchd agent (without .plist extension)
property triggeredScriptName : "myJob.scpt" -- name of the script to execute
property hr : "18" -- execute script every day at hr:mn (24 hr mode)
property mn : "0"

set triggeredScriptLocation to path to scripts folder as Unicode text -- the folder (~/Library/Scripts/) to put in the script to execute

-------

set userLibraryPath to (path to home folder as Unicode text) & "Library:"
try
	set LaunchAgentsFolder to (userLibraryPath & "LaunchAgents:") as alias
on error
	tell application "Finder" to set LaunchAgentsFolder to make new folder at userLibraryPath with properties {name:"LaunchAgents"}
end try
set pListfile to ((LaunchAgentsFolder as Unicode text) & pListLabel & ".plist")

set PLIST_File to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
	<key>Label</key>
	<string>" & pListLabel & "</string>
	<key>LowPriorityIO</key>
	<true/>
	<key>Program</key>
	<string>/usr/bin/osascript</string>
	<key>ProgramArguments</key>
	<array>
		<string>osascript</string>
		<string>" & POSIX path of triggeredScriptLocation & triggeredScriptName & "</string>
	</array>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>" & hr & "</integer>
		<key>Minute</key>
		<integer>" & mn & "</integer>
	</dict>
</dict>
</plist>
"

set loadBut to button returned of (display dialog "Load or Unload plist file" buttons {"Cancel", "Unload", "Load"})
if loadBut is "Load" then
	try
		set ff to open for access file pListfile with write permission
		write PLIST_File to ff as «class utf8»
		close access ff
	on error
		try
			close access file target
		end try
		display dialog pListLabel & " couldn't be created" buttons {"Cancel"} default button 1
	end try
	launchctl("load", pListfile, pListLabel)
else
	set launchdFiles to list folder LaunchAgentsFolder without invisibles
	try
		set unloadFile to item 1 of (choose from list launchdFiles with prompt "choose a file to unload:" default items {pListLabel & ".plist"})
		launchctl("unload", ((LaunchAgentsFolder as Unicode text) & unloadFile), text 1 thru -7 of unloadFile)
	end try
end if

----------------------
on launchctl(load, pListfile, N)
	try
		set launchctlCmd to "launchctl " & load & " -w " & quoted form of POSIX path of pListfile
		do shell script launchctlCmd
		if load is "unload" then do shell script "rm " & quoted form of POSIX path of pListfile
		display dialog N & " successfully " & load & "ed" buttons {"OK"} default button 1
	on error
		display dialog load & " failed" buttons {"OK"} default button 1 with icon stop
	end try
end launchctl

Thanks for the reply, but I am using the current os on a new Imac. Any thoughts?

Regards

:smiley:
Sorry, Tiger was the current OS when I wrote this article.
Of course launchd works also in Leopard


do shell script "launchctl load -w " & quoted form of POSIX path of pListfile & " >/dev/null 2>&1 &" 

On 10.4.11 (PPC) I hit up against some ‘workaround bonjour’ error when trying to load launch agents. Adding ’ >/dev/null 2>&1 &’ to the end allows rapid (presumptious) completetion of the buggy ‘launchctl load’ which loiters before returning the ‘workaround bonjour’ error. It makes the try/error clause somewhat obsolete… as I say, presumptuous - but worked for me.

This is a very cool idea, and I’d love to make it work. But I tried it with a test script (see below) and nothing happened. (Even after I restarted the computer). Did I do something wrong?

tell application "Finder"
	beep
	display dialog "hi there - it's " & (current date)
end tell

Model: MacBook 13"
Browser: Safari 525.28.3
Operating System: Mac OS X (10.5)

o never mind, it worked after I let it sit for a while. Just took a while to kick in. Very cool, thanks!!