Get contents of iChat message via Applescript - applescript

I'll preface this with the fact that I'm new to applescript...
I have iChat set up to run this script whenever a new message is received:
using terms from application "iChat"
on message received theMessage from theBuddy for theChat
set theHandle to handle of theBuddy
tell application "MyApp"
receivedInstantMessage from theHandle message theMessage
end tell
end message received
end using terms from
This works as expected an MyApp (which I'm coding) receives two strings (the handle of the buddy and the message content). The only trouble is that this script only seems to work once at least one message has been received in iChat. I.e the script seems to only work from the second message onwards. If the buddy logs out, he'll have to send two messages again before my app receives the AppleEvent.
Am I making a basic mistake here?

The first message is actually a "text invitation" so your notification script will need another handler:
on received text invitation theMessage from theBuddy for theChat
-- your tell app statement goes here
end received text invitation

Related

MacOS. How to programmatically check a new SMS message?

I want to periodically programmatically check whether a new SMS message has arrived from a specific sender. If it came, then I want to copy text the text to the clipboard.
using terms from application "Messages"
on message received theMessage from theBuddy for theChat with eventDescription
set the clipboard to theMessage
end message received
end using terms from

applescript send SMS message

I am trying to send NEW messages using applescript through the macos messages app.
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
send targetMessage to buddy targetBuddyPhone of service "SMS"
end tell
end run
The above script runs fine on the condition that there is already a conversation started for the specific targetBuddyPhone in the messages app. Any ideas how to send a NEW message?.. I can't believe it isn't possible.
It could be possible to call URL from Apple Script in this form:
sms://open?addresses=+15558675309,+15558675301/&body=Text%20Here%20end!
It will not send message, but just prefill it and user will need to press Enter key.

Active Chat Received Message Applescript Handler gets runs twice, anybody know why?

Please can anyone help me with the below issue:
Active Chat Message Handler in Applescript for Messages app runs twice, looks like it receives the message twice, has anyone else seen this issue?
below is my code:
global myBuddysName
global textmsg
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))
set txt to paragraphs of (read foo for (get eof foo))
close access foo
return txt
end readFile
on msgImage(unixPath)
tell application "Finder"
set randomFile to (some file of folder unixPath)
set the clipboard to (randomFile as «class furl»)
end tell
tell application "System Events"
keystroke "v" using command down
key code 36
end tell
end msgImage
using terms from application "Messages"
on message received theMessage from theBuddy for theChat
set myBuddysName to 1st item of readFile("Users/username/Desktop/AutomatedTextReply/BuddyName.txt")
set textmsg to some item of readFile("Users/username/Desktop/AutomatedTextReply/Texts.txt")
if name of theBuddy is myBuddysName then
if theMessage contains "text" then
send textmsg to theBuddy
end if
if theMessage contains "image" or theMessage contains "img" then
msgImage("Macintosh HD:Users:username:Desktop:AutomatedTextReply:Images")
delay (5)
end if
if theMessage contains "video" then
msgImage("Macintosh HD:Users:usernameDesktop:AutomatedTextReply:Videos")
end if
end if
end message received
on message sent theMessage with eventDescription
end message sent
on chat room message received with eventDescription
end chat room message received
on active chat message received theMessage from theBuddy
set myBuddysName to 1st item of readFile("Users/username/Desktop/AutomatedTextReply/BuddyName.txt")
set textmsg to some item of readFile("Users/username/Desktop/AutomatedTextReply/Texts.txt")
if name of theBuddy is myBuddysName then
if theMessage contains "text" then
send textmsg to theBuddy
end if
if theMessage contains "image" or theMessage contains "img" then
msgImage("Macintosh HD:Users:username:Desktop:AutomatedTextReply:Images")
delay (5)
end if
if theMessage contains "video" then
msgImage("Macintosh HD:Users:username:Desktop:AutomatedTextReply:Videos")
end if
end if
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
end using terms from
Looks like the both handlers on message received theMessage from theBuddy for theChat and on active chat message received theMessage from theBuddy are called. Comment the second handler using (* and *) and try again. If it works as wanted keep the changes ;-)
Have fun, Michael / Hamburg

check if receiver number have iMessage enabled in apple script?

I am creating an apple script that will send iMessages to some numbers,
I want to check if those numbers have iMessage or not before sending it.
How could i achieve that in script editor? The following is the script to send the iMessage
tell application "Messages"
set theBuddy to buddy "the receiver number here" of service "my service"
send "Washer Done! :)" to theBuddy
end tell
still need to check if the receiver will be able to receive it or not before sending.
Any help?
Regards,

Trying to receive a message from os x message w/ applescript

Hey I'm running the following script:
using terms from application "Messages"
on message received this_message from this_buddy for this_chat
display dialog "test"
end message received
end using terms from
But I get the following error every time in Messages when i get a message:
Event: Message Received in Active Chat
File: registerToReceiveMessages.applescript
Error: Error -1708
I can't find that error anywhere on the internet. It seems to work for every chat except the active chat. Any ideas?
Also I'm trying to add the events for "addressed message received" but everytime I compile applescript replaces that to "received remote screen sharing invitation"
You will get an Error -1708 whenever you have chosen an AppleScript which doesn't handle the specified event.
For example, if your script has only implemented on message sent, but you set this script to run every time you receive a message, you will get Error -1708. This is because your script only knows how to handle outgoing, not incoming messages, therefore, Error -1708.
Now here is something interesting...
If you attempt to use the default script Mix Message Case.applescript for the events Message Received, Message Received in Active Chat, and Message Sent. The first and last work fine, but you will get a -1708 error for the active chat event. We can deduce that this means the script isn't handling the event Message Received in Active Chat. And so it appears that not even Apple can handle this event right now.
OS X Mavericks Update:
This update fixes the previously mentioned bug. If you select Apple's sample script Speak Events.applescript, you will notice that it handles messages received to the active chat room flawlessly. If you examine the code, you will notice that it is using the on active chat message received method. We can now use that in our scripts. Since I no longer have the old version installed, I cannot test if this same method works in the previous version.
Here is the code from Speak Events.applescript:
on active chat message received with eventDescription
say eventDescription
end active chat message received
Also notice how you no longer specify individual scripts to be run for specific events. Instead, you specify a single script handler for Messages events. This means that you must implement all the events in order to avoid getting -1708 methods. Notice how in the sample scripts, Apple even has the comment # The following are unused but need to be defined to avoid an error. Here is a template which can be used as a starting point for scripts:
using terms from application "Messages"
# The following are unused but need to be defined to avoid an error
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
end using terms from
If you begin with this script and implement only the methods you need (while leaving the rest intact), then you should avoid all -1708 errors.
I looks to me like the message received event handler only works when it feels like it in Messages. I had the same problem with a slightly different script. After finding another example on another website, copy-pasting it into a new AppleScript editor window and saving it over the AppleScript file copied to ~/Library/Scripts/Messages by Messages, it started to work.
There doesn't seem to be a problem with your script. If I replace my current script with your code and save the script, it works as expected, displaying a dialog containing the text test.
It may also be enough to just set the script to run to None and back to the script you created in the Messages Settings.

Resources