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

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
I'm trying to design a script that will cause iTunes to play a song when I tell Siri on my iPhone to send an e-mail to my e-mail address. Mail is supposed to receive the e-mail, see that it was addressed to "iTunes", and run the script. I did send an e-mail addressed to "iTunes," and everything that doesn't have to do with AppleScript seems to be configured correctly. Here's my code:

Code:
using terms from application "Mail"
	on perform mail action with messages messageList for rule aRule
		tell application "Mail"
			repeat with thisMessage in messageList
				set theCommand to content of messageList
			end repeat
		end tell
	end perform mail action with messages
end using terms from

tell application "iTunes"
	if theCommand contains "play" then
		if theCommand is equal to "play" then
			play
		else
			set prevTIDs to text item delimiters of AppleScript
			set text item delimiters of AppleScript to "play "
			set subject to text items of theCommand
			
			set text item delimiters of AppleScript to ""
			set subject to "" & subject
			set AppleScript's text item delimiters to " by artist "
			set delimitedList to every text item of subject
			set theTrack to the first item of delimitedList
			try
				set theArtist to the second item of delimitedList
				set artistIsDefined to true
			on error
				set artistIsDefined to false
			end try
			set AppleScript's text item delimiters to prevTIDs
			if artistIsDefined is true then
				play (every track in playlist 1 whose artist is theArtist and name is theTrack)
			else
				play (every track in playlist 1 whose name is theTrack)
			end if
		end if
	else if theCommand is equal to "pause" then
		pause {}
	else if theCommand is equal to "stop" then
		stop {}
	end if
end tell

Now, when I remove everything before the "tell iTunes" part and set theCommand to a valid command, the script works.
 
Last edited by a moderator:
Try this :

Code:
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with thisMessage in messageList
set theCommand to content of [COLOR="Red"]thisMessage[/COLOR]
end repeat
end tell
end perform mail action with messages
end using terms from
 
Try this :

Code:
using terms from application "Mail"
on perform mail action with messages messageList for rule aRule
tell application "Mail"
repeat with thisMessage in messageList
set theCommand to content of [COLOR="Red"]thisMessage[/COLOR]
end repeat
end tell
end perform mail action with messages
end using terms from

Still doesn't work.
 
I added a try block to catch any errors, and I got this error message:

Can't make <<class ctnt>> of <<class mssg>> id 9574 of <<class mbxp>> "INBOX" of <<class mact>> "Gmail" into type <<class utf8>>.
 
Code:
using terms from application "Mail"
	on perform mail action with messages messageList for rule aRule
		tell application "Mail"
			repeat with thisMessage in messageList
				try
					set theCommand to content of thisMessage as string
				on error errMsg
					display alert errMsg
				end try
			end repeat
		end tell
		tell application "iTunes"
			if theCommand contains "play" then
				if theCommand is equal to "play" then
					play
				else
					set prevTIDs to text item delimiters of AppleScript
					set text item delimiters of AppleScript to "play "
					set subject to text items of theCommand
					set text item delimiters of AppleScript to ""
					set subject to "" & subject
					set AppleScript's text item delimiters to " by artist "
					set delimitedList to every text item of subject
					set theTrack to the first item of delimitedList
					try
						set theArtist to the second item of delimitedList
						set artistIsDefined to true
					on error
						set artistIsDefined to false
					end try
					set AppleScript's text item delimiters to prevTIDs
					if artistIsDefined is true then
						try
							play (every track in playlist 1 whose artist is theArtist and name is theTrack)
							say "Playing " & theTrack
						on error errMsg
							say errMsg
						end try
					else
						play (every track in playlist 1 whose name is theTrack)
					end if
				end if
			else if theCommand is equal to "pause" then
				pause {}
			else if theCommand is equal to "stop" then
				stop {}
			end if
		end tell
	end perform mail action with messages
end using terms from

My new code - the original problem is fixed, but an unknown problem has arisen. It will say "playing (track)," but iTunes won't play it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.