For flexible use of (Keyboard Maestro Engine).doScript with named clipboards, you need the UUID (rather than just the name) of a named clipboard.
(See, for example:
There may be more canonical ways of obtaining the UUID in exchange for a Clipboard name, but here, in the meanwhile, are AppleScript and JavaScript for Automation routes to doing that:
AppleScript
use framework "Foundation"
use scripting additions
-- Returns an Applescript record which either has a UUID in its |Right| field
-- or a message in its |Left| field,
-- (if no named clipboard was found by the name supplied).
-- kmNamedClipUUID :: String -> Either Message UUID
on kmNamedClipUUID(strName)
set k to toLower(strName)
set ca to current application
set clips to (ca's NSArray's ¬
arrayWithContentsOfFile:((ca's NSString's ¬
stringWithString:("~/Library/Application Support/" & ¬
"Keyboard Maestro/Keyboard Maestro" & ¬
" Clipboards.plist"))'s ¬
stringByStandardizingPath)) as list
script nameMatch
on |λ|(x)
k = toLower(|name| of x)
end |λ|
end script
script uidMay
on |λ|(x)
Just(UID of x)
end |λ|
end script
set mbFound to bindMay(find(nameMatch, clips), uidMay)
if Nothing of mbFound then
|Left|("Clipboard not found by this name: " & strName)
else
|Right|(Just of mbFound)
end if
end kmNamedClipUUID
-- TEST ------------------------------------------------------------------
on run
kmNamedClipUUID("Gamma")
end run
-- GENERIC FUNCTIONS -----------------------------------------------------
-- Just :: a -> Just a
on Just(x)
{type:"Maybe", Nothing:false, Just:x}
end Just
-- Nothing :: () -> Nothing
on Nothing()
{type:"Maybe", Nothing:true}
end Nothing
-- Left :: a -> Either a b
on |Left|(x)
{type:"Either", |Left|:x, |Right|:missing value}
end |Left|
-- Right :: b -> Either a b
on |Right|(x)
{type:"Either", |Left|:missing value, |Right|:x}
end |Right|
-- bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
on bindMay(mb, mf)
if Nothing of mb then
mb
else
tell mReturn(mf) to |λ|(Just of mb)
end if
end bindMay
-- find :: (a -> Bool) -> [a] -> Maybe a
on find(p, xs)
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return Just(item i of xs)
end repeat
Nothing()
end tell
end find
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- toLower :: String -> String
on toLower(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower
JavaScript for Automation
(() => {
'use strict';
// Returns an JS dictionary which either has a UUID in its |Right| field
// or a message in its |Left| field,
// (if no named clipboard was found by the name supplied).
// GENERIC FUNCTIONS --------------------------------------
// Just :: a -> Just a
const Just = x => ({
type: 'Maybe',
Nothing: false,
Just: x
});
// Left :: a -> Either a b
const Left = x => ({
type: 'Either',
Left: x
});
// Nothing :: () -> Nothing
const Nothing = () => ({
type: 'Maybe',
Nothing: true,
});
// Right :: b -> Either a b
const Right = x => ({
type: 'Either',
Right: x
});
// bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
const bindMay = (mb, mf) =>
mb.Nothing ? mb : mf(mb.Just);
// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = (lf, rf, e) => {
const ks = Object.keys(e);
return elem('Left', ks) ? (
lf(e.Left)
) : elem('Right', ks) ? (
rf(e.Right)
) : undefined;
};
// elem :: Eq a => a -> [a] -> Bool
const elem = (x, xs) => xs.includes(x);
// find :: (a -> Bool) -> [a] -> Maybe a
const find = (p, xs) => {
for (var i = 0, lng = xs.length; i < lng; i++) {
var x = xs[i];
if (p(x)) return Just(x);
}
return Nothing();
};
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[1], null, x[0]] : x
);
// MAIN ---------------------------------------------------
// kmNamedClipUUID :: String -> Either Message UUID
const kmNamedClipUUID = strName => {
const
k = strName.toLowerCase(),
mbFound = bindMay(
find(
x => k === x.Name.toLowerCase(),
ObjC.deepUnwrap(
$.NSArray.arrayWithContentsOfFile(
$(
"~/Library/Application\ Support/" +
"Keyboard\ Maestro/Keyboard\ Maestro" +
"\ Clipboards.plist"
)
.stringByStandardizingPath
)
)
),
dct => Just(dct.UID)
);
return mbFound.Nothing ? (
Left('Clipboard not found by this name: ' + strName)
) : Right(mbFound.Just);
};
// MAIN ---------------------------------------------------
return show(
kmNamedClipUUID('Gamma')
);
})();