Ulysses - No Text in Clipboard

The required information is here, almost at the end of the Wiki article (don’t ask me why).

So, to get the value of a KM variable:

var kme = Application("Keyboard Maestro Engine");
kme.getvariable('Var');

First, I'd like to request that you be more precise in your terminology. This will help you get better answers faster, and help other avoid wasting time. :wink:

So, what are you actually asking about?

  1. JavaScript in a Browser
  2. JavaScript for Automation (JXA)
  3. Java (a language completely unrelated to JavaScript)?

The terminology that is normally used is:

  • "JavaScript" -- when referring to JavaScript in a Browser
  • "JavaScript for Automation" OR "JXA" -- when referring to just that.

I'm going to take a guess that you mean JXA, since JavaScript (in a Browser) does not have solid, reliable access to the System Clipboard.

Here is an example JXA script that shows you how to:

  • Get a KM Variable
  • Set a KM Variable
  • Read the Clipboard
  • Set the Clipboard

##example JXA Script

'use strict';
(function run() {      // this will auto-run when script is executed

  // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax ---
  var app = Application.currentApplication()
  app.includeStandardAdditions = true

  // --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES ---
  var kme = Application("Keyboard Maestro Engine");
  

  //--- GET A KM VARIABLE ---  (remove if not needed)
  //    Returns empty string if it doesn't exist
  var someVarNameStr = kme.getvariable("KMVarNameToGet") || 'Default Value if NOT Found';
  console.log("someVarNameStr: " + someVarNameStr)
  
  var someNewDataStr = "Text to be set to a KM var";
  
  //--- SET A KM VARIABLE ---
  //      Creates the Variable if it doesn't exist
  //      Verify Variable in the KM App Preferences
  kme.setvariable("KMVarNameToSet", { to: someNewDataStr });

  
  //--- GET TEXT ON CLIPBOARD ---
  var clipboardStr = app.theClipboard()
  console.log("clipboardStr: " + clipboardStr)

  var someDataStr = "Example text to put on clipboard"

  //--- COPY TO CLIPBOARD ---
  //      Verify using KM Clipboard History Viewer
  app.setTheClipboardTo(someDataStr)


}  // END of function run()
)();

Please feel free to ask any questions.

When you copy [a selection to the System] clipboard, the app puts on a variety of flavours of data, they might include plain text, rtf, rtfd, proprietary formats, jpg, pdf, png, etc. There are literally thousands of flavours and each clipboard you copy typically has half a dozen or more flavours.

And unfortunately, the formats that applications use are not particularly consistent, and there are a variety of styled formats that are not always mutually compatible. Also unfortunately, Applications do no always put consistent flavours (that is, some of the flavours might represent different text for example).

When Keyboard Maestro reads text from the clipboard, it can do it in one of two ways depending on where the text is going.

It can read it as plain text, looking for the specifically un-styled text formats; or it can read it as styled text, looking for rtf styled formats.

It sounds like in this case, in response to something (probably not text length per se, more likely something in the text, but who knows for sure), Ulysses is putting a different set of flavours in the clipboard. Maybe it is deciding the text is styled a specific way and using some specific format for that.

In any event, there are two things you can try to do to "clean up" the clipboard.

  • Set Clipboard to Plain Text %CurrentClipboard%

Keyboard Maestro will explicitly read the un-styled clipboard flavours.

  • Set Clipboard to Styled Text %CurrentClipboard%

Keyboard Maestro will explicitly read the styled clipboard flavours.

Either way, Keyboard Maestro will set the clipboard with what it considers to be a consistent set of flavours.

2 Likes

Thank your for your reply Peter. After some trial and error I made it work by doing this:

Here is what I do not understand:

  1. How come setting Clipboard to System Clipboard did not do anything but setting it to Default Clipboard did the trick?

  2. I thought that I had to add the option to set the clipboard to plain text after "Cut" and not before it. Why before and not after?

The only thing of relevance that that highlighted action is doing is reading the clipboard.

It might be a clipboard caching issue.

Show the Keyboard Maestro Clipboard History Switcher and watch what it displays.

Hey @slyfox,

Just to make sure – you do understand that the Default Clipboard isn’t really the default?

The real default clipboard is the System Clipboard. The so-called “Default Clipboard” is just another named clipboard (i.e. a variable that holds clipboard data).

-Chris

1 Like

This has confused many, many KM users, including me. :frowning:
We keep hoping that some day @peternlewis with change the name of this named clipboard. :wink:

1 Like