Change Variable Syntax to Accommodate the Things 3 URL Scheme

I am interested in exploring how I can use KM to integrate with URL schemes in Things 3, which the developers recently made available: Things URL Scheme - Things Support

The issue I'm running into is that these links use a lot of percentage signs, which is the same syntax that KM uses, so those links get messed up (since KM is trying to interpret these percentage signs as variables). Is there some sort of way around this?

For example, a base "create new task with X text" would look like this for the URL scheme:

things:///add?title=test%20to-do%20item%20here

The three percent signs there screw stuff up. Any ideas? Thanks!

1 Like

Not all KM text contexts process tokens.

One approach, for example, would be to let an Execute JS action:

  1. Perform the url encoding
  2. Open the encoded url

(() => {

    // standardAdditions :: () -> Application
    const standardAdditions = () =>
        Object.assign(Application.currentApplication(), {
            includeStandardAdditions: true
        });

    const strURL = encodeURI(
        'things:///add?title=test to-do item here'
    );

    return (
        standardAdditions()
        .openLocation(
            strURL
        ),
        strURL
    );
})();
1 Like

Or:

Build and open url.kmmacros (1.9 KB)

1 Like

and in the options gearwheel at the top right of the open URL action:

47

Yep, just set the Action Gear menu options to "Process nothing":

image

I hate to bump an old topic, but has anyone gotten this working? I have the exact same URL working in the Shortcuts app, but when I try to run it on my Mac, Keyboard Maestro tells me it's an invalid URL. I've tried setting the action gear for the URL action to both process text normally and process nothing, and both result in the same error.

No worries about bumping the old topic; people do that all the time here. As for your question, it's hard to say what might be going on without seeing the URL in question. Can you share it here, along with the macro you're using to try to open it?

Sure thing. Here's the URL:

things:///add?title=Read%20%Variable%Title%%20by%20%Variable%Author%&checklist-items=Read%2010%25%20of%20%Variable%Title%%0ARead%2020%25%20of%20%Variable%Title%%0ARead%2030%25%20of%20%Variable%Title%%0ARead%2040%25%20of%20%Variable%Title%%0ARead%2050%25%20of%20%Variable%Title%%0ARead%2060%25%20of%20%Variable%Title%%0ARead%2070%25%20of%20%Variable%Title%%0ARead%2080%25%20of%20%Variable%Title%%0ARead%2090%25%20of%20%Variable%Title%%0ARead%20100%25%20of%20%Variable%Title%%0ASave%20Highlights&tags=Read&list=Personal

Like OP, I am trying to pass variables into the Things 3 URL scheme to function as a template. I've successfully made a similar workflow using Shortcuts for my iOS devices. I've also been able to create an Alfred workflow, but I much prefer the interface of KBM.

Here's the full macro.

2018-11-14-screenshot-1

Note: I can copy that same URL onto my iOS device or open it via Google Chrome on my Mac and it works just fine minus passing the actual variables, so there doesn't seem to be an actual issue with the URL but KBM thinks there is.

Also, I've done some testing and KBM seems to have no problem with the first part of the URL. As soon as I enter %Variable%Title% the Open URL text turns orange to report an error.

Thanks for sharing. As you suspected, KM is indeed getting mixed up with the % signs used for variables and the ones used in percent encoding for URLs. The way I'd go about resolving this is to assemble the URL in a KM variable first, run a filter on the entire URL to percent encode it, then open the encoded URL variable:

New Book Project.kmmacros (3.4 KB)
image

Results

52%20AM

This also has the advantage of making it easier to change the resulting to-do item, since you can edit the title, checklist, notes, and other fields in plain text without worrying about hard-URL encoding everything.

3 Likes

Note that Keyboard Maestro itself does not care anything about the format of the URL, however the URL is stored within a system NSURL object, and NSURL will only allow valid URLs, so it is NSURL that is rejecting the URL as invalid.

Also note that if you are processing text tokens (as controlled by the gear menu in the action), then you must deal with the fact that % is special to Keyboard Maestro, and so where you want an explicit % sign in the URL you need to double it, so eg:

…Read%%20%Variable%Title%%%20by%%20…

@gglick’s solution is a better solution, since all the % tokens in the Set Variable action are intended for Keyboard Maestro to process, and all the percent tokens needed in the URL are created by the Filter action.

Sorry for such a late reply on this. The end of the year got away from me with completing my Masters and the holidays. This is exactly what I needed. Thank you so much. Seeing it broken down this way helped me port over all my existing Omnifocus templates and I'm back in business with Things. My workflow and frustration levels thank you!

1 Like

@gglick Thank you so much for posting this macro. I have one question. I used it as is to better understand how it works and it doesn't separate the checklist items into individual items, just one large item with all the lines (See screenshot). I tried adding a %0A at the end of each line --- getting that from the URL building for things and it just pastes it into the checklist.

Is there anything obvious that I am missing? I appreciate it.

Here is the macro view... the rest of it is unchanged.

I'm glad you found it useful, or at least will once it's working as expected for you. Unfortunately, I'm afraid I don't know what to tell you, as the macro continues to work as expected for me without %0A, which should be added automatically by the filter action. I don't suppose quitting and relaunching Things changes anything?

Hey there, okay, so you mentioned "should be automatically filtered" do I tried deleting the line breaks and then reinserting them. I imported the macro and for some reason, KM stripped the line breaks. Re-adding them worked and now it worked. Thanks for the tip, and thanks for the great script.

1 Like

Hello,

I tried your macro, but when I try it, I receive this message from Things 3: The URL scheme cannot be empty and must be set to "things:"
Perhaps I'm missing something, since I'm new to Keyboard Maestro.... :slight_smile:

Regards,

Angelo

Hi Angelo,

Welcome to the forum. You're not missing anything, it's just that the way KM's URL percent encode filter changed between version 8, when that macro was originally posted, and version 9, which came out several months later. If you're new to KM you're almost certainly using version 9, so this updated version should work with that:

Things Quick Entry Improved 1.1.kmmacros (9.1 KB)

Hello,

Thank you, this works!

Regards,

Angelo

Just wanted to say, thank you! This is exactly what I was stuck on, and I didn't know that Keyboard Maestro could do the encoding for me :slight_smile: