I’m trying to find and replace text within a variable, NOT draft contents, and I’m having some trouble.
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)
var selection = p.show()
var target_project = p.fieldValues["project"].replace(" ","%20")
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"];
var encodedProject = encoreURIComponent(target_project);
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:
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:
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:
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!