Getting value of most recent variable assignment, regardless of the variable’s name?

I’m writing a macro that uses AppleScript GUI scripting to scan for and return the x,y coordinates of an apps UI elements to a series of separate variable names.

As part of this macro, I’d also like to use the “Highlight Location” action to display visual feedback of the UI element that the macro is currently processing.

But because I’m returning the coordinates of the UI element to a unique variable name for each element, right now I’m manually referring to each separate variable name in each instance of the Highlight Location action.

Needless to say this is more tedious than I would prefer. Conceptually, I was hoping there was a way to reference the “last assigned” variable in a macro, regardless of that that variable was named. Then I wouldn’t have to manually update the variable reference in the Highlight Location action.

I’m sure there’s probably a much more elegant way overall in handling a repetitive workflow like this, and I’m sure my feature query is fraught with danger, so I’m open to other alternative approaches this this workflow.

Hi!

I am not fully sure that I am reading your request correctly, but could an approach that resembles something like what I’ve done bellow do anything for you?

Get value from last accessed variable.kmmacros (12 KB)
(v11.0.2)

It's a bit route-one, but why not simply have another variable -- Local_latestValue -- that you update at the same time as you collect your coordinates? If you want the coordinates as collected, just pass Local_latestValue the same coordinates. If you want more -- maybe the name of the variable or the current value after some post-collection transformations -- store the name of the last-updated variable instead and reference via that when you need to.

Demos of both approaches -- keep setting variables in the prompt until you're bored, on the last one click "Finish", see the results. First, storing the value:

Latest Variable Value Test.kmmacros (6.0 KB)

Image

Second, storing the variable name and referencing it later:

Latest Variable By Name Test.kmmacros (7.4 KB)

Summary

I'm sure there's a better way to reference the variable in the second macro, but I'm a bit rusty with KM. Anyone?

1 Like

Ah, great idea! I knew I was overthinking this. Also never occurred to me to use variable variable names for something like this as well. I'll give this a shot, thanks!