Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 9, 2012, 08:15 PM   #1
moonman239
macrumors 65816
 
Join Date: Mar 2009
[OS X 10.8.2] AppleScript Mail script help

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.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.

Last edited by SandboxGeneral; Nov 11, 2012 at 07:07 PM. Reason: Changed to code tags
moonman239 is offline   0 Reply With Quote
Old Nov 9, 2012, 10:02 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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 thisMessage
end repeat
end tell
end perform mail action with messages
end using terms from
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Nov 11, 2012, 06:37 PM   #3
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
Quote:
Originally Posted by kryten2 View Post
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 thisMessage
end repeat
end tell
end perform mail action with messages
end using terms from
Still doesn't work.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote
Old Nov 11, 2012, 08:00 PM   #4
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
I added a try block to catch any errors, and I got this error message:

Quote:
Can't make <<class ctnt>> of <<class mssg>> id 9574 of <<class mbxp>> "INBOX" of <<class mact>> "Gmail" into type <<class utf8>>.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote
Old Nov 27, 2012, 06:32 PM   #5
moonman239
Thread Starter
macrumors 65816
 
Join Date: Mar 2009
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.
__________________
The funny thing about this signature is that by the time you get to the end of it, you will have spent between five and ten seconds doing so.
moonman239 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Applescript newb: declaring a variable in another script. Vanilla Face Mac Programming 2 Jan 5, 2014 07:39 PM
Why won't this applescript execute as folder action script? dancks Mac Programming 20 Jul 18, 2013 12:46 PM
Anyone know how to use AppleScript or shell-script? gba2000 Mac Applications and Mac App Store 0 Apr 23, 2013 11:14 AM
How to access the script dictionary for numbers for help in applescript? Jitin Jindal Mac Programming 1 Mar 7, 2013 07:51 AM
iCal "Run Script" alert opens .scpt file in AppleScript Editor instead of running it drskot Mac Programming 3 Jun 7, 2012 12:47 PM

Forum Jump

All times are GMT -5. The time now is 08:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC