Improving on the Text Reference Macro - Test if Clb is empty Macro (v11.0.4)

Question

Why does the Test Macro (is empty?) not show the KM Display window, when nothing has been copied and the Current Clipboard still has a string ("add")?

This test is necessary for constructing a macro with 2 braches:
A: Get DocTTL, PATH, and some TEXT of a file → Clipboard (this macro exists already)
B: Get DocTTL, PATH, no TEXT → Clipboard

First to test if something has been copied, and here 1 branch doesn't return anything when CurrentClipboard still has a string: "add"

WriteClb3 Test if Clb is empty.kmmacros (28 KB)

This is the plan for the macro

The Test macro is from this page:

Because you can't Copy when nothing is selected.

The default "Copy to Named Clipboard" Action has a 10 second timeout and will abort the macro if nothing can be Copied.

You should be getting a Notification telling you that has happened, similar to:

...but with the names of your macro and Named Clipboard substituted. KM's Notifications are invaluable when debugging -- you should turn them on and pay attention to them.

Note that you are not testing for an empty clipboard but for a clipboard that doesn't contain text -- but it might have an image. That may not matter here, but watch out for it.

Similar Notification here:

However, the main problem with the macro not branching, is that

when nothing (i.e. no text) has been selected (because a copy is not needed in this case),

the macro will "expect" the %Variable%VarBODY% (as something like "0", but it only gets "nothing", and then it will fail.

This is the problem, and I don't know how to solve it.

/okn

WriteClb4 Title and Path of Text File ⇢ Clb with TEXT.kmmacros (28 KB)

No.

The main problem is that the "Copy to Named Clipboard" errors when nothing is selected and, by default, the macro aborts.

Once you understand that you know how to handle it -- catching errors is what the "Try/Catch" Action is made for:

No Copy demo.kmmacros (6.4 KB)

You'll want to play with the "Copy to Named Clipboard" Action's "Timeout" setting so that it is long enough to allow a successful Copy in your slowest-to-respond apps but not so long it gets annoying waiting for your macro to complete.

I've added a couple of Actions to show that the Named Clipboard is not set to empty when there is nothing to Copy -- its previous contents remain, unchanged.

The only reason to use a Named Clipboard in your macro above is if you want to use what you Copied in a different macro later. "Copy to Named Clipboard" is a two-part Action, Copying to the System Clipboard then copying the contents of System Clipboard to the Named Clipboard -- all Copy and Paste is done via the System Clipboard. So you might as well replace your "Copy to Named..." and "Set Variable..." Actions with:

Even better, use a Local variable because you don't have to reset it -- if it isn't set during the "Try" block it won't exist and will later evaluate to an empty string:

Remember to update your "Set System Clipboard" Action to use the Local variable!

OK, very interesting, also the ways to simplify unnecessary steps,

Do you mean I can use the ”Try executing Actions…” block instead of an "If" block?

    • this is being built as soon as possible :slight_smile:

/okn

In this case, yes -- your actual decision is based on "Did this Action successfully execute?", which is what the "Try/Catch" block is for.

The Reference Macro seems to be working alright!

BRANCH-A: everything incl. BODY (or TEXT)

BRANCH-B: DocPath & FileName, no BODY (or TEXT).

So far so very good,
Only it is maybe unexpected, that

BRANCH-A with BODY is very fast,
BRANCH-B however very slow, about 11 sec!

So the process to check if the %temporary Clipboard% exists,
and then at the beginning of BRANCH-B: "Delete past clipboard 1" takes some time.
Maybe after all, some kind of integration with the IF function would be faster?

/okn

WriteClb7 Title and Path of Text File ⇢ Clb with or wout TEXT (inverted).kmmacros (37 KB)

Well, yes...

If you tell the Action to wait 10 seconds before erroring out, your macro will wait 10 seconds before starting to execute branch B.

From earlier:

Yesterday, both alternatives / branches worked, however after 11sec.

So I reduced the timeout for temporaryClipboard tested with many values,
Branch-A is fine (all incl. BODY),
Branch-B, not

  • there is an immediate error pling

Then in the Display which shows up after a while

  • shows a former temporaryClipboard (for BODY, which it is supposed to not bother about.)

→So in Branch-A:
although there is no selection, and nothing can be copied,
and old text appears.

The "Delete Past Clipboard 1" action doesn't do much.

This is not easy - peasy.

/okn

It's easier if you don't over-complicate things and also always stick to Local variables unless you absolutely need either persistence or greater scope.

Here's your macro, simplified and working for TextEdit:

WriteClb7 Title and Path of Text File ⇢ Clb with or wout TEXT (inverted) DEMO.kmmacros (28.6 KB)

I say "...for TextEdit" because different apps behave in different ways when you try to Copy when nothing is selected -- Obsidian, for example, Copies the line the insertion point is in.

Hm,

Did you not maybe forget to put the actions

  • Get Front Document Path
  • Get File Name

into

  • otherwise execute the following actions

?

Hm,

Maybe you didn't even bother to try it?

Yepp;
I waited for the input, but now I'm so tired I need a rest, back very soon :_)

But when I think about it, it could work, it probably does too!

Debugging any macro would be something, somehow there is this way of executing a macro step by step and observe exactly what's happening.
But I'm not good at it, tried it once. I'll check it on YT…

/
best regards,
OmarKN

This macro only works with a selected text.
Did it work for you?

With text selected:

Without anything selected:

Then it's on my machine.
Tried it with TextEdit.
For the "without text" - branch there always this error sound/ bell at start.

Used the "Debugger Breakpoint This Macro" step-by-step: no problems.

Later today, I'll separate the 2 branches and see what happens…

Thank you so far.

/
best regards,
OmarKN

Yes, because if you tell an application to do something and it can't then it should return an error, making your computer beep at you.

If you don't want that then you'll need a different approach. The more usual way of doing what you want is to see if something can be Copied and proceed accordingly. For many apps you can do that by checking whether the Copy menu item is enabled:

WriteClb7 Title and Path of Text File ⇢ Clb with or wout TEXT (inverted) DEMO Menu Check.kmmacros (29.9 KB)

This version goes back to using a Named Clipboard, to keep any formatting in the text you Copy.

Yes, this version is the best,
extremly fast and precise
also with formatted text (optional).

Tested successfully with

  • TextEdit
  • TextSoap
  • Pages
  • BBEdit
    Tested unsuccessfully with
  • CotEditor*

*Have they not scripted their app? (to find the COPY command)

The new macro is awsome, and it will be uploaded to the KM favourites webpage as soon as possible.

/
best regards,
OmarKN