Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Messages + iTunes Applescript

Hello all,

I have made an applescript to control iTunes via iMessage from my iPhone. It contols volume, play/pause, etc... When I set it as the AppleScript Handler and send a message to my Mac, i get two errors:User uploaded file

The code is as follows:

using terms from application "Messages"

on message receivedtheMessage

if theMessage is "Play" then

tell application "iTunes" to play

else if theMessage is "Pause" then

tell application "iTunes" to pause

else if theMessage is "Volume Up" then

set curVolume to output volume of (get volume settings)

if curVolume < 90 then

set newVolume to curVolume + 10

else

set newVolume to 100

end if


set volumeoutput volumenewVolume

else if the message is "Volume Down" then

set curVolume to output volume of (get volume settings)

if curVolume > 10 then

set newVolume to curVolume - 10

else

set newVolume to 1

end if


set volumeoutput volumenewVolume

else if theMessage is "Play Song" then

tell application "Messages"

set theBuddy to buddy "(My Phone # here)" of service id "81E10DF5-FDED-44F8-BD89-AD2A299465ED"

send "Song Please in 10 Seconds" to theBuddy

delay 10

end tell

set song to theMessage

tell application "iTunes"

activate

playtracksong

end tell

end if

end message received

end using terms from

I have no idea what is wrong, any help is appreciated,

Ryan.

MacBook Pro, OS X Mountain Lion (10.8.4)

Posted on Feb 8, 2014 3:11 PM

Reply
6 replies

Feb 9, 2014 4:43 AM in response to RyanSiebecker

I think the problem is with nesting of commands for iTunes inside the Messages tell (using terms ...) block.


See if something along these lines helps:


using terms from application "Messages"

on message receivedtheMessage

set iTunesCmd to theMessage

end message received

end using terms from

tell application "iTunes"

if iTunesCmd is "Play" then play

if iTunesCmd is "Pause" then pause


--...

end tell

Feb 9, 2014 8:00 AM in response to RyanSiebecker

I did a bit of research AKA googling:


If you're using the Messages app interactively, you need to add another handler:


on active chat message receivedwitheventDescription

--

end active chat message received



The full explanation can be seen here: http://stackoverflow.com/questions/12377572/trying-to-receive-a-message-from-os- x-message-w-applescript



Actually, it seems to go a bit further than the above (but I had not read the whole piece I reference). You need to add all the handlers in the template and then only use the ones that are relevant to you.


on message sent theMessage with eventDescription
end message sent
on message received theMessage with eventDescription
end message received
on chat room message received with eventDescription
end chat room message received
on active chat message received with eventDescription
end active chat message received
on addressed message received theMessage from theBuddy for theChat with eventDescription
end addressed message received
on received text invitation with eventDescription
end received text invitation
on received audio invitation theText from theBuddy for theChat with eventDescription
end received audio invitation
on received video invitation theText from theBuddy for theChat with eventDescription
end received video invitation
on received local screen sharing invitation from theBuddy for theChat with eventDescription
end received local screen sharing invitation
on buddy authorization requested with eventDescription
end buddy authorization requested
on addressed chat room message received with eventDescription
end addressed chat room message received
on received remote screen sharing invitation with eventDescription
end received remote screen sharing invitation
on login finished with eventDescription
end login finished
on logout finished with eventDescription
end logout finished
on buddy became available with eventDescription
end buddy became available
on buddy became unavailable with eventDescription
end buddy became unavailable
on received file transfer invitation theFileTransfer with eventDescription
end received file transfer invitation
on av chat started with eventDescription
end av chat started
on av chat ended with eventDescription
end av chat ended
on completed file transfer with eventDescription
end completed file transfer


Message was edited by: Bernard Harte

Feb 9, 2014 11:00 AM in response to RyanSiebecker

Try using just this script as the handler, then build-out from there. (I have taken one of Apple's examples, and used it as a template to process just message received events.) It works for me.


using terms from application "Messages"

on message senttheMessagewitheventDescription

#

end message sent


on message receivedtheMessagewitheventDescription

my messageReceived(theMessage)

end message received


on chat room message receivedwitheventDescription

#

end chat room message received


on active chat message receivedwitheventDescription

#

end active chat message received


on addressed message receivedtheMessagefromtheBuddyfortheChatwitheventDescription

#

end addressed message received


on received text invitationwitheventDescription

#

end received text invitation


on received audio invitationtheTextfromtheBuddyfortheChatwitheventDescription

#

end received audio invitation


on received video invitationtheTextfromtheBuddyfortheChatwitheventDescription

#

end received video invitation


on received local screen sharing invitationfromtheBuddyfortheChatwitheventDescription

#

end received local screen sharing invitation


on buddy authorization requestedwitheventDescription

#

end buddy authorization requested


on addressed chat room message receivedwitheventDescription

#

end addressed chat room message received


on received remote screen sharing invitationwitheventDescription

#

end received remote screen sharing invitation



# The following are unused but need to be defined to avoid an error


on login finishedwitheventDescription

#

end login finished


on logout finishedwitheventDescription

#

end logout finished


on buddy became availablewitheventDescription

#

end buddy became available


on buddy became unavailablewitheventDescription

#

end buddy became unavailable


on received file transfer invitationtheFileTransferwitheventDescription

#

end received file transfer invitation


on av chat startedwitheventDescription

#

end av chat started


on av chat endedwitheventDescription

#

end av chat ended


on completed file transferwitheventDescription

#

end completed file transfer


end using terms from


on messageReceived(theMessage)

tell application "iTunes"

ignoring case

if theMessage is "play" then play

if theMessage is "pause" then pause

end ignoring

end tell

end messageReceived

Messages + iTunes Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.