Applescript SMS Notification w/ Transmission bit-torrent client

(*
Transmission bit-torrent client has a feature to run a script when a download finishes.
Transmission only accepts shell scripts so I broke this into two parts: a compiled applescript and a shell script. Bear with me.
*)

– PART 1: transmissionSendSMS.scpt

on run argv
tell application “Mail”
set sms to “Transmission finished downloading " & item 1 of argv & " !”
set savedDelimiters to AppleScript’s text item delimiters
set the clipboard to sms as string
set AppleScript’s text item delimiters to " "
set subjectline to the clipboard
set my_message to make new outgoing message with properties {subject:“:white_check_mark::white_check_mark::white_check_mark:”, content:subjectline, visible:false}
set AppleScript’s text item delimiters to savedDelimiters
tell my_message
make new to recipient at end of to recipients with properties {name:“5552352525@messaging.sprintpcs.com”}
send
end tell
end tell
end run

(*
Where “5552352525@messaging.sprintpcs.com” is your 10 digit phone number (no spaces) followed by your carrier service

Addresses and corresponding carriers:

PHONENUM@message.alltel.com # SMS: Alltel
PHONENUM@txt.att.net # SMS: AT&T (formerly Cingular)
PHONENUM@myboostmobile.com # SMS: Boost Mobile
PHONENUM@sms.mycricket.com # SMS: Cricket Wireless
$PHONENUM@messaging.nextel.com # SMS: Nextel (Sprint Nextel)
PHONENUM@messaging.sprintpcs.com # SMS: Sprint (Sprint Nextel)
PHONENUM@tmomail.net # SMS: T-Mobile
PHONENUM@vtext.com # SMS: Verizon
PHONENUM@vmobl.com # SMS: Virgin Mobile USA
PHONENUM@txt.bellmobility.ca # SMS: Bell Canada
PHONENUM@cwemail.com # SMS: Centennial Wireless
PHONENUM@csouth1.com # SMS: Cellular Sout
PHONENUM@gocbw.com # SMS: Cincinnati Bell
PHONENUM@mymetropcs.com # SMS: Metro PCS 1
PHONENUM@metropcs.sms.us # SMS: Metro PCS 2
PHONENUM@qwestmp.com # SMS: Quest
PHONENUM@pcs.rogers.com # SMS: Rogers
PHONENUM@tms.suncom.com # SMS: Suncom
PHONENUM@msg.telus.com # SMS: Telus
PHONENUM@email.uscc.net # SMS: U.S. Cellular
*)

– PART 2: runTransmissionScript.sh

#!/bin/bash
osascript ~/scripts/transmissionSendSMS.scpt “$TR_TORRENT_NAME”

–Where TR_TORRENT_NAME is a transmission builtin variable
–Note about osascript: It turns out you can pass variables from a shell script into applescript using the form:

on run argv
… item 1 of argv …
end run

–and then passing argv variable (TR_TORRENT_NAME) after calling the .scpt with osacript
– This is inspired by a transmission script that uses s-mail and an SMTP server for same result
https://github.com/transmission/transmission/blob/main/extras/send-email-when-torrent-done.sh
*)

I liked your idea of sending automatic notifications from Transmission when the download is complete. Thanks for that.

I can’t test, but I’m wondering if the following shell script (.sh) would work without the need to create a separate apple-script (.scpt) file:

osascript -e ‘function run(argv) {
tell application "Mail"
set sms to "Transmission finished downloading " & item 1 of argv & " !"
set savedDelimiters to AppleScript’s text item delimiters
set the clipboard to sms as string
set AppleScript’s text item delimiters to " "
set subjectline to the clipboard
set my_message to make new outgoing message with properties {subject:":white_check_mark::white_check_mark::white_check_mark:", content:subjectline, visible:false}
set AppleScript’s text item delimiters to savedDelimiters
tell my_message
make new to recipient at end of to recipients with properties {name:"5552352525@messaging.sprintpcs.com"}
send
end tell
end tell
}’ “$TR_TORRENT_NAME”

NOTE: Maybe, “:white_check_mark::white_check_mark::white_check_mark:” should be replaced with some ASCII equivalents to get working shell script.
NOTE 2: it isn’t clear (for me) why do you process sms content with clipboard.

osascript -e “function run(argv) {
set sms to "Transmission finished downloading " & item 1 of argv & " !"
tell application "Mail"
tell (make new outgoing message with properties {subject:"√ DONE!", content:sms, visible:false})
make new to recipient at end of to recipients with properties {name:"5552352525@messaging.sprintpcs.com"}
send
end tell
end tell}” “$TR_TORRENT_NAME”

Thank you for the concise summation. I was getting this syntax error with the shell script you posted:

$ bash transmissionSendSMS.sh
12:13: syntax error: Expected “given”, “with”, “without”, other parameter name, etc. but found “(”. (-2741)

So I tweaked the script removing unnecessary parentheses around argv. Also added a sender property and implemented property ‘address’ instead of ‘name.’

osascript -e “on run argv
set sms to "Transmission finished downloading " & item 1 of argv & " !"
set msg to ASCII character 195 & " DONE!”
tell application "Mail"
set newmsg to make new outgoing message with properties {sender:"youremail@icloud.com", subject:msg, content:sms, visible:false}
tell newmsg
make new to recipient at end of to recipients with properties {address:"5552352525@messaging.sprintpcs.com"}
end tell
send newmsg
end tell
end run" “$TR_TORRENT_NAME”

Minor Issue: Subject line should appear as “√ DONE!” but arrives as “Ã DONE!” (encoding changed from UTF8 to ISO-8859-1)

Major Issue: Despite successfully sending the email, the SMS Notification is now completely unreliable and often won’t appear for hours, in batches, or not at all. I tested several times, only 2 messages arrived after many hours, then a batch of many at once. I suspect this issue is the iCloud smtp server (somehow caching the message and getting lost or ???). I created a dummy contact for “5552352525@messaging.sprintpcs.com” thinking it would increase send priority, without a fix. Next step would be to set up a local SMTP server apply to Mail’s Outgoing Mail Account settings.

I’m not 100% sure, but setting sender for outgoing message is illegal and harmful in my opinion. For security purposes, this is also logical: whoever sends a message to someone is responsible for their actions.

That is, the Mail.app application should determine the sender itself and insert information into the outgoing message automatically.

As for the 1st issue, try set msg to (ASCII character 195 & " DONE!") as Unicode text