1

I'm hoping to programmatically send files via imessage, yet cannot figure out how to send a file object via the API.

This script send a message to a specified user with the file path:

imessage() {
    file="$PWD/$2";
    osascript -e 'tell application "Messages" to send '\"$file\"' to buddy '\"$1\"';
}

How can I send the actual file? A pointer to the docs for this would also be helpful.

3
  • 2
    Unfortunately you can't automate anything you don't know how to do yourself. You'll have to figure out how to use AppleScript to send a file before you can have Bash ask AppleScript to send a file. Commented Feb 25, 2019 at 21:05
  • 2
    BTW, note that the pattern given in the code in the question is prone to injection attacks -- someone who controls the filenames can run arbitrary programs on your machine. Commented Feb 25, 2019 at 21:10
  • @CharlesDuffy Noted, this is for personal use. Commented Feb 25, 2019 at 21:11

1 Answer 1

2

It seems that the easiest way to do this is to create an AppleScript file and call this via bash as demonstrated here https://gist.github.com/homam/0119797f5870d046a362.

AppleScript - sendmessage.scpt

on run argv
    set filename to item 1 of argv
    set buddyName to item 2 of argv
    set attach to POSIX file filename
    tell application "Messages" to send attach to buddy buddyName
end run

Bash script

imessage() {
    osascript sendmessage.scpt "$PWD/$2" "$1";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.