UI Selection

Not answered :wink:

Not really.

-Chris

Other that testing whether the Copy menu is enabled (which may or may not work depending on the application) or testing for a selection via AppleScript (which will rarely work and depends on the application), no.

The Copy action waits for the clipboard to change. To essentially it is a Pause Until clipboard changes.

Not that I can see.

@ccstone, @peternlewis Thank you.

As I (believe) understand each application knows only about its own selection.

KM can only use indirect information about selection in other application but itself: so I will restrict use of selection to KM :joy:

–Alain

Hey Alain,

Right. Unless the app in question publishes information about its selection.

tell application "BBEdit"
  tell front text document's text
    set mySelection to the selection
    set selPropList to properties of the selection
  end tell
end tell

Not many apps offer this ability, and it is not universal either – it cannot be used in various fields and dialogs for instance.

-Chris

The free sister application “TextWrangler” also gives information about its selection: potentially useful, as it is my universal editor :wink:

Thanks Christopher for your script.

–Alain

Alain, are you having problems using my technique of setting the clipboard to "[NONE]" first, and testing after a ⌘C? I have found that it works find in all apps.

1 Like

General information and all experienced users advise about a main feature as selection is useful (needed).

JMTX in your universal solution I just worried about the completeness of the clipboard load for large selection with a Pause of (arbitrary) 0.2 seconds.

So my tentative (synthesis) solution, taking the best of each worlds is finally:

With this code the arbitrary duration Pause can be anyway very small and independent of selection size...

-Alain

Good point, and your solution looks reasonable to me.

I have found for 99% of my use cases a 0.2 sec pause is succificent, but since these KM Actions are in each Macro, I have sometimes tailored it to the specific use for that macro, if I am expecting, or want to allow for, a large selection.

My primary use case is to make the macro work with, or without a selection, making it easier for the user to use.
EXAMPLE:

  • I have a Styles macro for Evernote, which will either apply the chosen style to the selection, or offer to auto-select one of the following AFTER the current cursor location:
    • Word
    • Line
    • Paragraph

So, in all of these cases, the size of the selection is small/modest.

What drove me to use ⌘C vs Copy action is the long wait/timeout of Copy. But your solution deals with that nicely. Knowing that the KM Copy does a pause until clipboard changes helps a lot.

1 Like

A post was split to a new topic: Copy with Selection Test [Sub-Macro]

All this is very interesting, but I am missing something basic: if there is no selection, Copy fails and the macro is exited. Seems to me that what’s needed is the equivalent of a “try” around the Copy. I don’t understand how anyone is getting any results from Copy when there is no selection.

Can’t AppleScript it because applications don’t necessarily publish their selection. I am trying to tell if there’s a selection in a TextEdit document.

Yep, that's the point -- to know that in fact the user has NOT made a selection, when the calling macro requires a selection to process.

I've used this technique many times to make a default selection if the user has made none. For example, I have a google search macro that if there is no selection, assumes the cursor is at the beginning of a line of text to be selected and used in the search.

There is a "Try" around Copy.

If the copy does not happen, then the action will time out and the ActionResult token will contain the results. You can control the length of the timeout and the timeout behaviour in the Gear menu.

Thanks for the info, Peter.

I just ran a little test, and I may have to change my approach for testing for selection. I think using a Copy Action timeout of 2 sec should be adequate in most cases, and then just test the %ActionResult% for ≠ "OK" (then no selection was made).

###Copy Action Settings

1 Like

OK, thanks. But I still get a sound alert when I run a macro the macro tries to Copy. I am in desperate need of macro that will return an empty string if there is no selection, without any sound alert.

My external speakers connected to my Samsung 27" monitor, connected to my MacBook Pro by way of a Thunderbolt-DVI cable does not respond to volume or muting controls. And even if it did, Toggle System Sound Mute is not right because the user may have mute on.

It appears that KM offers no way to read the system sound volume or set it to a specific value.

Will have to look to see if there is an AppleScript or ObjC way to (a) read the current volume setting. Worse comes to worse I think I can do this on the command line. Then I can save the volume, set it to 0, Copy, and restore the volume. This is pretty ugly, but it would work.

But none of that will turn the sound off when I am using my external monitor.

I need the Copy because I want to copy, modify the clipboard, and paste, In particular to “Apply Style to Named Clipboard”.

The only option for that is to use the Menu condition, and that will only work in some (most normal) applications. OS X simply does not offer any other alternatives.

SYSTEMVOLUME() function.
Set System Output Volume action.

Unfortunately, you cannot use a calculation in the Set System Output Volume action, so to actually do it would be an interesting challenge (it can be done using XML, but it is difficult). There is also the option of using AppleScript for this which would be relatively straight forward. But you said "does not respond to volume or muting controls" so there may not be any point in that.

Is the problem false negatives (Copy dimmed even though there is a selection) or false positives (Copy not dimmed even though there is no selection)?

You are our application guru, since you have seen all and done all regarding state in OS X :relieved:.

You get a sound ONLY if no selection was made by the user.
IMO, this should rarely be the case, so it's not worth spending a lot of time worrying about the sound.

If the macro expects the user to have made a selection before running the macro, then I want a sound, and an alert msg, advising the user, if no selection was made.

There are applications without the Copy menu, and there are applications that do not update their menus which can lead to false negatives and positives.

I have moved the macro I posted above to a new topic in the "macro" category: