OS X Messages AppleScript handler: save images? - applescript

I've been attempting to use AppleScript to save incoming messages on an Apple Messages account, like so:
on active chat message received theMessage from theBuddy
set theFileID to open for access POSIX file "/Users/me/Desktop/test" with write permission
write theMessage to theFileID
close access theFileID
return theMessage
end active chat message received
which works great for text messages, but if the user sends an image the message appears to just be an empty string. Is there any way to fetch this image, or - as I fear - is it not accessible?

This isn't a direct answer, but I've discovered that the OS X Messages app uses SQLite for storage, at ~/Library/Messages/chat.db. It has a table named attachment which lists the filenames of all attachments.
For my purposes, this is enough - but I'd love to hear if there are more legitimate alternatives.

Related

Use Automator to send files via Messages App

I've seen some older posts about this issue but no real fixes.
I"m using macOS 13.1 and I would like to use Automator to send my clients iMessage or SMS texts that include images. I am able to send actual "text" just fine.
When I try to send a file, I see the file in the Messages app but I get a failed to send error. I see the same behavior using SMS or iMessage.
My simple Code
set image to POSIX file "/Path/To/File.ext"
tell application "Messages"
activate
set iMessageService to 1st account whose service type = iMessage
set client to participant "5551234567" of iMessageService
send image to client
end tell
Again, I can send texts to the same number but not a file. I am able to send the file to the number directly from the Messages app.
Google, Apple Developer, multiple phone numbers.

Send iMessage to group chat

How can you send a iMessage to a group chat? I have searched everywhere and I can't find anything. I want to send it to a group chat named something, possibly If I get a list of buddies in the chat I can send it to everyone at once? A problem may be that I'm also in the chat, so that might be a problem.
To send to an existing group chat, you will 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), you can use AppleScript to send the message.
tell application "Messages"
set myid to "iMessage;+;chat831551415676550949"
set mymessage to "Hello World"
set theBuddy to a reference to text chat id myid
send mymessage to theBuddy
end tell
To send an image, replace the mymessage text with:
set ImageAttachment to POSIX file "File path here" as alias

Scheduling an email to send in mail (mac)

I have an email that I need to schedule to send later. I know you can schedule an apple script to fire off at a set time using iCal, but I have been unable to find one that works. It doesn't necessarily have to be done this way though. I would be grateful of a solution to this problem.
This works for me with a run script alarm in iCal on 10.6.8
tell application "Mail"
activate
set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"Triggered"}
tell mymail
make new to recipient at beginning of to recipients with properties {address:"first#email.com, second#email.com"}
set content to "hi"
end tell
--show message window (otherwise it's hidden)
set visible of mymail to true
--bring Mail to front
activate
send mymail
end tell
I'm also getting an error like outgoing message 1 doesn’t understand the send message when trying to send an outgoing message. Referring to a message created from a script still seems to work though.
tell application "Mail"
set m to make new outgoing message with properties {subject:"subject", content:"content"}
tell m
make new to recipient at end of to recipients with properties {address:"jana#doe.com, john#doe.com"}
end tell
send m
end tell
You could also create a Calendar alarm in Automator.
I just accomplished this without Automator, using only Apple Mail:
Double-click anywhere your calendar to start creating an event.
Give the event a name. This will be the subject line of the email.
Set the date and time of the event to when you want the email to be sent.
If you want the email to be sent on a schedule, choose a repeat frequency and potential termination date.
For the alert, choose Custom....
Select email. You might be prompted to add a contact card to the Contacts app.
Choose the person to whom you want to send the email, and set the time to At Time Of Event. Press OK.
You can choose to add Notes, a URL, or attachments. I haven't tried these options, but I assume that they are carried forward in the email that is sent.

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.

Receive Adium messages in Applescript

I'm trying to write a simple chat bot for Adium, that will post "lol" and "haha" and answer basic questions in annoying group chats that some people keep adding me to.
I've covered the "lol" part with a few simple send and delay commands in a repeat, but I need to do some interaction as well.
Answer "yes" to anything with a question mark for example...
Believe me or not, such a simple bot would pass a Turing Test for those conversations.
Opening the Adium dictionary does not reveal any obvious way of getting messages, nor does the Growl dictionary.
I did found out that I can run a script every time a message is received, Is there a way to get access to the sent message?
Adium pref http://media.ruk.ca/images/adiumpreferences.png
My code:
tell application "Adium"
activate
set theChat to the active chat
send theChat message "Hi"
delay 5
send theChat message "How's life?"
delay 10
repeat 10 times
send theChat message "Realy?"
delay 5
send theChat message "Lol :P"
delay 15
send theChat message "Haha XD"
delay 15
send theChat message "Yes1!!1"
delay 20
send theChat message "I like it! :D"
delay 10
end repeat
send theChat message "Bye!"
tell theChat to close
end tell
You can pipe to a script using Pipe Event as well now.
Based on review of the current Adium sourcecode and a search for current and past items in the Adium bug tracker and wiki which contain both "applescript" and "message" as substrings, this does not appear to be possible when using only AppleScript in Adium 1.0 through 1.3.10 (latest at time of writing). It seems to have been possible with plain AppleScript in Adium 0.89.1, but the volunteer developers are not yet convinced that adding this feature back is worth the effort.
To access the message content in AppleScript right now probably requires writing an Adium Xtra to forward the information. Examples of Xtra plugins that access the text of last message include Challenge/Response or SpamFilter. The sourcecode for SpamFilter is available on BitBucket, so you could conceivably modify it to send message contents to an AppleScript.
EDIT: Since I posted my response, user 'zostay' has spotted a new Adium Xtra called "Pipe Event". It allows sending the text of an event to a script in exactly the manner I envisioned when I wrote my second paragraph, so I'm up-voting zostay's answer. Sourcecode is also available.

Resources