Applescript content of incoming iMessage - applescript

I need to extract/read the content of an incoming Message received via iMessage. I am having a hard time finding out the right command to extract the info. Could someone point me in the right direction ?
I know I don't have much so far, I am a newbie :(
using terms from application "Messages"
on message received theMessage
return theMessage
end message received
end using terms from

I suggest you to look examples provided in ElCapitain your user Library / Application Scripts / com.apple.iChat, there is "Mix message Case.applescript file which contains syntax for each case of message (received, send, invitation,...)
According to that file, you must add attribute to the "on message received" like :
on message received theMessage from theBuddy for theChat

Related

Can't get meeting of outgoing message in Outlook applescript

I want to get meeting of a meeting message from Sent Items folder with AppleScript.
tell application "Microsoft Outlook"
set myFolder to folder "Sent Items" of default account
set meetingList to (every meeting message whose (is read is true)) of myFolder
repeat with theMeeting in meetingList
set meetingType to type of theMeeting
end repeat
end tell
but the I got this error:
error "Microsoft Outlook got an error: Can’t get type of outgoing message id 783." number -1728 from type of outgoing message id 783
it looks like meeting message class inherits form incoming message and not outgoing message. so it's not possible to get meeting message for outgoing emails.
meeting message n [inh. incoming message > message > todoable object > categorizable object > object > …] : A meeting message recieved by the user.
however, I find another solution to use Calendar Suite APIs directly which is a lot better with events processing.

Can't send message to group chat

I found a script that sends messages from the command line, but It won't work with my group chat with spacing and capitalization in the name.
#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
EOF
when I run ./sendmessage "GROUP CHAT" "test" it gives me
228:261: execution error: Messages got an error: Can’t get buddy id "7087805D-ED73-4DB9-81AB-7C964B98AB34:group chat". (-1728)
Unforunately, it is not possible to send a message to a group chat with applescript by using the chat's name. To send to an existing group chat, you will instead need to obtain the chat's internal guid. There are 2 methods of doing this.
Extract it from the messages.app internal database, located on disk at ~/Library/Messages/chat.db. It is the guid column under the chat table. If you have the group chat named it will be easy to find by display_name.
You can use an AppleScript handler for Messages with the on chat room message received handler to capture the ID when someone posts in that chat.
Once you have the guid (should in a format similar to iMessage;+;chat831551415676550949), your script can be modified to send to the ID instead:
#!/bin/sh
recipient="${1}"
message="${*:2}"
echo "$recipient"
cat<<EOF | osascript - "${recipient}" "${message}"
on run {targetChatID, targetMessage}
tell application "Messages"
set targetChat to a reference to text chat id targetChatID
send targetMessage to targetChat
end tell
end run
EOF
To send an image instead of text, replace the targetMessage with:
set ImageAttachment to POSIX file "File path here" as alias

Can't access to mail attachments in mail app via apple script

Let's assume that in the following code
tell application "Mail"
tell first account
tell first mailbox
set myMessage to first message
set myAttachment to mail attachments of myMessage
end tell
end tell
end tell
the first message of the first mailbox of the first account actually has attachments. Let's further assume that the account in question is a GMail account (maybe that proves relevant) and that I am trying this on El Capitan.
On the "set myAttachments" line I always get the error "error in apple event routine, -10000" (losely translated from German). I have read in old threads that there was indeed a bug in the access to attachments. But I also saw a new thread that was talking specifics about how to best handle them (no mentioning of having no access at all).
What am I missing? Does it work? Maybe in general but not with GMail? Do I have to do something specific?
Thanks!
Sandro
The exact syntax for this is "mail attachement of myMessage" (without "s").
The script bellow is working perfectly on my side for selected emails:
tell application "Mail"
activate
set ListMessage to selection
repeat with aMessage in ListMessage
set AList to every mail attachment of aMessage
repeat with aFile in AList
if (downloaded of aFile) then
-- do something with aFile
end if
end repeat -- next file
end repeat -- next message
end tell
Are you sure the the message with attachement is in your first account/first mailbox?

How to start new conversation in iMessage using AppleScript?

