Using variables

I'd like some advice on using variables/clipboards. I just got a macro working, but it seems awkward so I'd appreciate any pointers on how to do things better.

This macro determines the longest line in whatever text is selected. I ended up using 3 variables and 2 clipboards. Is this just how KM macros are, or is there a better way?
longest line

longest line.kmmacros (8.4 KB)

Also, to upload I put the macro into a disabled group -- I think that's the way it should be done, but I'm not sure.

There's always a different way, sometimes a better way. But, so long as it gets you the right result, the best way is the way that you can easily understand -- because it's you who will need to make changes later!

And as what you easily understand will change as you get more experience with KM, your macros will change too.

Here's another way of doing "Longest Line" -- two variables and only the System clipboard:

Longest Line.kmmacros (4.6 KB)

1 Like

The easiest way to do it is to hit the Share button in the toolbar—it will do all the work for you, including disabling the macro. Or if you want to get fancier, you can install a macro to post in a really nice manner. In fact, that's what I used for the macro below.

As for your macro, you will need some number of variables, but you shouldn't need any clipboards. Here's one solution that works:

Download Macro(s): Find longest line.kmmacros (7.2 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

It runs on a variable for testing, but you can switch it over to the clipboard easily, as shown in the macro.

-rob.

1 Like

If you post here often enough, you'll find that there are roughly 15,235 ways to solve any given problem, as evidenced by @Nige_S and my solutions above :).

-rob.

I haven't downloaded your macro but from the screenshot I can ask: why are you using a named clipboard Temporary at all? Instead, you can just use the Set Variable action to set a variable called localTemporary to %Variable%localClipboardLine%.

Named clipboards are good for all kinds of items (images, formatted text etc) and they persist like global variables do, but you seem to be using just plain text so a run-of-the-mill variable will do nicely for that.

If I loaded your macro onto my Mac maybe I'd see a reason to use named clipboards - but I suspect not :wink:

Just to prove the point -- here's a version with <cough> no variables and <cough, cough> only one action:

Longest Line (Shell).kmmacros (1.7 KB)

The most concise so far -- and, probably, the least understandable and least useable!

1 Like

Wow, it will take me a while to digest all of these posts.

Much thanks!