Problems Copying, executing JS script and then pasting transformed value

Hey guys,

I am new here and trying to to implement a macro that will

  1. copy the selected text
  2. transform the text to camelCase + add some characters
  3. Will paste the new transformed text

I tried to do so:

but still it is not pasting the transformed text. Could you please let me know how to deal with this?
I also tried to do it via some other blocks, but without greater results.

Application("Keyboard Maestro Engine")
        .getvariable("SystemClipboard");

Are you expecting Keyboard Maestro to have a pre-defined variable of that name ?

(I notice that you are creating one lower down ...)


There are various ways of reading the clipboard contents in a JavaScript for Automation context. I personally tend to use something like this:

Expand disclosure triangle to view JS source
(() => {
    "use strict";

    ObjC.import("AppKit");

    const main = () =>
        clipboardText();


    // --------------------- JXA ---------------------

    // clipboardText :: IO () -> String
    const clipboardText = () =>
        // Any plain text in the clipboard.
        ObjC.unwrap(
            $.NSString.alloc.initWithDataEncoding(
                $.NSPasteboard.generalPasteboard
                .dataForType($.NSPasteboardTypeString),
                $.NSUTF8StringEncoding
            )
        );

    return main();
})();
1 Like

I simplified and improved it. Now it is working, but really slowly ( takes 5-10 seconds to transform and paste):

Possibly faster to capture the clipboard inside the Javascript action, and simply return a value from it (instead of that ignore results setting you show there).

(You may find that you get a speed bump by dropping all those text_in text_out actions, (and .setvariable, .getvariables) which are essentially redundant)

I don't know whether this is roughly the kind of thing that you are after:

Change selected text to camel case.kmmacros (4.0 KB)

this is not working unfortunately, when running on selected from clipboard text nothing happened

I improved mine solution, but still it is slow and takes around 7 seconds to copy->transform->paste

Not sure that I quite understand that phrase ... (η”¨δΈ­ζ–‡θ§£ι‡‹δΉŸθ‘Œ)

The copy action is not working ?

You are selecting text in a text editor ?

I am selecting the text and then clicking hot key. The same way you are copying the text, but instead of cmd+c :smiley:

Not sure what's happening there – the macro at Problems Copying, executing JS script and then pasting transformed value - #5 by ComplexPoint above is working fine here (and swiftly, though hardware may vary).

If you do still want to find something faster, you could try:

  • replacing my Copy action above with a ⌘C keyboard action, (and/or)
  • using a different hotkey, in case of clashes.

Good luck !