AppleScript to Encode Variable with non-Alphnumerics, pass to Things 3

TL;DR My solution so far only works for Things 3 tasks without spaces in the titles (yes only one-worded tasks work with it presently!). I can’t yet percent-encode in AppleScript. I prefer a method of producing a master task containing links to many other routine tasks than to be constantly utilising keyboard shortcuts in Things 3 to filter out a long list of tasks and I think this will be scalable in the future to carry out other functions as I improve my skills. I want help to please learn how to encode a variable called theTitle within Applescript.

The below script is intended to copy the titles of several tasks each morning in my Things 3 Today list (tagged with M-Routine) and place them in a pre-pended order into the checklist of a master-task. It then adds a task link to the same checklist line, which allows me to revisit the original task within its area. This is useful to me since I remove the original tasks from Today view, and the notes of these particular tasks are fairly detailed and contain callback URL’s which create content elsewhere. So checklist line 1 follows the general form:

theTitle, things:///show?id=55555555

note1: theTitle is variable holding the task title (see script at the bottom of post below)
note2: I have used 5s for masking task ID above

The script then adds a link to the checklist line below the one containing task title+ID; this is an update URL which, when clicked, marks the original task as completed without me having to switch back to its area. Here is that second line of checklist:


As you can see from the script at the bottom, to get this into Things, I had to percent encode it myself although since this part of the code is static, it isn't an issue for me yet. In contrast, the first part of the set theUrl syntax will be variable at the point where theTitle variable appears.

The script continues in its repeat sub-routine finding all tasks with 'M-Routine' tag and extracting the pieces of info I mentioned above, placing a further two lines into the checklist above the previous 2 for each instance. The reason for prepending is that my order of completion the previous day lends itself to reversing this without having to reorganise Upcoming or Today every day.

Please note I am fairly new to scripting in general. I am trying to keep it all within one AppleScript action in KM, but if signposted to do otherwise, I would do that without hesitation. My initial efforts are an adaptation of a neat script presented online a good while ago by Coding Bull Junky [who accredits the script to Ben Eskola], which moves tasks to Evening view instead:

Coding Bull Junky's Move Tasks to Evening View

I have done a lot of research and run many variations within Script Editor but failed to fully comprehend exactly how I could pass the theTitle variable to bash shell as I have not yet had to deal with shell scripts and system-level stuff. I know that I will have to give it go eventually, of course. I did see some interesting information relating to encode-handlers and do shell script open command and whilst there was some promise here I still didn't manage to encode theTitle spaces with %20 etc. I also looked briefly at escaping spaces but haven't fathomed if that is a method which varies from encoding in some way. I also tried to add 'as text' after various instances of theTitle without success.

Nevertheless, my first attempt looks quite promising, but I can’t find a way to percent-encode the task titles in AppleScript so if there is more than one word in the task title [aka the variable theTitle] the script falls over.

I have masked my Things authentication code with ‘As’ in its set variable form and with all ‘Xs’ in its manually encoded form.

If the variable theTitle could have the spaces and any other special chars encoded (and this does occasionally include emoji’s), then I think this would work as the concept works for one-worded tasks. Also, any other suggestions would be most welcome.

I intend to run the script every morning using KM wake trigger or Cron or Hot Key method - probably the hotkey one as I have the Evening tasks one running on system wake.

tell application "Things3"
  set theToken to [insert your-token]
  set newToDo to make new to do ¬
    with properties {name:"Morning Routines"} at beginning of list "Inbox"
  move newToDo to list "Today"
  set theID to id of newToDo
  set theTodos to to dos of list "Today"
  repeat with aTodo in theTodos
    set theTitle to name of aTodo
    set tagList to tags of aTodo
    repeat with aTag in tagList
      if (name of aTag as text) is "M-Routine" then
        if class of aTodo is project then
          set urlCommand to "update-project"
          set urlCommand to "update"
        end if
        set theUrl to "things:///" & urlCommand & "?auth-token=" & theToken & "&id=" & theID & "&prepend-checklist-items=" & theTitle & ",%20things:///show?id=" & (id of aTodo as text) & "%0athings:///" & urlCommand & "%3Fauth%2Dtoken%3D[insert your-token as %encoded]%26id%3D" & (id of aTodo as text) & "%26completed%3Dtrue"
        open location theUrl
        set theUrl2 to "things:///" & urlCommand & "?auth-token=" & theToken & "&id=" & (id of aTodo as text) & "&when=anytime"
        open location theUrl2
      end if
    end repeat
  end repeat
end tell