Create file or append to existing file?

There are various times when I want to take a draft and:

  1. Check if a file with the name [[title]].txt exists in a Dropbox location;

  2. If it doesn’t, create it with [[title]].txt as the file name and [[draft]] plus some metadata (a date stamp, mostly) as the content;

  3. If it does exist, simply append [[body]] to the contents, without first line or the metadata

Is there a series of action steps that can do this? Or am I going to have to figure out enough JavaScript to make it work? (I could do it in Python…)

Thanks!

1 Like

If you want to include the first line in the file content on create but not on append that’s the point where you have an issue.

If you just wanted to use the body each time then it would just be this.

Otherwise a script step in your action like this should suffice.

let dbx = Dropbox.create();
let filePath = "/test/" + draft.processTemplate("[[safe_title]]") + ".txt";
let fileContent = dbx.read(filePath);

if (typeof fileContent == 'undefined')
{
	dbx.write(filePath, draft.content, "overwrite", false);
}
else
{
	dbx.write(filePath, fileContent + draft. processTemplate("[[body]]"), "overwrite", false);
}

Hope that helps.

1 Like

This is fantastic. Thanks so much! Between your script and the examples in the Drafts scripting documentation, I think I see what’s going on and how to modify things still more.

For example, if I want to man’ipulate the existing content of a file (eg, to insert the new material after the old but before my end-of-file metadata), I would modify filecontent from your script …

One question: I see the Dropbox write function has parameters “overwrite” and “add” — i get what overwrite does. Does “add” work like Drafts’ append mode, by adding to an existing file if there is one, or does it add (create) a new/second file if it successfully finds an existing one?

Thanks again!

Here’s how it works with an original file followed by a subsequent file.

  • Add & autorename enabled => two files produced with auto naming of the second file.
  • Add & autorename disabled => first file persists, no overwrite no additional file.
  • Overwrite & autorename enabled => second file overwrites the first.
  • Overwrite & autorename disabled => second file (again) overwrites the first.

Of course, when in doubt, try it out…

let dbx = Dropbox.create();
let filePath = "/test/";
let fileContent = "foo";
let fileExtension= ".txt"

dbx.write(filePath + "Test-A" + fileExtension, fileContent, "add", true);
dbx.write(filePath + "Test-B" + fileExtension, fileContent, "add", false);
dbx.write(filePath + "Test-C" + fileExtension, fileContent, "overwrite", true);
dbx.write(filePath + "Test-D" + fileExtension, fileContent, "overwrite", false);
alert("Let's have a definite pause...");

fileContent = "bar";
dbx.write(filePath + "Test-A" + fileExtension, fileContent, "add", true);
dbx.write(filePath + "Test-B" + fileExtension, fileContent, "add", false);
dbx.write(filePath + "Test-C" + fileExtension, fileContent, "overwrite", true);
dbx.write(filePath + "Test-D" + fileExtension, fileContent, "overwrite", false);
alert("All done. Check your files.");

Make sense?

2 Likes

Perfect. Thanks! Yes, I was plannning to try it out, but I always get twitchy testing out file actions, because of the risk of things going wrong. Also, without an iPad, I’m trying to do all of this on a little phone screen… off to experiment.

Can this script be modified to work with iCloud? I have what sounds like similar wishes to @tf2 only in iCloud (within Drafts area in iCloud).

It should just be a case of switching from Dropbox to FileManager.

Thank you. I was looking at FileManager but was unsure whether that is where I should go. I explore further.