How to pass a URL containing parameters through x-callback-url?

I’m working on an Alfred workflow for adding entries to a note, with a link previously stored in the clipboard. (It’s part of a project for implementing Daily Log routines in the Agenda).

It works when the URL doesn’t contain any parameters but breaks because ?s and &s are not correctly treated.

This is how I am preparing the text to pass to the callback-url:

CLIP=`pbpaste`
PROTOCOL=`echo -n $CLIP | cut -d: -f 1`
URL=`echo -n $CLIP | cut -d: -f 2`
URL=`/usr/bin/python -c "import urllib; print urllib.quote(\"${URL}\")"`
LINK="${PROTOCOL}:${URL}"

echo -n "[${query}](${LINK})"

In a later stage, this output will be stored in the TEXT variable, and the callback is called with open:

open "agenda://x-callback-url/append-to-note?title=${TITLE}&text=${TEXT}"

Any ideas?

The quotes seem wrong, especially line 4, and the last term looks off (“echo …”).

This echo is simply the way to pass data inside an Alfred workflow. That part is working fine, the real issue is how to invoke the open command.

Having this in the clipboard, for example:

airmail://message?mail=xxxx%40gmail.com&messageid=0100016beb3c0858-28997f42-9b7a-4272-afb4-ded5f819b783-000000%40email.amazonses.com

This is how the open command is being issued:

open "agenda://x-callback-url/append-to-note?project-title=Journal&title=13 Jul | Sáb&text=- [ ] [test](airmail://message%3Fmail%3Dxxxx%2540gmail.com%26messageid%3D0100016beb3c0858-28997f42-9b7a-4272-afb4-ded5f819b783-000000%2540email.amazonses.com)

The note is successfully appended in the Agenda, but the link is broken.

In other words, the question is how should &s and ?s be encoded in the value of a text parameter for the x-callback-url call.

I also tried substituting just those characters (sed -e 's/&/%26/g; s/\?/%3F/g' instead of the url encode via python), but the following doesn’t work either:

open "agenda://x-callback-url/append-to-note?title=13 Jul | Sáb&text=- [ ] [test again](airmail://message%3Fmail=xxxx%40gmail.com%26messageid=Apd.23965.51558.77863976.1563033585.365788.6ex%40a2plmmsworker08.prod.iad2.gdg.mail)"

Maybe Python & urllib.encode() ?

You definitely have to escape your URL, for example spaces should be converted to %20 etc. Accented characters are officially also not part of the URL spec.

The ampersands that separate parameters should not be encoded of course, but all values for those parameters should be.