I’ve set up SMS Server Tools on my Mac Mini so that I can send text messages from my PC and I thought someone might be able to use the Applescript I’ve created or even suggest a better way of doing things.
If you’re not familiar with SMS Server Tools it’s a really great utility for sending text messages from your PC (I use the term generically) it works on (almost?) all UNIXs and Windows too. It works by having folders which it monitors for text message files and then sending them via the phone. It was very easy to set up just download the source, make, make install, edit the config file and away you go.
I set it up so I could send myself text alerts when I get new emails. I did have it set up last year on my Gentoo machine and I used a bash script to process mail passed from Kmail. This time I’m using my Mac Mini and OS X with Apple’s Mail and a bit of applescript.
The biggest difference between the way Kmail and Mac Mail does things is that Kmail would run the bash script each time a message matched a given rule. Mail passes a collection of messages to the applescript that match the rule.
SMS File Format
The SMS files are just plain text files with unique file names;
Message Text goes here
Notice how the phone number you’re sending the message to is international format without the leading + and that there is a blank line inbetween the number and the message text.
This is the applescript I’m using to send the messages. I don’t have much experience with Applescript so anyone who can see a better way of doing things (especially the writing of the file) then I’d appreciate some feedback.
using terms from application “Mail”
on perform mail action with messages theMessages
repeat with theMessage in theMessages — loop through the messages sent by Mail
–Extract the desired details
set theSubject to subject of theMessage
set theSender to extract address from (sender of theMessage)
set MessageRecipients to get recipients of theMessage
set firstRecipient to item 1 of MessageRecipients
set toAddress to ((address of firstRecipient) as string)
–Create the randomly named file
set dir_prefix to “~/sms/”
set the_file to do shell script “mktemp ” & dir_prefix & “tempfiles/XXXXXX”
–Output the message
do shell script “echo To: 447xxxxxxxxx > ” & the_file
do shell script “echo >> ” & the_file
do shell script “echo From: ” & quoted form of theSender & ” >> ” & the_file
do shell script “echo To: ” & quoted form of toAddress & ” >> ” & the_file
do shell script “echo Subject: ” & quoted form of theSubject & ” >> ” & the_file
–Move the text message to the spool directory
do shell script “mv ” & the_file & ” ” & dir_prefix & “outgoing/”
end perform mail action with messages
end using terms from
You can download the script from here. You will need to change the directory variables to suit your setup and you’ll need to replace the mobile number with the one you wish to send the messages to. I decided to edit my /etc/smsd.conf to define the spool folders to be in my home directory.