Tuesday, October 14, 2014


Learning AppleScript

After many trial and errors, managed to get my first AppleScript working. It moves messages from Outlook Inbox folder which belong to specific "category" to another folder, but only if these messages are more than 24 hours old.

(So the goal is that certain messages sent by various automated tools would appear in Inbox but would be automatically moved to a dedicated folder the next day)

Here it is:

tell current application
set today to current date
end tell
tell application "Microsoft Outlook"
set tainbox to mail folder "Inbox" of exchange account "Tripadvisor"
set viprbt to mail folder "VIP Robots" of exchange account "Tripadvisor"
set yesterday to today - 172800
set ctgrob to category "Robots"
set _messages to every incoming message of tainbox whose time received < yesterday
# set subjs to {}
repeat with msg in _messages
# if categories of msg contains {ctgrob} then copy msg's subject to the end of subjs
if categories of msg contains {ctgrob} then move msg to viprbt
end repeat
# subjs

end tell

The biggest issues I encountered are:

  • When using "contains", totally counter-intuitively RHS has to be a list;
  • Operator 'whose' might or might not work with specific predicate, you never know; if it does;t, you have to iterate manually, which is much slower;
  • You code might not work with complicate expressions until you'll make it simple enough by saving intermediate results using "set". E.g. the code above would't work if I tried to avoid using _messages:
repeat with msg in (every incoming message of tainbox whose time received < yesterday)

This page is powered by Blogger. Isn't yours?