MacBook not reaching remote servers when away from desk

Yeah I make extensive use of the branch by %MacUUID% trick, for things like killing apps on some machines but not others. In fact, all machines load some global variables each day that, among other things, define machine names like “Matt”, so I can just say If %MacUUID% = Matt type stuff.

While I have you experts here :slight_smile: there’s something else I ran into the other day. I tried to create a variable using a variable, while looping through all machine names:

ThisMachineShortName = %ThisMachine%_short

If I did an alert on ThisMachineShortName, it would display something like matt_short, so I thought things were working well.

But later when trying to compare stuff like, If %ThisMachineShortName% = matt_short the macro would fail.

Any idea what I was doing wrong? Thanks again very much for all the help!

From your description it should work, but clearly something's wrong.

Create a minimal macro to demonstrate the issue and post it here -- I'm sure someone will be able to help.

A sample is attached! :slight_smile:

Variable_from_Variable.kmmacros (2.8 KB)

At no stage in that macro do you set the variable ThisLongName to a value, so the "If..." Condition is likely to be false.

Note the italics in that sentence. Because you've used Global variables you may have set that variable to "MattsMBP_matt" outside of this macro, so it might be true!

Other than that, I'm not sure what your logic is here.

...puts the literal "MattsMBP" into the variable ThisShortName.

...first decides the variable to set by evaluating the text token %ThisShortName% and appending "_temp" to the result -- "MattsMBP_temp". It then evaluates %ThisShortName% and appends "_matt" to get the string "MattsMBP_matt". So it's putting "MattsMBP_matt" into the variable MattsMBP_temp.

So, if anything, your "If..." should be something like

...or even

If you haven't already, try stepping through your demo macro in KM's Debugger -- at the end you'll see what's going on:

Sorry about that; I just created the script in a rush to try to demonstrate the following. (In my production script the variable does have a value.)

The above is the actual problem I’m having—i.e. it seems like something like %variable_name%_temp can not be used in an if/then/else comparison. It also shows as red in my own script, and I’ve tested that %variable_name%_temp does evaluate to something, i.e. if I display that, it shows something like MattsMBP_temp.

Yes, that shows it's "illegal" and cannot be used -- that value isn't even saved, and if you run the macro it will execute using the previous (valid) field value.

You can do some clever stuff with variable "indirection" (see How to Use Dynamic Variable Names) but this isn't one of them.

You can usually get round this by refactoring to use a different approach, but if you simply must use indirection in an "If..." you'll need to go the "Filter to a temp var" approach as shown on that linked Wiki page. See actions 3 and 4 in this demo:

Indirection in If.kmmacros (5.9 KB)

Image

Thanks again for the reply! Variable indirection sounds a bit above my pay grade :wink: and like you suggested, usually in these situations I get through them by changing approaches. So that’s what I’ll try here!

People often run into this when they want to store multiple, labelled, versions of a similar thing. An example might be IP addresses when they might think to use the variables:

Global_server_1_IP
Global_server_2_IP
Global_server_3_IP

...and later access them with Global_server_%variable%_IP.

KM has Dictionaries for just this sort of key/value storage, or look at using JSON for more complicated structured data.