Get Value From Keyboard Maestro Clipboard via AppleScript

How do I get the contents of a Keyboard Maestro clipboard to use in AppleScript?

I would like to replace this:

set barNumbers to the clipboard

do shell script "<<<" & quoted form of barNumbers & " sed -En '
	# Remove leading space.
	s!^[[:blank:]]+!!
	# Remove spaces if any, pipe, and all trailing characters.
	s![[:blank:]]*\\|.+!!
	p
'"

set the clipboard to the result

With something more like this:

tell application "Keyboard Maestro Engine"
	-- Step 1: Get the value from the Keyboard Maestro named clipboard
	set barNumbers to getvariable "Pro Tools - Renumbered Bar"
end tell

-- Step 2: Process the content with the shell script without using the system clipboard
set processedBarNumbers to do shell script "<<<" & quoted form of barNumbers & " sed -En '
    # Remove leading space.
    s!^[[:blank:]]+!!
    # Remove spaces if any, pipe, and all trailing characters.
    s![[:blank:]]*\\|.+!!
    p
'"

-- Step 3: Send the processed result back to the named clipboard in Keyboard Maestro
tell application "Keyboard Maestro Engine"
	setvariable "Pro Tools - Renumbered Bar" to processedBarNumbers
end tell

But that doesn't seem to work and I am not able to find documentation on how to do this.

I could be wrong, but I don't think there's a way to read/write named clipboards from Applescript, so you may have to save your data as a regular variable. If you just set a variable with the contents of the clipboard, you can return the value you want directly to the named clipboard in the Execute AppleScript action, or set it manually with an additional action.

You can't do it directly. You could probably use do script to execute a "Copy Named Clipboard to System Clipboard" action, then get the clipboard as you do in your first script.

But.... why? It would seem to be a lot easier to do what you want in a "normal" KM macro and just execute that with a do script call.

To get the value of a named clipboard you could do something like this:

tell application "Keyboard Maestro Engine"
	set clipVal to process tokens "%NamedClipboard%MyClipboard2%" -- or whatever it's named
end tell

But I don't believe there's a way to write to named clipboards from AppleScript.

Hello Folks

KM has a Plist for Clipboards, too …

Since Named Clipboards are stored as UUID‘s you’ll have to parse the dedicated Plist for the UUID thats stored there for the Name of your desired Named Clipboard - if it’s a via AppleScript created one or not.

Then - if you have the UUID you can execute a Copy to Named Clipboard actions xml in AppleScript with this UUID to write to that desired Named Clipboard any data you want …

This might not be the easiest task to do - and could be a hassle depending on the data you want to write - but this is the only way if you want to do it at runtime of the Macro…

Greetings from Germany :de:

Tobias

1 Like

Not sure exactlly how to do that but will look into it. I have of course ways around it that I have been doing of first putting a clipboard into system clipboard and then running the AppleScript to get system clipboard and then removing the last clipboard after the AppleScript runs and puts the results back into the clipboard. It just seem less effective then if I could directly get the contents of the clipboard and about the same hassle as putting the clipboard contents into a variable and then calling on the variable in AppleScript. Thank you everyone for all the help and feedback on this!

Depends on why you are using AppleScript. But it would seem easier to do the processing in a KM macro, where an "Execute Shell Script" can accept from, and output to, a Named Clipboard directly:

image

Whether you trigger that macro with a hot key, state change, AppleScript, or some other trigger will depend on how the macro fits into the rest of the workflow.

Thanks that makes sense and no doubt something I could just run from right within the Execute Shel Script action. AppleScript has just been my go to and I really need to learn Shell Scripting at some point as well as a deeper dive into regex. Thanks again.

You've already got some knowledge -- if you can do shell script within an AppleScript, you'll be fine with KM's "Execute Shell Script" action.