How do I set system clipboard?

How would I create a snippet which takes the highlighted text, copies that to the system clipboard, and then calls a specific URL?

The purpose is to be able to call a Keyboard Maestro macro (via URL) and pass the highlighted text to KM via the system clipboard.

Pretty sure you don’t need the clipboard — you can just put *** in the KM URL as a placeholder for the highlighted text. Take a look at the examples for URL snippets.


Yes that probably will work - thank you.

In turn I should be able to pass that to a KM variable.

1 Like

In addition to @tf2’s answer, if you do find yourself needing to place the selected text on the clipboard you can use the before field with a value of copy. This will make PopClip invoke a Copy operation in the target app before the main action of the extension. An example of this is given in How to make a Key Press extension in PopClip — TextExpander example

For more advanced usages there is also a global object in the JavaScript environment called pasteboard which has read/write property called text which represents the plain text content of the pasteboard e,g, pasteboard.text = 'set this text'

1 Like

Thanks @nick - that is helpful

I tried the before/after option in a different snippet - in this one I anticipated to see a Preview of the result but for some reason that does dot work. What am I missing?

# popclip extension to copy with source link
name: Copy with Link
before: copy
after: preview-result
javascript: |
  if (popclip.context.browserUrl.length > 0) {
    pasteboard.text =  '\"' + pasteboard.text.trim() + '\"' + '\n\n' + popclip.context.browserUrl

As it’s a JavaScript action, the result to be previewed will be the string returned from the JavaScript (imagine it as being wrapped in a function), but the code doesn’t return anything. (Well technically it returns undefined.) So you just need to add a return statement at the end returning whatever string you want to display.

OK that makes sense and that works - thanks.

The previewed text appears to be limited to only one line and a limited number of characters. Is there a way to enlarge that (similar to the hidden preferences for MenuSizeLimit)? Or ideally a way to not restrict the size of the preview at all?

Or would it be preferable in that case to do the Preview directly from within Javascript - if so what would be the syntax in Javascript to display the given variable at the present mouse/popclip location?

I tried alert(pasteboard.text) and document.write(pasteboard.text) and neither of these work as Javascript inside a snippet.

1 Like

I’m afraid it won’t show more than one line, and there isn’t a setting to lengthen it, it’s set at 160. It’s was just meant for short messages.

In code it’s popclip.showText('text to display') (equivalent to show-result)

or popclip.showText('text to display', {preview: true}) (equivalent to preview-result)

1 Like

Thank you for the clarification.

So does that mean Javascript in a snippet is sandboxed, i.e. you cannot do within Popclip Javascript code the full range of what you would othewise do in Javascript?

Or is there some way to take the Popclip output and direct Javascript to display it in a new window separate from the regular Popclip output?

Yes the JavaScript runs very much in a sandbox. It has the basic language features and core libraries but doesn’t have any of the Web API objects that you would find in a browser (especially document, window etc.). It also has a few extra objects specifically for popclip. Rough documentation of the PopClip parts.

For showing a window, you are currently out of luck. Jus the popclip.showText() is available right now. I’m planning to add Large Type and Share Sheets methods. If you can describe to me exactly what sort of other features you would like to have from JS, that would help me with planning. One that I already have in mind is to display a submenu with further options. (Similar to the spelling submenu.)

A submenu would be great!

My suggestion would be a well-documented way to pass any PopClip-related objects to Keyboard Maestro as you call the Keyboard Maestro Macro. This can be done to some extent currently as a URL extension but could probably be expanded upon. If that ability to easily transition from PopCllip to Keyboard Maestro is possible including multiple parameters which become KM variables, then there is no necessity to reinvent the wheel further; Popclip provides a front-end UI capability that no other app can offer, whereas Keyboard Maestro provides the ability to manipulate and act on those front-end objects at a level that is very mature and far beyond the scope/goals/ of PopClip. So it’s a win-win for all.

(I agree with submenus -that extends the Popcip UI even further, which is a great place to focus on.)

1 Like

Sounds interesting … I’m just perusing Triggers [Keyboard Maestro Wiki] which defines the set of KM triggers which a PopClip extension might use. There are a lot of options… If you or anyone has a suggestion as to what kind of trigger would bring the most benefit. I note that in addition ot URL trigger there is script trigger already which can be used with an AppleScript PopClip extension.

I suspect the URL trigger is the simplest way to pass a parameter to a KM variable; it can be done in Applescript but that is a bit more complicated.

If possible, what would be best of all is if Popclip did the call in the background (using whatever method you prefer) and if a snippet could simply declare the name or UUID of the Macro and what Popscript objects to pass to what variables in KM. That would seem to fit with the general Snippet philosophy of simplifying the coding syntax.

Ah I understand. I will have to give that some thought. It’s almost like we could have extensions to extend extension syntax…

1 Like