Copy notes to Obsidian folder

Just to throw another option into consideration.

I’m assuming that Obsidian is in use on a Mac (I’ve not seen an iOS/iPadOS version as yet) alongside Drafts in this particular case.

While the JavaScript run from inside of Drafts is sandboxed, the same isn’t true in terms of a shell script. This setup seems to work for me in my testing.

Set-up a script step in an action with the following.

function exportFile(p_strDestinationPath)
{
	// Write a file to the Drafts area
	let fmLocal = FileManager.createCloud();
	fmLocal.writeString("/movethisfile.txt", draft.content);

	// Set up a shell script to move the file out of the Drafts area
	let strScript = `#!/bin/zsh
	mv "$HOME/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/movethisfile.txt" "${p_strDestinationPath}"
	`;
	let shScript = ShellScript.create(strScript);

	// Run the script
	if (shScript.execute())
	{
		app.displayInfoMessage("Success");
		return true;
	}
	else
	{
		app.displayErrorMessage("Error: " + shScript.standardError);
		console.log(shScript.standardError);
		return false;
	}
}

exportFile("$HOME/Library/Mobile Documents/com~apple~CloudDocs/Temp/test_output_from_drafts.md");

Here the draft content is effectively placed into a file called test_output_from_drafts.md, within my Temp directory in iCloud, but it could be a file anywhere accessible in the file system on my Mac.

What actually happens is that Drafts outputs the file as normal, within its sandbox, and then the shell script moves it to its final destination.

1 Like