Length of text on clipboard is not calculated

I'm trying to let my macro insert a double quote when no selection exists. I've tried several techniques but I cannot get it to work ...

When a selection exists, the selection is correctly surrounded by double quotes. But when no text is selected, no single double quote is inserted.

08)Surround selection with ".kmmacros (4.9 KB)

The problem is that when nothing is selected the first Copy action will fail either aborting the Macro or, (if set not to Abort) leaving the Clipboard with whatever was in it before the Macro was run.

I've modified your Macro to the below, adding a test to see if the menu item Copy is enabled.

08)Surround selection with ".kmmacros (4.9 KB)


On testing I found that the calculation test CHARACTERS (%SystemClipboard%) made no difference. I think it is there to test whether the clipboard contains text or something else, like an image? The problem is that even it the clipboard contains an image it returns a count of "1" which means it is the same if you selected a single letter. So, assuming you are using this just for text, your macro can be further simplified to:

08)Surround selection with " v2.00.kmmacros (3.6 KB)


Thank you. Alas my app doesn't have a menu item "Copy" ...

For now I have added one extra item to my button bar, just to insert one single " :frowning: :

The key to making this work is the CLIPBOARDSEED() function. This function simply returns a unique integer whenever the clipboard changes. Here's the fairly simple macro built using that function:

Download Macro(s): Insert a quote.kmmacros (6.7 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

The macro first records the current clipboard seed, tries the copy command, then checks to see if the clipboard seed value changed. If it did, something was copied; if it didn't, nothing was copied, i.e. there wasn't anything selected.

If nothing was copied, the macro inserts a quote. If something was copied, the macro first makes sure it was text, and if so, wraps the copied text in quotes.

The only tricky bit is actually in the Copy command: If you try to run Copy and no Copy item exists (i.e. the menu item isn't active), the Copy command will, by default, keep trying to run until it times out. If you click the gear icon for the Copy command in this macro, you'll see that both Timeout Aborts Macro and Notify on Timeout are disabled. Further, and more important, Set Action Timeout is set to only five hundredths of a second:

This is to make the macro run much faster—if it tries Copy and it fails, it gives up after only five hundredths of a second. You may need to increase this number depending on the speed of your Mac and the app in question. You want to make it as low as possible, but high enough such that when you run the macro with text selected, it works.

By using a low timeout value and disabling the default abort/notify actions, the macro continues on very quickly when you don't have anything selected.

I've tested this in TextEdit and it seems to work perfectly.


1 Like

Does the Keyboard Maestro Action "Copy" still work in the App? If so, the below might work:

08)Surround selection with " v3.00.kmmacros (3.6 KB)

1 Like

Nah -- the key to this is that the "Copy" action will eventually error out if nothing is selected. The ideal time to wheel out the "Try/Catch" action!

Quote Selected or Insert Quote.kmmacros (2.5 KB)

@ALYB -- as with @griffman's macro, adjust the "Copy" action's timeout so it works with your setup.


Excellent! Four actions total, can't get much more elegant than that!