So I'm working on creating an applescript which essentially automates sending an imessage. What I have working now is:
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send msg to buddy phoneNum of service id serviceID
end tell
end run
This works for the most part except it doesn't work when starting a new conversation. When you run the script to a number which you don't have a conversation with in messages, you get a popup warning saying "Your message does not have any recipients". However, this creates a conversation with that person, and when you run the same script again it works.
I figured if it works the second time, there must be a way to create a new conversation somehow, however I have never really used applescript or really any script languages before so I'm not sure how to go about that.
Edit: Immediately after posting I thought of a rough workaround. If right before you send the message you send an empty string, you can create a new conversation, and it works with a already existing conversation.
on run {msg, phoneNum}
tell application "Messages"
set serviceID to id of 1st service whose service type = iMessage
send "" to buddy phoneNum of service id serviceID
send msg to buddy phoneNum of service id serviceID
end tell
end run
While this works, I'd imagine there is a better/more elegant solution than this one.
My solution is to tell Applescript to press "Command + N", which is the shortkey for "Start a new conversation"
activate application "Messages"
tell application "System Events" to tell process "Messages"
key code 45 using command down -- press Command + N to start a new window
keystroke "<replace with phone number>" -- input the phone number
key code 36 -- press Enter to focus on the message area
keystroke "<replace with message>" -- type some message
key code 36 -- press Enter to send
end tell
This script will start a new conversation and send the message to the phone number through iMessage
There are many ways to do it.
First example:
on run {targetBuddyPhone, targetMessage}
tell application "Messages"
set targetService to 1st service whose service type = iMessage
set targetBuddy to buddy targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run
Second example:
tell application "Messages"
set targetBuddy to "+18001234567"
set targetService to id of 1st service whose service type = iMessage
repeat
set textMessage to "Hello pal!"
set theBuddy to buddy targetBuddy of service id targetService
send textMessage to theBuddy
delay (random number from 10 to 30)
end repeat
end tell

Duplicate Firing of `active chat message received` handler

While Manu G E asked a similar question twice, neither got an adequate answer, and I'm hoping I'll have better luck.
I'm writing an AppleScript to execute a handler when Messages.app receives a message. The script is being saved to ~/Library/Application\ Scripts/com.apple.iChat and is set in the Messages preferences to be the AppleScript handler.
When Messages is the frontmost application and a message is received, the active chat message received handler is fired twice. This doesn't seem to be a problem when Messages is in the background (received messages then fire message received, and that handler only once). I know which handler is fired because the portion that deals with the handlers looks like this:
using terms from application "Messages"
on message received _msg from _sender for _chat with _text_desc
if DEBUG then display dialog "message received"
message_received(_sender)
end message received
on chat room message received _msg from _sender for _chat with _text_desc
if DEBUG then display dialog "chat room message received"
message_received(_sender)
end chat room message received
on active chat message received _msg from _sender for _chat with _text_desc
if DEBUG then display dialog "active chat message received"
message_received(_sender)
end active chat message received
-- More handlers below, mostly like the above or empty
end using terms from
I set a DEBUG property to true and can see which handler gets fired.
I've tried working around this by writing a temporary file (using the UUID of the _sender). The message_received handler checks for the existence of the file and is supposed to do nothing if it's present. But this hasn't worked, even with random delays. I tried extending the length of the random delays, but this brings up errors about the AppleScript running for more than 10 seconds, even when enclosing the code within a with timeout of block.
Regardless of Apple's apparent support for executing AppleScripts in response to Messages events, perhaps I should look at some other mechanism to support this request from the client. I'm open to ideas.
Somehow I managed to find a simple but (very) dirty hack that seems to work for me, but I can't say if it will work on any machine. So "active chat message received" seems to be called twice at the same time, but I noticed something like do shell script "php -r 'echo microtime() >> file.txt'" sometimes reveals slightly different values.
I also use a property as a flag and try to take advantage of that shell execution tiny interval by writing to a file:
echo 0 > ~/Documents/flag.txt
then:
property flag : 0
using terms from application "Messages"
#...
on active chat message received theMessage from theBuddy
set response to false
set the_script to "cat ~/Documents/flag.txt"
set flag to do shell script the_script
do shell script "echo 1 > ~/Documents/flag.txt"
if flag is "0" then
set response to true
else
do shell script "echo 0 > ~/Documents/flag.txt"
end if
if response then
#this should be executed only once
end if
end active chat message received
#...
end using terms from
And voilà. Again I cannot say if that solution works everytime, and explaining why it actually works in my case is way beyond my capabilities right now. Still, I hope it will be useful. Cheers

Resources