Redesigned: Convert Local Variables to Global Variables and Vice Versa

Howdy Folks,

I have completely redesigned this macro from the ground up. It's not even really the same macro but since I already had a well-established topic on it I will just update it here. The original post and macro information can be found at the very bottom of this post.

This macro is designed to replace an action(s)' global variables with local variables and vice versa. Click on each section below to read more about it, to see the screen recording and also a screenshot of the macro.

Screen recording (click to expand/collapse)

Local and debug variable toggle macro

RELEASE NOTES, INSTRUCTIONS AND DISCLAIMER

Purpose of this macro (click to expand/collapse)

Convert global variables to local and vice versa.

How this macro works (click to expand/collapse)

First, select whichever action(s) that contain variables you would like to convert, then trigger the macro.
By default, this can be done by the menu bar group icon or by hotkeys.
The action(s) you have selected are the copied to the system clipboard as XML.

Then, supposing you are converting from local to global, the clipboard is searched for your local variable prefix and replaces it with your global variable prefix (or vice versa)
By default, the prefixes are local__ for local variables and debug__ for global variables since that is the naming convention I use while building/editing/debugging.
If you use a different naming convention, you will need to change those portions of this macro to match whatever you use.

Once the prefixes have been converted, the updated XML is pasted into KM which automatically pastes it as the same actions but with the updated variable prefixes.
If you selected to delete the old actions, then they are deleted before pasting; otherwise the new actions are pasted below the originals.

How to configure this macro (click to expand/collapse)

Indicate in the purple-colored variable whether you would like the selected action(s) deleted by default by putting Yes or No (Yes is currently selected which will result in them being deleted).
If you run the macro via the prompt, you can also unselect the checkbox if you do not wish to delete the original action(s).

The default triggers are ⌃⇧⌘V and the group status menu.
If you trigger the hotkey without hold it down, they will be converted to local variables.
If you trigger the hotkey and hold it down for at least 0.2 seconds, they will be converted to global variables.
Obviously this can be modified to suit the end-user's needs.

How to use this macro (click to expand/collapse)

Select the action(s) that contain the variables you wish to convert, and trigger the macro.
If you trigger it using hotkeys, it will run instantly.
If you trigger it via any other method, it will show a prompt asking you what you would like to do.

Disclaimer (click to expand/collapse)

TL;DR: I built it and it works for me. You can share/modify it how you want. But don't blame me if it doesn’t work for you :laughing:

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

-Chris

CURRENT VERSION: 2.0.1 (Wednesday, February 16, 2022)

v2.0.1 Wednesday, February 16, 2022
Added notification if action(s) contain a display text in a window action since it's XML is encoded and cannot be converted.

Download: 23)[AS-KM] Variables- Convert Debugging and Local.kmmacros (43 KB)


Macro screenshot (click to expand/collapse)

Original Post Information and Macro (from November, 2021 - click to expand/collapse)

Howdy folks,

If you're like me, when you're building a macro you may like to use global variables to be able to see their value in real time to make sure everything works properly. However, you may want to convert those to local variables when you're done building the macro, or vice versa if you have to edit an already built macro.

This macro is not magic in that it will not automatically convert all those variables with no user input, but it will somewhat reduce the time it takes for you to convert them. For me, the longest part of this process was simply deleting the prefix I would use for my debug variables and typing local_, and I got tired of mistyping and not realizing it until the macro stopped working, and then having to track down that one variable that was called locl_iHateTypos instead of local_iHateTypos. :laughing:

All you have to do is click in the field where your variable is, and trigger this macro. It copies the text to the clipboard, and then uses a simple search and replace feature to replace local_ with debug_ and vice versa. The same macro accomplishes both purposes depending on if you continue to hold the modifier keys after triggering it or not (or your user prompt selection if ran from the Group's Menu Bar). More information on this in the macro comments.

I have used this already in a variety of macros and it seems to work fine for me. However, use with caution the first few times you use it because...well, you just never know haha. Let me know if you have any issues, questions or suggestions!

-Chris

Variables- Convert Debugging to Local (for export).kmmacros (15 KB)

Previous versions (click to expand/collapse)

v2.0.0: Tuesday, February 15, 2022
Complete redesign
NOTE: This will not convert variables that are encoded as formatted text (such as in a display text in a window action)

v1.0.0: November, 2021
Initial macro

2 Likes

Version 2.0.0 is now up... it is a complete redesign. See post 1 for the details.

You have an editing issue in the "How this macro works" section. Just read the first paragraph and you'll see what I mean.

1 Like

Haha thanks Dan. Just goes to show that you can never do too much proofreading.

1 Like

Boy, isn't that the truth!

This is great! I've modified it a bit for my purposes and it's saving me quite a bit of time, and it pairs nicely with @DanThomas's KMET: Edit KM Objects as Text (Search & Replace) Version 1.2. Thank you!

Glad you’re enjoying it and finding it useful! Feel free to reach out if you have any questions or ideas on how it may be improved. :wink:

-Chris

1 Like

The comment action is triggering your "display text in a window" warning because comment actions also have <key>StyledText</key>.

I have modified the action to watch for <string>DisplayWindow</string> instead which appears to have fixed it *for my purposes.

1 Like

Good point! I had considered doing that part differently but ultimately with the <key>StyledText</key> parameter to ensure it catches any encoded text, not just that in a display window.

For instance, you might have an “insert styled text by pasting” action that you also want to catch. Or in comments themselves, you might reference variables that would need to be modified. So I’d rather be alerted to any of those instances just in case. :wink:

1 Like

I see! Thank you. I have edited my last reply and will keep it this way for now; I leave myself so many comment actions as breadcrumbs for forgetfulness! lol. Thanks again

1 Like