Applescript, encoding URLS, and smart rules

Here’s a JavaScript version of the script (note: I used the “Title” to find the appropriate note in Agenda, so whoever prefers to use the “Identifier” has to change this).
It is a simple script, that CAN NOT be used in a smart rule (but from the context of this thread, I didn’t see the need for a smart rule anyway). Tested it with one DT record only.

(() => {
let app=Application("DEVONthink 3");
app.includeStandardAdditions = true;
let sel = app.selection();
sel.forEach(rec => {
  let URL = rec.referenceURL();
  let nameEncoded = encodeURIComponent(rec.name());
/* Change "title=Example%20Note" to "Identifier=xxx" if needed */
  rec.comment = referenceURL();
  let agendaNote = `agenda://x-callback-url/append-to-note?title=Example%20Note&text=[${nameEncoded}](${URL})`
  app.openLocation(agendaNote);
});
})();
3 Likes

Thanks for this, @chrillek.

I came across this page in Apple’s own Scripting Guide, last updated in 2016, which explains how to URL encode/decode with “pure” AppleScript.

Today, URLs can contain Unicode characters. Which the code you linked to doesn’t handle. For example, encodeCharacter("个") returns “63”. The correct value is “%E4%B8%AA”, i.e. three encoded characters, not one.
And it’s a more work to encode a complete string then a single character.

A better alternative (if you insist on using AppleScript, that is :wink: )would be the stringByAddingPercentEncodingWithAllowedCharacters method of the NSString class (or a similar method), which you can access through the ASObjC bridge. Still more than a single line of code, I guess, but at least you should get the correct result.

1 Like

FWIW, if you can use the satimage addition, it has an escapeURL command which returns the url encoding for any characters you feed it.

set x to "悪㎯个"

escapeURL x
--> "%E6%82%AA%E3%8E%AF%E4%B8%AA"

Of course, you can feed it just a single character as well.

set x to "个"
escapeURL x
--> "%E4%B8%AA"

As an aside, if you only have a character or two to encode, you can get the url encoding using the Character Viewer (aka Emoji & Symbols). From within the viewer, search for the character.

Right-click on the resulting character and you should see a Copy character info command. Click on it and then paste into an empty textedit window. You should see a result like this below.

个
CJK UNIFIED IDEOGRAPH-4E2A
Unicode: U+4E2A, UTF-8: E4 B8 AA

Take the hex numbers in the UTF-8 section, insert % and remove the spaces. You now have the url encoding for the character.

It works in both directions. If you search for E4 B8 AA then it will display the following:

Character
E4 B8 AA
Character code
个

Unfortunately, other than by using satimage’s escapeURL command, I don’t know how to programmatically get the UTF-8 code so it’s not as versatile as it might be (or maybe it’s just me that’s not).

:wink:
From within a script, that might be challenging.

I posted a code snippet in the other thread that doesn’t require installation of libraries or so.

1 Like

An even better alternative IMHO is…

tell application id "DNtp" to do JavaScript "encodeURI('个');"
--> %E4%B8%AA
1 Like

Using JS whenever AS falls short might be a good first step to switching. :stuck_out_tongue_winking_eye:

1 Like

Thanks. The ASObjC snippet will be useful but I’m not able to figure out what to do with the javascript one.

Apparently there is also a stringByRemovingPercentEncoding to convert in the other direction.

Well, you use it just as you’d use an AppleScript script. Run from script editor after having yet the language toi JavaScript, install in DTs script folder, useosascript on the command line…