Same named vars in different macros do not get set differently when run on System Wake Up

I am writing several macros to download my bills and bank/investment statements via Safari. I only want to run them once on the day the bill/statement drops. I was thinking of doing a Crono, but I can't be sure when the computer will be awake and I don't want it to run over and over again by using hours like 0-23. In any event, that's a different issue which doesn't impact this question.

I decide to use the Wake trigger and test if today is the day of the month the bill/statement drops ("My_Date"). (To run it only once, if it is the correct day I then test if a file exists with the bill/statement name, and if so, cancel the macro). To speed up the process, and for consistency, I use the same first set of actions. If I run them manually they all work. However, what I'm finding is that when I wake up my Mac from sleeping they all run, but they are not stepping through each macro to give the common named vars their unique values. I'll put in a screen shot below, but for example I have a var called "My_Day" which is the numerical day of the month I want to run it on. It is used to calculate "My_Date" which is made up of This_Month/My_Day/This_Year. Likewise, I have a variable called "Company_Name". Even though I set My_Day and Company_Name to a value in each macro, and then concatenate My_Date in each macro as described above, when the Mac wakes up it is using the same My_Day, My_Date, Company_Name, etc., in each macro. For example, even though Macro 1 sets My_Day to "1" and Macro 2 sets My_Day to 2, My_Day is not being changed to 1 and 2 for each macro when they run at system wake up. Likewise, "Company_Name" isn't getting changed to each macro's unique value. I'm thinking this Is this some sort of sync issue where KM isn't running all the way through each macro. Other than having to go back through each macro and give unique names for all these names (and there are several more like URL, Save_Name, Target_Dir, Full_Name, etc.) is there any way to get KM to step through each one from start to finish independently? If I have to rename each variable, I saw a "Find" but not a "Find and Replace" (I'm using version 8.2.4). Is there a Search and Replace so I could change "My_Date" to "AAA_My_Date" throughout a macro?

So, I came up with a kludge solution, which in this case may actually be better than what I was doing, but didn't provide an answer on why the same named vars in different macros weren't being set properly.

What I did is to create a master macro that set the basic Todays_Date, This_Month, This_Year dates variables. I then put in a Switch of Variable action in which I said If Todays_Date matches %Variable%This_Month%/[Num Here]/%Variable%This_Year% then run the macro(s). Where [Num Here] is the day of the month I want one or more of the macros to run, e.g. for the first it is: %Variable%This_Month%/1/%Variable%This_Year%

Before I did all of that I did a quick test of making two macros that put the Start time and my "Company_Name" var in a window where I set Company_Name to a different value in each macro. I also had a window pop at at the end of each macro with the then current time. Those tests verified that the first macro completed before the 2nd one started and that my var Company_Name was being changed to the correct value in each one.

While that lets me schedule all these, does anyone know why the vars were not getting set properly as described in my first post?

Since you’re using global variables, setting the value of My_Day to 1 in one macro means it has that same value i.e. 1, in all your macros and this evidently is confusing all your macros because they are each trying to set a value independently of all the other macros.

I suggest you use local variables instead, so change “My_Day” to “Local_My_Day” in all your macros (do this for all your variables, not just My_Day). The value of a local variable is available only to the macro in which it appears so that each of your macros can set its value without affecting its value in your other macros.

I apologise if this is difficult to understand but it relies on the “scope” of a variable which is an important concept when it comes to programming. If you are familiar with this concept then you’ll have no trouble understanding my somewhat awkward explanation/suggestion.

By the way, I personally would stick with what you’ve called your kludge: using a switch is both simpler and more efficient an approach.

1 Like

Thank you for your reply. I do know about scope, I just didn't know adding "Local_" to the front of a variable would do that. But, as we both concluded, using the Switch routine is a much better solution as then only the Master macro runs at wake up and then runs the macros sequentially if more than one is to be run on a particular day. Plus, it makes for less code and helps me see much easier what is to be run on a particular day.

1 Like

See KM Wiki Variables .