Find and Replace in Variable (as opposed to draft)

Hi there,

I’m trying to find and replace text within a variable, NOT draft contents, and I’m having some trouble.

As background, I have created a prompt that lets the user select a pre-defined set of projects from a list. I want to take the resulting project and use it to create a URL to add the content of the draft to things. The challenge is a lot of my projects have spaces, and I need to convert them to “%20” so the URL will be valid. Everything I’ve seen in drafts forums around find and replace assumes you’re doing so in the draft contest (“draft.replace(…)”). Looking around at Javascript documentation, I’ve found that you can use “.replace” at the end of a variable to do a similar operation on a variable. But when I try that, I get this result:

Script Error: TypeError: p.fieldValues["project"].replace is not a function. (In 'p.fieldValues["project"].replace(" ","%20")', 'p.fieldValues["project"].replace' is undefined)

Line number: 23, Column 54

Here is the snippet of my script:

var p = Prompt.create();

p.title = "Pick a project";
p.message = "What project is it for?"
p.addSelect("project", "Project", selections, [], false)
p.addButton("OK");

var selection = p.show()

var target_project = p.fieldValues["project"].replace(" ","%20")

app.setClipboard(target_project)

Any ideas on how to go about this? Also open to another way to pass the prompt result into a valid url.

After running the prompt, the value stored form the addSelect is an array, not a string (docs).

So, to get the selected value, you want to get the first element of that array, like:

var target_project = p.fieldValues["project"][0];

If you are using that in a URL, it might well have more than just spaces that need special encoding. JavaScript has a built in method to encode strings for use in URL parameters:

var encodedProject = encoreURIComponent(target_project);
2 Likes

Ah, arrays get me again. That method is a really handy, thanks so much! (Note a typo I discovered trying to get it to work: “encore…” should be “endode…”).

So I am SOOOO close to having this working. The really weird thing is now I’ve verified that the script pastes the properly encoded result into the clipboard. Hooray!

Unfortunately I am trying to build a things url with the clipboard contents and it seems to refuse to pass it. Here’s how I’ve written it:

things:add?title=[[title]]&list=[[clipboard]]

When I run this, it puts the to-do in my inbox. So I assume it is not actually passing the clipboard contents in the url. When I re-write the URL action to include an exact result I got from my clipboard via the prompt:

things:add?title=[[title]]&list=Architecture%20Practice 

It works. Even if I replace [[title]] with [[clipboard]] it successfully passes the prompt result into Things as the title of the to do.

Any ideas? I know this could be a Things thing, but I feel like something is getting broken in the way the url is being built.

Do you maybe have a URL action with URL encode tags disabled?

It would be easier to debug if you could share a link to your action.

Definitely don’t have encode tags disabled. Sure here is a link to the action:

https://actions.getdrafts.com/a/1bs

It looks like you are putting the encoded project name on the clipboard and then encoding the clipboard again brcaus you have the URL encode tags option set in the URL section.

Since you also use the title tag try placing the unencoded project title on th clipboard.

So I think you’re right. The only thing is, if I turn off enclose tags, then the title element doesn’t work consistently. I guess my script can just generate the entire URL from the draft and pass that onto the URL step.

What do you mean by that?

It should produce the same result every time you run it on a draft. It should pick the first line (up to but excluding a newline) as the title; every single time.

If you mean it does not then ever encode the title (consistently), you would be correct and that is intentional. That was why I said not to encode the clipboard in the script. The URL action would then encode both tags.

You could, but the point is that with your current approach you shouldn’t need to.

Saying that, you might want to consider not using the clipboard and instead using a custom template tag as that would then leave your clipboard intact. Unless you have a need to use it from the clipboard subsequently of course, I think that would be neater.

Nailed it! Yup, I just removed encoding the variable from my script and pasted the unencoded variable to the clipboard, and enabled the drafts URL step encode tags and voila! My selection puts it into the correct project in Things.

Thanks for everyone’s help on this. What a great community Drafts is! :smiling_face_with_three_hearts:

1 Like