Hook to Roam Research - filenames with parentheses

Roam Research’s flavour of Markdown doesn’t handle certain characters well, such as parentheses, so I thought I’d change the Hook > Scripts > Finder > Get Address script to encode them, as below:

set mypath to get URL of item 1 of theItems

set AppleScript's text item delimiters to {"("}
set subject to text items of mypath
set AppleScript's text item delimiters to {"%28"}
set mypath to subject as text

set AppleScript's text item delimiters to {")"}
set subject to text items of mypath
set AppleScript's text item delimiters to {"%29"}
set mypath to subject as text

return mypath

If I inject a display dialog mypath command, it runs exactly as expected, showing me the file’s URL with the %-escaped parentheses.

But the address that is in the clipboard after Hook has run doesn’t have the escaped versions, it still has the original parentheses.

I’ve successfully changed other scripts to modify the URL returned by Get Address, so I’m very confused why this one isn’t working. For instance, the same maneuver works perfectly well for Safari links.

Ideas, thoughts, suggestions?

I came across the same problem with DOIs from scientific articles.

At least for these, one easy way to deal with this issue is to use Textsoap’s with two consecutive “Find and replace” functions (https://www.unmarked.com), replacing “(” with “%28” and “)” with “%29”.

For example, https://doi.org/10.1016/s0140-6736(79)91437-5https://doi.org/10.1016/s0140-6736(79)91437-5

Two approaches I can think of:

  • Percent encoding.
  • Entity references.

Sounds like you’re doing a kind of the former. I’m not sure which would work better for you.

Thanks both – I should have been more clear about my challenge.

My code successfully swaps out the parentheses for %28 and %29. I can confirm this with a display dialog statement just before return mypath

My problem is that Hook doesn’t use the modified URL; it seems to ignore the value returned by my Finder > Get Address script

I’m perplexed because I’ve made similar changes to the Safari > Get Address script, and that one works just fine