Get text from Named Clipboard from KM & use in AppleScript?

thanks again for all the help on this, I really appreciate it!

The shell script now doesn’t show any errors in LaunchBar, but doesn’t show any results when run.

I tried running it in Code Runner and it works fine, and shows the results correctly in the pane below the code.

So I guess (hope) it’s ‘not quite formatting the results the way LaunchBar likes it’ or something?

Here’s an example that successfully shows the items in LaunchBar when run:

function run() {

    return [{ 'title' : 'Library', 'subtitle' : '~/Library', 'icon' : 'Library_Folder_Icon.icns', 'path' : '~/Library' },
			{ 'title' : 'Preferences', 'subtitle' : '~/Library/Preferences', 'icon' : 'Library_Folder_Icon.icns', 'path' : '~/Library/Preferences' },
            { 'title' : 'Application Support', 'subtitle' : '~/Library/Application Support', 'icon' : 'Library_Folder_Icon.icns', 'path' : '~/Library/Application Support' },
		    { 'title' : 'Containers', 'subtitle' : '~/Library/Containers', 'icon' : 'Library_Folder_Icon.icns', 'path' : '~/Library/Containers' },
            ];

}

is there anything in there that shows how ‘LaunchBar likes it to be formatted’? :blush:

That's right, the code I sketched is just for data capture – others may know more about LaunchBar actions and their formats and requirements – I haven't looked at them for a long time.

(But the documentation looks good)

Perhaps something to ask about on a LaunchBar forum ?

and I see there's this:

Sure, I’ll take a look at the documentation, and play about with it. And ask on the LaunchBar forum if needed.

That link looks like it could be useful too.

Thanks again!

1 Like

…I managed it, it was just a matter of changing 'text' to 'title' in:

title: kme.processTokens('%NamedClipboard%' + k + '%'),

Here's the (almost finished version)

Thanks again!

1 Like

Quick progress ! Good work.

1 Like

I remembered that I have another use case that I wanted to setup with this :blush:

I wanted a LaunchBar action to just show the first 3 named clipboards (Clipboard 01, Clipboard 02, and Clipboard 03), instead of all of the named clipboards (that I’d use when setting up projects for work).

Would it be much effort to tweak the script for this?

If it’s the Bash version you’re after, then perhaps something like this:

#!/bin/bash

osascript -l JavaScript <<JXA_END 2>/dev/null
(() => {
    'use strict';

    // clipNames :: () -> [String]
    const clipNames = () =>
        ObjC.deepUnwrap(
            \$.NSArray.arrayWithContentsOfFile(
                \$(
                    '~/Library/Application\ Support/' +
                    'Keyboard\ Maestro/Keyboard\ Maestro' +
                    '\ Clipboards.plist'
                )
                .stringByStandardizingPath
            )
        )
        .map(clip => clip.Name)
        .sort();
        
    // take :: Int -> [a] -> [a]
    const take = (n, xs) => xs.slice(0, n);

    const
        kme = Application('Keyboard Maestro Engine'),
        kvs = clipNames()
        .map(k => ({
            name: k,
            title: kme.processTokens('%NamedClipboard%' + k + '%')
        }));

    return JSON.stringify(take(3, kvs), null, 2); //:: [{name::String, title::String}]
})();
JXA_END
2 Likes

Yes, that’s it. Perfect, thanks!

( You may well not have time for this at the moment, but others who find this thread (now or later), might find it very helpful to have a link to (or brief explanation of) the final LB action )

1 Like

Yes that’s a good idea, I’ll definitely do that.

I just have to finish off the action first. I’m still trying to find out how to paste the selected clipboard contents to the front most app (I’ve tried a few variations with the code, but struggling with it at the moment :blush: ).

Sorry if this has already been covered above, but may I ask why you are using Named Clipboards for plain text?

It would be much easier to use KM global Variables for plain text, and probably faster too.

I have 10 named clipboards (Clipboard 1, Clipboard 2, Clipboard 3 etc.) and copy info to the named/numbered clipboards via keyboard shortcuts. Then paste the info somewhere else via keyboard shortcuts.

Because they're named/numbered clipboards it's easy for me the know/keep track of what info is copied to which clipboard.

It works really well for me, I use it all the time.

Interesting, how would this work for my use case?