Day of Week Sets Variable

OK, I thought I had this figured out but, I guess not. I have a TaskPaper task list that I run every day. It has become such an important part of my work-flow that it is almost always on one of my screens. I can easily trigger it using my KM macro. I thought, just for grins, why not change the background color slightly to nudge me that it is the weekend, Monday, Friday, etc.? TaskPaper documents use stylesheets which have a similar code structure to using a CSS file - so, that is pretty simple to do. Any running TaskPaper document has a pull down menu to select the stylesheet name for that document.

My goal then, is to set up a variable that checks the date and it includes Mon or Tue or … etc. to set that variable to the stylesheet name that I want to use on that day. The stylesheet name is in the Taskpaper menu so it should work - IF - I am using this variable in the right way. It would seem that I am not.

Set Variable “VarDate” to Text
%ICUDateTime%EEE%

If All Conditions Met
The variable “VarDate” contains “mon”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightYellowTheme.less

If All Conditions Met
The variable “VarDate” contains “tue”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightYellowTheme.less

If All Conditions Met
The variable “VarDate” contains “wed”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightRedTheme.less

If All Conditions Met
The variable “VarDate” contains “thu”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightGreenTheme.less

If All Conditions Met
The variable “VarDate” contains “fri”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightBlueTheme.less

If All Conditions Met
The variable “VarDate” contains “sat”
Execute the Following Actions:
Set Variable “VarDate” to Text
LightTheme.less

If All Conditions Met
The variable “VarDate” contains “sun”
Execute the Following Actions:
Set Variable “VarDate” to Text
DarkTheme.less

--------------------------
Select Menu Item in the Front Application
Select: Window ⇢ StyleSheet ⇢ VarDate
Stop macro and notify on failure.
--------------------------

In this last step … if not using the macro, this is how the menu command would look:

--------------------------
Select Menu Item in the Front Application
Select: Window ⇢ StyleSheet ⇢ LightYellowTheme.less
Stop macro and notify on failure.
--------------------------

Macro continues with no relevant data to my question …

It works when the StyleSheet is named in text, but apparently not using the macro. Or, have I gotten the macro somehow, wrong? Insights are appreciated. Thanks.

PS: I checked using clipboard that I have the correct date string that contains the days of the week abbreviations and that the final macro has the correct StyleSheet names. They are all correct and so this should work. Except of course, that it doesn’t. I thought using a macro as text was as good as text?

UPDATED:

Changed my logic and got it working. Instead of setting a variable on the day of the week condition and using the variable at the end on a single menu selection choice, I put the menu selection choice as a result of each yes condition for that day of the week.

I will leave the question as I am still curious why a variable is not giving text results in my first example. Thanks.

If All Conditions Met
The variable “VarDate” contains “Sun”
Execute the Following Actions:
Select Menu Item in the Front Application
Select: Window ⇢ StyleSheet ⇢ LightTheme.less
Stop macro and notify on failure.

If All Conditions Met
The variable “VarDate” contains “Mon”
Execute the Following Actions:
Select Menu Item in the Front Application
Select: Window ⇢ StyleSheet ⇢ LightYellowTheme.less
Stop macro and notify on failure.

To answer your immediate question:

You can definitely use a variable in place of certain text in KM, and menu items are no exception. However, it looks like you may not have been using the correct syntax to denote that VarDate in you original macro was a variable, and not just the literal text "VarDate". To show what I mean, here's a quick sample macro that successfully uses a variable to select a menu item:

07 AM

When copied as text, the latter action is listed like so:

Select Menu Item in Keyboard Maestro
Select: Actions ⇢ %Variable%LocalTest%
Stop macro and notify on failure.

However, in your example, your similar action is written like this:

Select Menu Item in the Front Application
Select: Window ⇢ StyleSheet ⇢ VarDate
Stop macro and notify on failure.

In other words, the key variable does not appear to use the %% token and variable syntax that tells KM to substitute a token or variable with its contents. As long as you use the proper syntax, your original macro idea should work as expected.

That said, I think your second idea that you were able to make work is probably a better one that can also be made more efficient. For a multi-branching macro like this, rather than several If Then Else actions, I would recommend using a single Switch/Case action, like so:

TaskPaper Theme Selector.kmactions (4.5 KB)
21 AM

You'll notice that rather than set a variable to a date token, we can just use a date token directly by testing a Text condition. If you wanted to stick with your original %ICUDateTime%EEE% formatting, you can just as easily use that without the intermediate variable step:

36 AM

I'd also be remiss if I didn't note that KM includes a myriad of date related functions, one of which is explicitly called DOW(). It returns 1-7 based on the current day of the week, and can be used in a Switch/Case like this:

20 AM

While setting up the macro in one of these ways certainly isn't required if you have something that works, I do think one like this would both be more efficient and easier to maintain and change (say, if you ever wanted to start using a different theme on a given day of the week).

1 Like

Thanks for the explanation. I was using the syntax incorrectly, good to know. I have not heard of “Switch/Case action” before. From your example, it appears to be some self contained version or extension of the if/then/else function. I am not understanding when or why this is meant to substitute for a standard if/then/else function? I will have to read up a bit on this function.

ADDED: After using your suggestion, it looks like using switch allows multiple things to be done after a single if/then or switch has been acknowledged. This is going to be very useful. Thanks again.

1 Like

If you have only two conditions to branch on, then a IF/THEN is a good choice.
But if you have more than two, then a Switch action is usually better to use than a bunch of nested IF/THEN Actions.

A Switch Action allow you to examine the value of something (like a Variable), and then branch to ONE of many choices based on that value.

For more info, see Switch statement - Wikipedia.

1 Like