Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Criteria for If statement in AppleScript

Tags:

applescript

I'm attempting to modify an applescript that fires a growl notification when there is a new message in Outlook. The original script is here.

In my if statement, I am trying to say that if the folder is either Deleted Items, Junk E-mail or Sent Items, don't fire the notification.

Here is the statement:

if folder of theMsg is "Junk E-mail" or "Deleted Items" or "Sent Items" then
    set notify to false
else
    set notify to true
end if

It appear that applescript does not like the multiple is/or items I've added. Is there a way to include the multiple criteria, or do I need to write a nested if/then?

like image 437
mikebmassey Avatar asked May 09 '12 18:05

mikebmassey


3 Answers

The correct way to chain if conditions in AppleScript is to repeat the full conditional:

if folder of theMsg is "A" or folder of theMsg is "B" or folder of theMsg is "C" then

– there is no implicit repetition of the left hand argument. A more elegant way to do this is to compare your left hand argument to a list of items:

if folder of theMsg is in {"A", "B", "C"} then

which has the same effect (note this relies on the implicit coercion of text to list, which, depending on your tell context, may fail. In that case, explicitly coerce your left, i.e. (folder of theMsg as list)).

like image 197
kopischke Avatar answered Oct 21 '22 13:10

kopischke


When including multiple criteria in your conditional statements, you must rewrite the entire conditional. This can be extremely tedious at times, but that's just the way AppleScript works. Your expression would become the following:

if folder of theMsg is "Junk E-mail" or folder of theMsg is "Deleted Items" or folder of theMsg is "Sent Items" then
    set notify to false
else
    set notify to true
end if

There is a workaround, though. You can initialize all of your criteria into a list and see if your list contains a match:

set the criteria to {"A","B","C"}
if something is in the criteria then do_something()
like image 1
fireshadow52 Avatar answered Oct 21 '22 13:10

fireshadow52


Having come accross this post by Googling "applescript if multiple conditions" and didn't came accross the code snippet I was expecting here is what's I've done (just for informative purposes) :

You could also recursively scan multiple conditions. The following example is : — Looking if the sender e-mail address contains (Arg 1.1) something (Arg 2.1.1 and 2.1.2) to immediately stop the script and "notify" => true (Arg 3.1). — Looking if the folder/mailbox (Arg 1.2) starts with "2012" (Arg 2.2.1) but is not folder 2012-A B or C (Arg 2.2.2) if it doesn't start with 2012 or is contained in one of the 3 folders stop and do nothing => false (Arg 3.2).

if _mc({"\"" & theSender & " \" contains", "\"" & (name of theFolder) & "\""}, {{"\"@me.com\"", "\"Tim\""}, {"starts with \"2012\"", "is not in {\"2012-A\", \"2012-B\", \"2012-C\"}"}}, {true, false}) then
    return "NOTIFY "
else
    return "DO NOTHING "
end if

-- multiple conditions comparaison via a shell script

on _mc(_args, _crits, _r)
    set i to 0
    repeat with _arg in _args
        set i to i + 1
        repeat with _crit in (item i of _crits)
            if (item i of _r) as text is equal to (do shell script "osascript -e '" & (_arg & " " & _crit) & "'") then
                return (item i of _r)
            end if
        end repeat
    end repeat
    return not (item i of _r)
end _mc

https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW3

like image 1
llange Avatar answered Oct 21 '22 12:10

llange