Because this post has been so successful I’m reposting it here.
Don’t ask me why but for a fixed period (one month, each day at 14:30 GMT+1) I needed to send SMS to a fixed number. The text had to be choosen by random from a set of texts. I have no SMS gateway provider account so I searched for a simple solution. The simplest one is to create a Python script that reads the text from a file and then calls an Applescript that triggers Skype.
The Python script (dumb, no magic):
FILENAME = ‘texts.txt’
CHECKER = ‘checker.txt’
PROGRAM = ‘/usr/bin/osascript’
ARG = “‘sendsms.scpt’”
if __name__ == ‘__main__’:
# compute already sent sms
# it is one line with all line numbers already sent
checkfile = open(CHECKER, ‘r’)
fline = checkfile.readline()
already = 
already = [int(al.strip()) for al in fline.split(',')]
textfile = codecs.open(FILENAME, ‘r’, ‘utf-8′)
count = 0; lines = ; lastline = ”
for textline in textfile.readlines():
if not (count in already):
count += 1
lastline = textline
# no more data found so we take the last text and start from the beginning
if lines == :
already = 
ourcount, ourline = random.choice(lines)
checkfile = open(CHECKER, ‘w’)
checkfile.write(‘,’.join([str(inp) for inp in already]))
line = ourline.encode(‘macroman’).replace(‘\n’, ”).strip()
os.system(‘%s %s “%s”‘ % (PROGRAM, ARG, line))
You absolutely need to encode your texts into MacRoman because Applescript only understands that encoding.
Here the AppleScript:
on run argv set smstext to item 1 of argv as Unicode text tell application "Skype" set message to send command "CREATE SMS OUTGOING +49..." script name "SMS" set smsid to item 2 of every word of message send command "SET SMS " & smsid & " BODY " & smstext script name "SMS" set result to send command "ALTER SMS " & smsid & " SEND" script name "SMS" display dialog "SMS send! Text: " & smstext end tell end run
Now you can include the Python script into your crontab. Enjoy.