Problem with Percent Encode filter with ampersand

I think I've found a small bug!

I've been using KM to create some macros for Drafts for Mac beta and discovered that when I Percent Encode for URL any text with &, I get &amp instead of %26 which is what it is needed for URL schemes to work.

Am I doing something wrong or is this, in fact, a bug?

The code for Percent Encoding for URL currently uses the system API:

[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

As far as I can see, that API ignores the & key and leaves it unencoded (it definitely does not encode it as & which would be an HTML entity.

That API is now deprecated, quite rightly since (as the comment on the depreciation points out), what exactly should be encoded varies depending on your point of view. So the next version of Keyboard Maestro will use the API:

[string stringByAddingPercentEncodingWithAllowedCharacters:xxx]

The question is, what to use for xxx. I think I'll end up encoding everything except alphanumerics, which will include the & character, as well as any other symbols.

1 Like

There was some discussion about this in old threads. Someone had provided actions and plug-ins using JavaScript for Applications that did it correctly. I found it with a search for "Percent encode ampersand" in the forums.