AppleScript, Keychain and GUI Access from launchd
I wanted to launch some scripts from cron (I know it's deprecated) that would mount some shares using AppleScript (so that share credentials are prompted via GUI or accessed from Keychain) and display notification through Growl. On first try, this worked under Mac OS X 10.5.5, but then began to fail after an upgrade to 10.5.6. I would find error messages that were not very clear:
osascript: execution error: An error of type -6600 has occurred. (-6600) com.apple.launchd[1] (0x2103c0.cron37314): Could not setup Mach task special port 9: (os/kern) no access growlnotify[1929] could not find local GrowlApplicationBridgePathway, falling back to NSDNC
After some research it was apparent that my original cron choice should not have worked under 10.5.5 either, but for some unknown reason it did. To make a long story short, scripts which need to run in some logged-in user context (to have access to things like the user's keychain or the user's GUI environment) need to run in the "Aqua" launchd session. This can be accomplished by loading the agents using "launchctl load -S Aqua" or by including the key "LimitLoadToSessionType" with the string "Aqua" in the agent configuration file.