Bulk import text files

Is there a way to bulk import text files, either on the Mac or in iOS? (i.e., import all my nvALT .md files)

1 Like

Maybe drag and drop on the Mac or on an iPad?

couldn’t get it to work on Mac at this point. will try iPad later today.

If the files are in a supported cloud location, you could write a script to import them.

Providing that script/functionality in the finished product would make migration from other two-platform software possible for less-technical folks.

We do exist! I mainly write prose, not code. I use ia Writer because I don’t want to have use another cloud for my data.

Would also make Drafts for Mac more attractive for switchers.

1 Like

FYI for later folks reading this: bulk import is now possible on the Mac app. You can select several files when in the “import” menu command. Works great. Thanks @agiletortoise!

5 Likes

That’s just what I was looking for, thanks!

I have 800+ notes currently in another app. They’re .md files with the first line of the note contained in the file name, itself.

Has anyone written an action or script to bulk import while including the file name as the first line of the resulting Drafts note/file?

Thanks.

I have not seen such an example, but it would be possible. You would have to put the files somewhere Drafts can read them, like a subdirectory of iCloud Drive/Drafts, and loop over them with a script. This is untested, but assuming you create a Temp folder in iCloud Drive/Drafts, something like this should work:

let fm = FileManager.createCloud();
let path = "/Temp";
let files = fm.listContents(path);

for (let f of files) {
    // get file name from end of path
    let fileName = f.split("/").pop();
    // read content of file
    let content = fm.readString(f);

    // create draft
    let d = Draft.create();
    d.content = fileName + "\n\n" + content;
    d.update();
}
1 Like

Thanks. To clarify, this is JavaScript to be run in Script Editor? I’m getting errors on the object FileManager.

I just gave Greg’s script a try with a handful of files and it worked as expected and yielded no errors.

Here’s the action I created that uses just the script from above - Import Temp Files.

Give it a try and if it still fails take a look in the action log and let us know exactly what it says.

All I can think is that you perhaps have something unusual in a file name that may be causing an issue of the path is set to look in the wrong place (note Greg’s point on iCloud drive). You could always console log the path and the name of the files as they are being processed and that could at least tell you where the script is actually looking for files, or on which file it is failing.

1 Like

I was attempting to run in Script Editor. Your action worked like a champ. Thanks!

@agiletortoise: The only improvement I would suggest is to trim the file extension from the filename before prepending it as the first line of the note.

I so by modifying your code as follows, with the caveat that regex would likely be more performant.

let fm = FileManager.createCloud();
let path = "/Temp";
let files = fm.listContents(path);

for (let f of files) {
    // get file name from end of path
    let fileName = f.split("/").pop();
    // read content of file
    let content = fm.readString(f);

    // create draft
    let d = Draft.create();
    let t = fileName.split('.').slice(0, -1).join('.');
    d.content = t + "\n\n" + content;
    d.update();

}

How would this script need to change if the cloud service was either OneDrive or Dropbox? My work disables use of iCloud drive for devices so I’m looking for a workaround.

Fundamentally you would change the FileManager object references to Dropbox or OneDrive objects and methods. The tricky bit is probably the file listing. I don’t see anything in the docs for an inbuilt method for either, so probably best to go with Dropbox and make the API call directly.

If that’s out of scope for your skillset, consider just copying the files using the iOS Files app between Dropbox/OneDrive and iCloud, and then import.

Hope that helps.

thank you for the quick reply

Definitely beyond my skill set. Unfortunately I can’t deploy your workaround since my work blocks icloud drive on my devices.

Perhaps I will look into it. If I can get anything to work, I will post it here. If someone has already figured this out, would definitely be interested in seeing what you have done.

Ah okay. I’d somehow conflated this to being work desktop and personal iPhone, hence my suggestion. I now realise that it’s the iOS device that belongs to work and block’s iCloud :man_facepalming:t2:

If this happens to be a one-off or infrequent process, another option might be to concatenate the files with separators, import one “mega draft” into drafts, and then use a split-based action to generate multiple drafts from it. That could cut down the workload massively.