Email Flags

Trying to figure out how to do this-

In Apple Mail. Select an email. Hit F1. Have the Flag set to Red. Then have successive inputs of F1 rotate through Green, Blue, Unflagged and back to Red. I can do this in a few ways: with one macro enabling the next and disabling itself. Or with a variable tracking where in the loop it is.

But here’s the catch. When I switch to a new email, I want the macro to be able to tell what flag is already set or if it is not flagged yet so it picks up in the loop from where the new email is now. Not from where the previous flag operation left off.

This is quite doable with the inclusion of a few simple AppleScripts:

Set and Cycle Flags.kmmacros (13.4 KB)

Every time this macro is run, it executes an AppleScript that checks the unique ID of the currently selected message to see if it matches the ID of the message that was selected the last time the macro was run (which was also acquired with AppleScript). If the two IDs are the same (i.e. if the same message is still selected) then it uses AppleScript to query the current flag status as an integer (-1 is no flag, 0 is red, and so on) and set the next color flag appropriately, then queries the flag status again and sets it as a new variable, which we then use to store and set the flag color if the selected message is different the next time the macro is run.

Let me know if you run into any issues with the macro, and feel free to ask if you have any further questions!

Wow! Impressive. Works!

All best-
Mike

1 Like

So if we are able to read the flag status from an email, why the need store any variables? Just read it and increment it. No?

All best-
Mike

Glick-

I just lifted the Get Flag Index and Switch out and it works perfectly, just those two steps.

Now, how can I use a long press of F1 to clear the flag? Been a head scratcher.

Really appreciate your input!

All best-
Mike

Right you are. I misread your initial post and thought that you wanted to preserve flag color settings across emails rather than simply repeat the same cycle with every selected email, and the variables were necessary to do that. Sorry for any confusion!

Glad you were able to get it working how you wanted!

This can definitely be a head scratcher until you see how easily this can be done. Just add these two actions to the end of the macro:

For convenience, here's a new version of the macro that includes them:

Cycle Flags.kmmacros (10.5 KB)

This is a variation of a method @JMichaelTX detailed where the macro pauses for a short period, checks if the trigger hotkey is still down, and then performs another action if it is (clearing the flag, in this case). This version of the macro will cycle through a flag once before clearing the flag if F1 is long-pressed, which is less than ideal, but I found this to be a preferable trade-off compared to putting the pause and check at the beginning, since it makes the flag cycling much more responsive.

Happy to help!

You are a Jedi Master!

-Mike

1 Like

Man, your first version though, incrementing the group. That’s lofty stuff.

-Mike

1 Like

Hey Mike,

Here it is all in AppleScript.

NOTE — To get the properties to save you MUST save the AppleScript as a compiled script using the Applescript Editor.app and run it from Keyboard Maestro as a compiled script.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/11/10 22:30
# dMod: 2017/11/10 22:43
# Appl: Mail
# Task: Set selected email's flag to Red — increment though flags on successive activation of macro.
#     : Reset after moving to a new message.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @Macro @Set, @Selected, @Email, @Flag, @Red, @Increment, @Flags, @Successive, @Activation
------------------------------------------------------------------------------
property lastFlag : 0
property messageID : 0
------------------------------------------------------------------------------

tell application "Mail"
   set msg to item 1 of (get selection)
   
   tell msg
      
      if messageID ≠ id then
         
         set messageID to id
         set flag index to 0
         
      else
         
         if flag index = -1 then
            set flag index to 0
         else if flag index < 6 then
            set flag index to (flag index) + 1
         else
            set flag index to -1
         end if
         
      end if
   end tell
   
end tell

------------------------------------------------------------------------------
1 Like