Question about an action to look for certain items and then replace text

I have list with packing items and want to create an action, that lists all items with a battery symbol as buttons and then, once I press a button replaces it with a charger.

This is a sample list:


  • [ ] hallo :battery:
  • [ ] ade
  • [ ] one :battery:
  • [ ] twoπŸ”‹
  • [ ] three
  • [ ] four :electric_plug:

And this is the action I have. It lists the items with a battery correctly but it does not replace the battery symbol. What am I doing wrong?

let prompt = Prompt.create();
prompt.title = β€˜Charge’;

let packingList = draft.content.replace(’# ’ + draft.title, β€˜β€™).split("\n").forEach(function (item) {
if (item.includes(’[ ]’) && item.includes(β€˜:battery:’)) {
prompt.addButton(item.replace(’- [ ]’,β€˜:battery:’).replace(β€˜:battery:’,’’).trim())
if (prompt.buttonPressed) {
draft.content = draft.content.replace(prompt.buttonPressed + β€œ:electric_plug:”, prompt.buttonPressed);

Thanks in addvance

Have you tried it with something more basic than the battery and charger symbols? I would suspect (only a guess) they could be problematic.

I am, by the way, intrigued at the idea of tagging items with symbols like these. (Tagging deeper than the normal Drafts tags - which work at the draft level.)

Try this:

//Specify some standard strings
const UNCHECKED = "- [ ] ";
const UNCHARGED = "πŸ”‹";
const CHARGED = "πŸ”Œ";

//Set-up the prompt
let prompt = Prompt.create();
prompt.title = "Charge";

//Build the list of devices requring charging
let packingList = draft.content.split("\n").forEach(function(item)
	if (item.includes(UNCHECKED) && item.includes(UNCHARGED))
		prompt.addButton(item.replace(UNCHECKED, "").replace(UNCHARGED, ""));

//Show the prompt;
if (prompt.buttonPressed)
	//Prompt was shown, update draft content
	draft.content = draft.content.replace(UNCHECKED + prompt.buttonPressed + UNCHARGED, UNCHECKED + prompt.buttonPressed + CHARGED);

Based on testing with a draft like this:

# Title
- [ ] halloπŸ”‹
- [ ] ade
- [ ] oneπŸ”‹
- [ ] twoπŸ”‹
- [ ] three
- [ ] four πŸ”Œ


Please also do use triple back ticks to code block your draft and your code. The forum software will interpret Markdown content, and also apply things like smart quotes to code. This means that to even start helping, we have to try and reverse what the forum applies to your original entries.

Hope that helps.

1 Like

Hi @sylumer and thanks for your help. Unfortunately it doesnβ€˜t work here either. What might I be missing or doing wrong, I understand the code worked with your list?

Somehow, magically, it now works. Thank you so much.