Drafts - File Manager

#1

Hi All,

I’ve posted this one on the Drafts forum, but thought of leaving it here as well.

Context: I have txt files in a dropbox folder that I importa into Drafts (iOS version) and run some actions to ultimately post it into a Wordpress site.

Once posted I’d like the original txt file in Dropbox to be moved into a different folder.

Is this possible with FileManager? I have no knowledge of js so any help with scripts would be very welcome too.

Thanks!!
Andre

1 Like
#2

FileManager is for iCloud, whereas the Dropbox object is for Dropbox use. But they have different functionality.

You might be able to push an API request in Drafts for Dropbox to do it. Or you could do a few file operations via Shortcuts and call that from Drafts.

You could even have Drafts rewrite the file to the new location, and just have Shortcuts delete the old one : move = copy + delete.

2 Likes
#3

Thanks @sylumer. Didn’t realise FileManager was for iCloud only.

I’ve been playing with shortcus but would prefer to have it as a Drafts action for this particular use case.

If I had the original files in iCloud could I have a FileManager action to move from folder A to folder B (ie, copy into B and delete it in A)? Is it a complex script?

Cheers

1 Like
#4

Always read the documentation :wink:

Yes, but there is a move function and there isn’t a delete function … so we can go with just a move here rather than an explicit copy and delete. It is just one line of script once you have everything in place.

I’ve put together a little example that walks you through things.

//Set some demo things to work with
const FILENAME = "movethisfile.txt"
const FILE_CONTENT = "just some arbitrary demo content here"
const BASE_PATH = "/"
const FOLDER1 = "Folder 1"
const FOLDER2 = "Folder 2"
const PATH1 = BASE_PATH + FOLDER1 + "/" + FILENAME
const PATH2 = BASE_PATH + FOLDER2 + "/" + FILENAME

//Initialise our iCloud object
let fmCloud = FileManager.createCloud(false);

//Create the folders we'll move the file between
fmCloud.createDirectory(FOLDER1, BASE_PATH);
fmCloud.createDirectory(FOLDER2, BASE_PATH);

//Write the file into the first folder
fmCloud.writeString(PATH1, FILE_CONTENT);

//Read the file back from the first folder
fileInfo(1, PATH1)

//Move the file from the first folder to the second folder
if (fmCloud.moveItem(PATH1, PATH2, true))
{
	//Read the file back from the first folder ... it should come back empty this time
	fileInfo(1, PATH1)

	//Read the file back from the second path
	fileInfo(2, PATH2)
}
else
{
	alert("File copy failed ... but I don't know why")
}

function fileInfo(p_intPath, p_strPath)
{
	let fmCloudInfo = FileManager.createCloud();
	alert("PATH (" + p_intPath + ") = " + p_strPath + "\n\n---\n" + fmCloudInfo.readString(p_strPath) + "\n---")
}

Note: I’ve put an if-else in at one point where it doesn’t ‘necessarily’ require it … but that’s just because I’ve noted some odd behaviour in my beta version of the Drafts app and I’d rather you got a warning if the move fails for you in the same circumstances. I’ve dropped Greg a note with some details to see if he can shed any light on it.

If I get chance I’ll have a dig into the Dropbox API for the rpcRequest. I know that a move file is a straight forward request just requiring the full source and destination paths. It’s just a case of getting the paths and it isn’t something I’ve had need to look at before - but that just makes it a more worthwhile excuse to be something I take a look at :laughing:

3 Likes
#5

Apparently the Dropbox thing I’d looked at first wasn’t quite what it should have been. It actually turned out to be straight forward enough that I could put an example together while waiting in line.

Here’s an example similar to the iCloud one above. As long as you have the folders in place you should be good to go. Hopefully the comments explain enough for how it works.

//Create Dropbox object
let dbxMain = Dropbox.create()

//Build some file info
let strID = Date.now()
let strSource = "/Temp/source/" + strID+ "_test.txt"
let strDestination = "/Temp/destination/" + strID+ "_test.txt"
let strContent = "doh rei mi fah so lah tee doh"

//Write a test file and show what the content is for the source and destination
dbxMain.write(strSource, strContent, "overwrite", "false")
//Source should be the content and destination should be undefined
alert("Source:\n" + dbxMain.read(strSource))
alert("Destination:\n" + dbxMain.read(strDestination))

//Build the arguments for calling the Dropbox move
let urlEndpoint = "https://api.dropboxapi.com/2/files/move_v2";
let dictArgs = {
	"from_path": strSource,
	"to_path": strDestination,
	"allow_shared_folder": false,
	"autorename": false,
	"allow_ownership_transfer": false
};

//Move the file
let objResp = dbxMain.rpcRequest(
{
	"url": urlEndpoint,
	"method": "POST",
	"data": dictArgs
});

//Baseline check if it worked
if (objResp.statusCode != 200)
{
	//It didn't :o(
	alert("Dropbox Returned an Error: [" + objResp.statusCode + "] " + objResp.error)
	context.fail()
}
else
{
	//It did :o)
	//Show what the content is for the source and destination
	//Source should be undefined and destination should be the content
	alert("Source:\n" + dbxMain.read(strSource))
	alert("Destination:\n" + dbxMain.read(strDestination))
}

Hope that helps.

2 Likes
#6

Both are great. Will check these out and hope it’s ok to come back with the odd question if need be.

Thanks!!

1 Like
#7

A quick follow-up on my earlier statement. This does look to be a bug. Greg has been able to reproduce it based on the example I sent him and is investigating.

1 Like
#8

Is your Dropbox folder on a computer that uses Hazel? Your Mac could move/edit/delete the files in the background using Hazel pretty easily.

1 Like
#9

Hi! I use Hazel regularly, but can’t use for this specific use case…

#10

Did either of the above options meet your needs then?

#11

Playing with it and its vars (have not as much time as I’d like now)

But will keep posted. Thanks!!