How to check if text is selected in a macro?

Hi all

i have this macro where i use it to cut selected text and paste it as a MD checklist

this works well but i want to enhance the macro where if there is no text selected that i will be able to run the macro by cutting the whole line where the cursor is (again only if not text is selected)

is that possible at all? and if so how do i best approach this

thx so much

Z

The exact method will depend on the application, but have a look in the menu items for something that is only enabled when some text is selected. So in TextEdit you might check the "Cut" item and if it wasn't enabled (nothing selected) you'd select the whole line:

...although the test breaks if you have an image selected.

ahh very interesting @Nige_S ! thanks so much for this insight

Sadly, in my main app obsidian the menu seems to be enables with both text selected or cleared (dont know if this is an electron thing...). any other tricks like this :smiley:

best

Z

Then it may not matter -- in Obsidian, โŒ˜X generally cuts the selection or, if there is no selection, cuts the line the insertion point is in. Just what you want!

Is there a situation where that isn't working for you? If so, provide more details and somebody might be able to find a solution.

thx @Nige_S , not sure im following all the way, though i do want it to work outside of obsidian as well

this is basicly what im aiming at:

so if text is selected it will launch the macro commands on the selected text and if nothing is selected it will launch the macro commands the entire line (ie cut the text across the line first)

does that make sense?

thx!!

Z

Yes -- but since Obsidian already has you covered, regardless of selection, the above should still work.

  • If you are in Obsidian, "Cut" is always enabled -- the "If" action will "Do nothing"
  • If you aren't in Obsidian and "Cut" is enabled -- the "If" action will "Do nothing"
  • If you aren't in Obsidian and "Cut" is not enabled -- the "If" action will select the line

Whatever happens, the following "Type โŒ˜X" should cut either the selection or the line.

"The line" may or may not be what you want, though. It will (usually) be a line as seen in the UI, not a line as in "the text between two linefeeds". If you want "the whole paragraph" you'll have to try some different keystrokes in your apps, but most "standard" Mac apps should accept โŒƒA to go to the start of the paragraph and โ‡งโŒƒE to select from there to the end of the paragraph.

More potentially useful keyboard shortcuts here, or check your apps' documentation.

Chrome has the same problem with the cut & copy.

What I do is capture the CLIPBOARDSEED() value, try a copy and then check to see if the clipboard seed has changed.

1 Like

thx both!

@devoy , any chance you can share an exmaple of how you use the CLIPBOARDSEED() in your macros?

best

Z

The value returned by the CLIPBOARDSEED() function changes every time the System Clipboard changes. So:

  1. Put CLIPBOARDSEED() into a variable
  2. Do your Cut (or Copy)
  3. Test the current CLIPBOARDSEED() against the one you saved. If it's the same then the Clipboard wasn't changed -- you didn't Cut/Copy anything (or it's exactly the same as the last thing you Cut/Copied!)

thx so much again @Nige_S , really appreciate your kind help

this is cool to learn this method!

i created a macro (See below), which works 99% of the way. the only little thing remaining is that if there is a cut issues and the line is empty i want to execute a different set of commands. in obsidian seems like when i issue the โŒ˜+x, it still copies something and thus the CLIPBOARDSEED() seem to change

anyway around this?

here is the current macro based on your excellent input!

I'm back!

@Nige_S example nicely demonstrates what I was talking about. But..

While I was trying to set up an example for you I realized a simpler way:

Sorry, I don't have Obsidian so I'm not sure how to address the "nothing selected so cut the whole line" issue.

Can you check this? I'm seeing the opposite behaviour -- CLIPBOARDSEED() doesn't change and KM's %SystemClipboard% return the last-good value, even though the Clipboard is currently empty.

But if we test the Clipboard with AppleScript it returns the correct response. So if your version is doing as you describe, this might help:

Thx so much again @Nige_S !

i got to say i got super confused with the CLIPBOARDSEED() and the example you gave haha :slight_smile:

So based on your first comment i tried this

its kinda clunky but seems to work, do you think its a valid approach or will this break stuff?

thx a bunch

Z

If it works, it's valid!

You're doing a lot more than you need to, though. You've an unneeded extra "If" (if Obsidian isn't not at the front it must be at the front, no need to test again) and the same three actions in both branches of your "If" action -- the only difference between "Obsidian not at front, so execute...." and "otherwise execute..." are the two keystroke actions (the first of which has an unnecessary โ‡ง in it).

So you can shorten it all to:

The advantage (besides better readability) is that if you want to change what is done between the Cut and Paste actions -- a different regex, or maybe some more transformations -- you only have to make the changes in one place. The disadvantage is that, being outside the "If" statement, the transformations will be applied whatever app is frontmost -- if you might want to do some things in most apps but some different things in Obsidian, leave it as it is.