How to make iTunes play from email messages

I have home automation and I want ed to be able to email my house to tell it to play music. I also wanted to be able to specifiy the song in the email. So, the script will open itunes if it has “itunes” in the subject box of the email. Next, I then want to refer to the body of the email as the title of the song and have iTunes play it. Here is what I have so far. I have one problem. It works fine if you put an actual song name where messageBody is. But, if I do it as below, the program gets the email body, but it errors when it trys to play. Any ideas on how to fix this? I guess the track name needs to be variable, and the body of the email specifies it.

activate
delay 5
check for new mail for pop account "home"
set messageBody to content of (message 1 of inbox)
set messageSubject to subject of (message 1 of inbox)
if messageSubject contains "itunes" then
	tell application "iTunes"
		activate
		delay 5
		play track messageBody of playlist "library"  <----error is here
	end tell
	tell application "Mail"
		delete (message 1 of inbox)
	end tell
	quit "mail"
else
	tell application "iTunes"
		quit "itunes"
	end tell
	tell application "Mail"
		quit "mail"
	end tell
end if

end tell

Please do this when you post code. If you need help, let me know.

like this?

tell application "Mail"
	activate
	set countUnread to unread count of inbox of pop account "home"
	if countUnread is less than 1 then
		quit "mail"
	end if
end tell

looks like that worked. I will repost my script. On that test script above, I wanted it to close Mail if there are no unread messages, but it doesn’t run. Do you know why?

tell application "Mail"
	activate
	set countUnread to unread count of inbox of pop account "home"
	if countUnread is less than 1 then
		quit "mail"
	end if
end tell

Here is the original script for this post:

tell application "Mail"
	activate
	delay 5
	check for new mail for pop account "home"
	set messageBody to content of (message 1 of inbox)
	set messageSubject to subject of (message 1 of inbox)
	if messageSubject contains "itunes" then
		tell application "iTunes"
			activate
			delay 5
			play (first track of library playlist 1 whose enabled is true and artist is "susan ashton")
		end tell
		tell application "Mail"
			
			delete (message 1 of inbox)
			
		end tell
		quit "mail"
	else
		tell application "iTunes"
			quit "itunes"
		end tell
		tell application "Mail"
			quit "mail"
		end tell
	end if
	
end tell

The message content may contain extraneous text or characters, bonedoc - such as returns, signatures, etc. Try something like this:

tell application "Mail"
	tell (first message of inbox whose subject contains "iTunes") to if exists then
		set target_track to first paragraph of (get content)
		tell application "iTunes" to play track target_track of playlist "library"
	end if
end tell

You can’t tell a string, “mail”, to quit. You’re already in an application "Mail" tell block, so you can just say: quit.

tell application "Mail"
	if inbox's unread count is 0 then quit
end tell

:slight_smile: