I am working on a JavaScript Draft action which, among other things, needs to remove invalid filename characters from the Draft title. I can’t reason out why it’s encountering undefined objects without issuing any errors.
I start with a draft as follows:
Title/ with/invalid:filename char/acters\
This is a test entry
Throw it away.
Really!
Wow!
I run a processing action on this draft which only executes the following JavaScript code:
// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting
var noteTitle = new String(draft.DisplayTitle);
draft.insert((noteTitle+'\n'), 4);
noteTitle = noteTitle.split("/").join("?"); // Replace '/' with '?'
draft.insert((noteTitle+'\n'), 4);
noteTitle = noteTitle.split(":").join("?"); // Replace ':' with '?'
draft.insert((noteTitle+'\n'), 4);
noteTitle = noteTitle.split("\x5c").join("?"); // Replace backslash with '?'
draft.insert((noteTitle+'\n'), 4);
What I get afterwards in the draft is:
Title/ with/invalid:filename char/acters\
This is a test entry
Throw it away.
Really!
undefined
undefined
undefined
undefined
Wow!
What am I missing? I expect the inserted lines to all be a variant of the title (first line) and I’m not understanding what I get at all. The code works in another environment (CodeRunner).