Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rothnd

macrumors newbie
Original poster
Oct 22, 2008
26
4
Hey all

I'm hoping someone out there has learned something useful about launchd, plist's, and timing scripts.

I have a rather simple script that I was trying to make run on a schedule through launchd (15 min to be exact) mainly to get to learn the system.

I'll have to post the code later since I'm currently on a Win comp at work and it won't read the scpt file.

Anyway, the problems... I finally got through it all enough to load the script into launchd through a valid plist ("verified in Lingon" though I don't think was necessary since all Lingon did was add fields withe "false" values). The jist though is the plist file (I've named com.nathan.messagealert.plist to keep the *^#4& formatting going) calls on an Applescript (which runs perfectly fine in the editor or even as an app on startup) which it should then run on a "StartInterval" schedule of 900 sec. Now, I read and found the proper way of making the script an executable file which made it load but it's acting very funny. Remember the time schedule of 15 min I mentioned?? Well, when the script was loaded it kept running and running and running...you get the idea. It wouldn't stop until I killed it.

Any thoughts on why this would react this way? Is a scpt file ok? Does the scpt need a "quit" or "exit" at the end of it? I'm just analyzing the possibilities...

Anyway, I'll post the code when I get home. I'd appreciate anything!
 
The Code to View

Ok, so here's my follow-up with the actual coding

Here is the applescript .scpt file that has no other properties other than being a scpt at the moment (not run only, or app, etc.)

Code:
say "Welcome back, Nathan."

tell application "Mail"
	repeat with thisAccount in every account
		set thisInBox to mailbox named "INBOX" of thisAccount
		set thisUnreadCount to unread count of thisInBox
		
		if thisUnreadCount is not 0 then
			set unreadMessages to (messages of thisInBox whose read status is false)
			
			if thisUnreadCount > 1 then
				set pluralText to "s"
				set verbText to "are"
			else
				set pluralText to ""
				set verbText to "is"
			end if
			
			set speechCountText to "There " & verbText & " " & thisUnreadCount & " unread message" & pluralText & " in " & (name of thisAccount) & "."
			say speechCountText
			
			tell application "Safari"
				activate
			end tell
			
		end if
	end repeat
end tell

And here is the plist file in use to call it

Code:
<?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>
	<true/>
	<key>KeepAlive</key>
	<false/>
	<key>Label</key>
	<string>com.nathan.messagealert</string>
	<key>OnDemand</key>
	<false/>
	<key>Program</key>
	<string>/usr/bin/osascript</string>
	<key>ProgramArguments</key>
	<array>
		<string>osascript</string>
		<string>/Library/Scripts/MailMessage.scpt</string>
	</array>
	<key>RunAtLoad</key>
	<false/>
	<key>StartInterval</key>
	<integer>1200</integer>
</dict>
</plist>

Again, any input on why this would cause the script to run over and over and over would be greatly appreciated. Thanks!!!
 
Updates Plist fixed, Applescript fails

Ok, Just to get caught up here and make it very clear where I'm at here's the new full story

The applescript script seems to be failing for some reason despite compiling and working properly from the script editor.

Here is the plist that calls the script and seems to be working properly (by that I mean that it executes the first say command at 5 min intervals)

<?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>com.nathan.MessageAlert</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/Library/Scripts/MailMessage.scpt</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>

And here is the script

say "hello"
tell application "Mail"
activate
delay 5
repeat with thisAccount in every account
set thisInBox to mailbox named "INBOX" of thisAccount
set thisUnreadCount to unread count of thisInBox

if thisUnreadCount is not 0 then
set unreadMessages to (messages of thisInBox whose read status is false)

if thisUnreadCount > 1 then
set pluralText to "s"
set verbText to "are"
else
set pluralText to ""
set verbText to "is"
end if

set speechCountText to "There " & verbText & " " & thisUnreadCount & " unread message" & pluralText & " in " & (name of thisAccount) & "."
say "Welcome Back Nathan."
say speechCountText

tell application "Safari"
activate
end tell

end if
end repeat
end tell

I thought by fixing the issues with the plist this would all be good but I am, yet again, at a loss. I'd like to mention that the script file is just that...a .scpt file... This file was not saved with any other properties offered (e.g. Run on Start, App, etc). Is this causing the problem or....? Also, again, it does execute the first say command but mail, the first application to open, never opens and the script never continues.

I'd appreciate any info in learning the problem here is!
 
It this running as a Daemon or an Agent?
A daemon is not going to run as the current user, so it is not going to have access to the current user's mail boxes. Also, daemon is not going to be able to access the GUI (e.g. put up the Main window).

So, if you aren't already, run it as a user agent
(you put the property list in ~/Library/LaunchAgents or maybe /Library/LaunchAgents rather than the daemon location).

Also, I think even for launch agents, I think the property list needs to be specific about whether or not it needs to display a GUI. I don't remember exactly how that's done... Look in here under launch agents: http://developer.apple.com/technotes/tn2005/tn2083.html
 
Thanks!

Thanks for the technote link...very helpful. I did have it running as an agent initially and is working properly now. However, it seems like the issue was that any changes to the plist were not being executed upon making those changes (immediate) rather they were only made after a system shutdown. Is this normal? Is there anyway to make changes take effect sooner/without shutdown?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.