I use launchctl to load LaunchAgents but in my script the launchctl seems very slow to respond. Even in this dummy script the ScriptEditor sometimes just hangs without signaling an error (also when it’s run as a script program it often hangs)
Anyone have any idea of what I’m doing wrong here? Could it be because I’m launching an app from a hidden folder (shouldn’t make a difference, I thought)?
set toDo to display dialog "what to do?" buttons {"load", "unload"}
set theToDo to button returned of toDo
try
do shell script "launchctl " & theToDo & " /Library/LaunchAgents/com.tmpass.plist"
on error
display dialog "There was an error, please try again!" buttons {"OK"} default button 1 with icon 0
end try
Glad to know it wans’t something stupid on my part. I played with the -w flag but it generates an error in my script which I don’t understand.
I found that adding a repeat loop checking the presence of the launched process seems to stabilize my script so that it won’t hang us much…
set toDo to display dialog "what to do?" buttons {"load", "unload"}
set theToDo to button returned of toDo
try
do shell script "launchctl " & theToDo & " /Library/LaunchAgents/com.tmpass.plist"
on error
display dialog "There was an error, please try again!" buttons {"OK"} default button 1 with icon 0
end try
-- check the "load"/"unload'
repeat
if theToDo is "load" then
tell application "System Events"
set test to (name of every process) contains "tmpass"
end tell
if test then exit repeat
else if theToDo is "unload" then
tell application "System Events"
set test to (name of every process) contains "tmpass"
end tell
if not test then exit repeat
end if
end repeat
Have you tried these commands in your plist to find out what’s happening? I haven’t used launchd so I’m not experienced to know what “normal” behavior is, but these seem to help finding problems.
– from the man page for launchd.plist
StandardOutPath
This optional key specifies what file should be used for data being sent
to stdout when using stdio(3).
StandardErrorPath <string>
This optional key specifies what file should be used for data being sent
to stderr when using stdio(3).