Can't get meeting of outgoing message in Outlook applescript - macos

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.

Related

Modify/change Outlook Calendar event with AppleScript

I have a script that creates an Outlook calendar event from content in an email (also from Outlook). The email contains the name of the event, date/time, and a single attendee's information.
The event is first created with the one attendee that was specified from the email.
I will receive more emails that indicate additional attendees (one per email) and which event the new attendee should be added to.
Right now my script is able to create a calendar event(with location, subject, start/end time, content, and reminder time) and add one required attendee to the event, then send the event to the attendee.
I need to get access to existing events with the same subject and add more attendees to the event.
Right now I'm trying to achieve this with this code:
tell application "Microsoft Outlook"
<...>
set textContent to paragraphs of msgContent
--Check for existing calendar event
set prevEvent to "false"
try
set existingEvent to get (calendar event of calendar whose its subject is item 9 of textContent)
tell existingEvent
make new required attendee at end of attendees with properties {email address:{name:attendeeName, address:attendeeEmail}}
end tell
open existingEvent --This is done so I can make sure it has the info I need. The I'm done it will be changed to "send meeting..."
set prevEvent to "true"
on error
log "Event does not exsist"
end try
if prevEvent = "true" then exit repeat
<...>
--Code to create a new event
set msgToSend to make new calendar event with properties {location:msgLocation, subject:msgSubject, start time:meetingDateTime, end time:endTime, content:msgNewContent, has reminder:true, reminder time:30}
<...>
end tell
I'm not getting an error but my code just exits the try/error so I think my "set existingEvent to" is wrong.
I was able to add an attendee with this code
--Create/add attendee for calendar message invite
make new required attendee at msgToSend with properties {email address:{name:"First Last", address:"first.last#email.com"}}
Where msgToSend is a new calendar event that will be sent out. This event is created by
--Create calendar message invite
set msgToSend to make new calendar event with properties {location:msgLocation, subject:msgSubject, start time:meetingDateTime, end time:endTime, content:msgNewContent, has reminder:true, reminder time:30}
I was not able to determine how to check for and modify existing events using AppleScript. I switched to using Microsoft Flow to achieve my task.

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

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

Applescript content of incoming iMessage

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

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