Replacing a Draft’s title

I have an action that runs a Shortcut using my draft title. It makes sure the title is capitalized, and then translates it to Spanish, concatenates the two (so it has the form “EnglishTitle // SpanishTitle”) and adds it to the clipboard. I next run the following script to replace the existing title, with the new title.

draft.title = app.getClipboard();
draft.update();

However, the title does not change. If I replace draft.title with draft.content, it replaces my entire draft, but I can’t seem to replace only the title.

Any help?

Thanks,
Mike

The draft title is the first line of the content. Replace the first line of your content and that should change the title.

2 Likes

Makes sense. I have tried some things this morning, but I cannot figure out how to replace the first line of the content. Can you provides some additional help. Thanks!

Something like this (untested) should work:

draft.content.replace(draft.title, app.getClipboard());
draft.update();
2 Likes

Perhaps an edge case, but just in case the first line is blank, I’d split the lines and then rebuild with the clipboard as the first line.

let arrContent = draft.content.split("\n");
arrContent.shift();
arrContent.unshift(app.getClipboard());
draft.content = arrContent.join("\n");
draft.update();

Replace can’t search for a zero length string and so can’t replace the title line in such circumstances.

1 Like

Thank you Rosemary and Stephen. Here is the draft i tested with:

Tournament information
Parents

Below are a few notes about our upcoming tournament.

The code Rosemary suggested executes without error, but the draft is unchanged. Not sure what is going on.

The code Stephen provided (I think the getClipboard method is missing the “()”) converts the draft to the following:

Tournament Information // Informacion del torneo
Parents

Below are a few notes about our upcoming tournament.

Which is what I wanted.

Thanks for the help!

1 Like

Apologies, yes I’d originally had some example text in there, realised you were using the clipboard and accidentally omitted the parentheses when substituting it in. I’ll correct it above :nerd_face: