Importing a named text file from iCloud


#1

I am new to Drafts5 and scripting, but tried using the scripting action to import a named text file from iCloud to create a new draft. I know you can manually import a file by long pressing the new Drafts import button, but want to do this using script as part of a larger multi-step action.

I set up a folder called Test at the root level of my iCloud directory and put a text file in there called “Test.txt” with some placeholder text in it.

The code I used to read the file was cut from the Drafts Filemanager and Drafts reference and pasted into the script action as follows:

// read from file in iCloud
var fmCloud = FileManager.createCloud(); // iCloud
var myDraft = fmCloud.read("/Test Folder/Test.txt")
var d = Draft.create();
d.content = myDraft;
d.update();

I can read the file okay but can’t seem to write it on to the blank draft I ran the action from or to a new draft in my Draft’s inbox.

Does anyone know how to write the content of the text file I read to a draft? I saw the write the method but don’t know how to use it.

Thanks

Stephen


#2

That code is creating a draft properly, but I suspect it’s an empty draft because you cannot read that file.

The FileManager can only read and write from the “Drafts” app folder and it’s subdirectories in iCloud Drive. Details on docs page. You might want to add an alert(myDraft) call to see what you are getting back from that read.

If you want to open the draft you create, you can add editor.load(d); at the end of that script.


#3

Cheers Agiletortoise I will give it a go and thanks for creating such an excellent app.

Regards

Stephen


#4

Works very well. Thanks for the prompt reply.


#5

I’m pleased it works. What was the learning point that affected your case?


#6

I learned that the file manager only works on files in the drafts folder in iCloud. I also learned how to read and write text to the draft I ran the action on.