Filter system clipboard to percent-encode a URI component

Filter system clipboard to encode URI component.zip (5.2 KB)

Custom Keyboard Maestro Plug-in

NAME

  • Filter system clipboard with encodeURIComponent()

VERSION

  • 0.1

SYNOPSIS

  • A sister function for the built-in Filter Clipboard with Percent Encode for URL.

  • The other action encodes only what is required to make a simply-formed URI string compliant under RFC 3986.

  • This action, in contrast, escapes all characters except the following:

    • alphabetic
    • decimal digits
    • - _ . ! ~ * ' ( )
  • It is essentially a wrapper for an applications of the standard JavaScript function encodeURIComponent() to the system clipboard.

REQUIREMENTS

  • Yosemite
    • The underlying script is written in Javascript for Applications

INSTALLATION

  • Drag the .zip file onto the Keyboard Maestro icon in the OS X toolbar.
  • (if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
    • ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions

CONTACT

Standalone JavaScript for Applications equivalent:

(function () {
	var a = Application.currentApplication(),
		strClip = (
			a.includeStandardAdditions = true,
			a
		).theClipboard({
			as: 'string'
		});

	return strClip ? encodeURIComponent(strClip) : '';
})();

This is great.
Can you please modify to accept other inputs apart from the clipboard including text tokens and variables?

I’m glad that seems useful.

I’ll probably leave it as it is for the moment, for compatibility with the built-in action, but you can always precede it with a set Clipboard to (Text|Variable) action to make use of text tokens and variables.

I am of the opinion that the use of the clipboard slows down actions compared to the use of variables. The good thing is we now have a working percent encode action which can be easily be integrated into a macro.

Beautiful. Thanks for the great work.
Just noticed a bug in the “Encode URI Component”. It frequently gives a blank clipboard while the earlier one, filter clipboard for percent encode gives correct results. What may be responsible?
I noticed it while trying it on “{“version”:0.8,“agent”:“Chrome2Maxel”,“urls”:[{“href”:“https://forum.keyboardmaestro.com/uploads/default/original/2X/8/85f71074117dac7a56647fe941e1fe20bebd5969.zip",“title”:"your browser”}]}”

Thanks – probably the difference between single and multi-line strings in the input – I'll take a look later today

Updated – does that seem better with multi-line strings ?

Yes, it works well! Thanks for the prompt response and great work in record time!