When importing text files, is there a way for Drafts to take the title and put in top line?

Hi!

I have a bunch of .txt files that I want to import into drafts, but I want to preserve the file names of the files, preferably in the top line of the draft itself. Is that easily doable? Thanks!

Certainly very doable with Shortcuts on iOS if the files are in a suitably accessible storage location. Just grab the files you want, loop over the files and pass the name and the content (magic variables are your friend) into a create new draft action.

On Mac, it might be a little harder to do directly, so I reckon an easier approach might be to produce modified versions of the text files where the filename is preprended in as the first line of the text file, and then just drag and drop the modified files into the app to create the drafts.

I think this shell script would do the trick. set the directory path variable at the top (I have it as my Desktop here) and it should search that folder for all text files, then create a new text file with the same name, in the same folder, but prefixed with mod_ (easy for sorting then). Each file should have the original file name as the first line, and the content as the subsequent lines.

#!/bin/zsh

directorypath=~/Desktop

for filepath in ${directorypath}/*.txt; do
	filename=${filepath:t:r}
	modifiedfilepath=${directorypath}/mod_${filename}.txt
	echo $filename > ${modifiedfilepath}
	cat $filepath >> ${modifiedfilepath}
done

Hope that helps.

1 Like

Wow, this is WAY more help than I expected (or deserve!)

Do I make this in automator and then run it, I’m fairly savy with the Mac, but haven’t run many shell scripts (if any!)

Thanks again.

-Eric

Shell scripts are independent of Automator and you would typically execute them from the terminal.

I would suggest having a read of the first couple of sections of this article on shell script basics (“Should I Learn” and “Writing Shell Scripts”). That will explain how to save and run a shell script.

Hopefully it makes sense, but if not, just use your preferred search engine to look up an introduction to shell scripts on the Mac.

You can create and save a script from your text editor app of choice (TexEdit will suffice). Then open the Terminal app, navigate to the script, change the permissions, and run it. Details of how to do this are in the example above.

Note that the script I used is utilising “zsh” not “bash”, and many shell scripting examples will reference bash instead. Don’t worry, they’re different shells, which you can think of as different languages or dialects. Which one is used is important interns of how a particular script is written, but not so much when you run it.