Writing a script to create an item in Gladys app

Hi,

I’m writing a script to create an item in the Gladys app. The user is presented with a prompt to create one of three types of item - base64data, text, or URL. The choice made determines the second part of the URL which gets added to the base URL and is then used to open the Gladys app with the contents of the draft creating the item.

I can get the script below to open the app; however, the user is immediately returned to Drafts and no item is created.

var p = Prompt.create();

p.addButton("Add binary data item");
p.addButton("Add text data item");
p.addButton("Add URL data item");

var didSelect = p.show();

if (didSelect === false) {
		 context.cancel();
	}

const baseURL = "gladys://x-callback-url/create-item";

var target = "";
switch (p.buttonPressed) {
	case "Add binary data item":
		target = "?base64data=[[line|4..]]&title=[[title]]&labels=[[line|2]]&note=[[line|3]]";
		break;
	case "Add text data item":
		target = "?text=" + draft.processTemplate('[[line|4..]]') + "&title=" + draft.processTemplate('[[title]]') + "&labels=" + draft.processTemplate('[[line|2]]') + "&note=" + draft.processTemplate('[[line|3]]');
		break;
	case "Add URL data item":
		target = "?url=[[line|4..]]&title=[[title]]&labels=[[line|2]]&note=[[line|3]]";
		break;
}

var cb = CallbackURL.create();
cb.baseURL = baseURL;
cb.addParameter("target", target);
let success = cb.open();
if (success) {
	console.log("Item added to Gladys")
} else {
  	console.log(cb.status);
  	if (cb.status == "cancel") {
		context.cancel();
	}
	else {
		context.fail();
	}
	}

Would anyone be able to offer me some help as to where I’m going wrong?

Many thanks,

Martin

I think this should do it.

let p = Prompt.create();
p.addButton("Add binary data item");
p.addButton("Add text data item");
p.addButton("Add URL data item");
if (p.show() === false)
{
	context.cancel();
}

let cb = CallbackURL.create();
cb.baseURL = "gladys://x-callback-url/create-item";

switch (p.buttonPressed)
{
	case "Add binary data item":
		cb.addParameter("base64data", draft.processTemplate("[[line|4..]]"));

		break;
	case "Add text data item":
		cb.addParameter("text", draft.processTemplate("[[line|4..]]"));
		break;
	case "Add URL data item":
		cb.addParameter("url", draft.processTemplate("[[line|4..]]"));
		break;
}
cb.addParameter("title", draft.title);
cb.addParameter("labels", draft.processTemplate("[[line|2]]"));
cb.addParameter("note", draft.processTemplate("[[line|3]]"));

if (cb.open())
{
	console.log("Item added to Gladys")
}
else
{
	console.log(cb.status);
	if (cb.status == "cancel")
	{
		context.cancel();
	}
	else
	{
		context.fail();
	}
}
Example draft used for testing
Foo
Bar
Quz
RXhhbXBsZSB0ZXh0IGZpbGUuCg==

In the original code you had…

  • Combined parameters, so they got encoded as non-parameters.
  • Were including “?” on some of the parameters, which I think was then getting encoded rather than dropped as that’s the parameter string identifier.

I’ve changed those bits, reordered and removed some unnecessary variable use (you can just assign direct to the object properties), and just used the draft object directly to get the title rather than processing templates. Hopefully this just streamlines what you already had and allows you to follow what changes were applied.

Hope that helps.

3 Likes

Thanks ever so much! That was really helpful :slightly_smiling_face: