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.


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 :).


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!

I like this version for Find longest line, but I tried to use the macro for uploading posts to this forum and expected it to post here. Instead, it had me post a new topic which seems to contradict the macro notes. It also disabled the Find longest line macro

Anyway, regarding this Find longest line macro, I think I understand everything except the penultimate action to Set system clipboard to %Variable%Variable Name% -- I don't understand what that's for. I noticed that if I disabled it, the macro worked fine.

That can happen if you don't give it enough time to finish. What should happen is that the post opens, then everything in it is cut to the clipboard. At that point, you can either open a new topic and paste it in, or visit an existing topic and paste it in.

Whoops. I think my intent was to write your variable back to the clipboard, but it got left in generic form. If you wanted it on the clipboard, just change %Variable%Variable Name% to %Variable%local_theLine%. But not required at all unless that's what you want.