Action

Read Files

Posted by @derekvan, Last update over 4 years ago

This pair of actions will write Drafts to text files, then allow you to update the Drafts later with the edited contents of the text files.

It works like this:

The Write Files action will ask you which workspace you want to choose, then which filter you want (e.g., inbox, flagged, all, archive, trash), then what you want to use for the file extension. It will then write the drafts to text files in the following folder structure in iCloud Drive “Drafts/Files/[Workspace Name]/[Filter name]/file.extension”

It will also create a JSON in iCloud drive (“Drafts/JSON/files.json”). This file will contain the names of the drafts and their UUIDs, as well as the file extension used.

The Read Files action will read this JSON file, provide a prompt allowing you to choose which workspace and filter you want to overwrite with the contents of the text files.

Notes:

  • The Write action will assign “Draft 0”, “Draft 1”, etc. file names to any Drafts without content in the first line. These names won’t be added to the Draft itself.
  • Each time you “write” Drafts, the JSON for that workspace and filter is overwritten. The text files will also be overwritten. However, if you delete a Draft, any text files you made of that Draft previously won’t be deleted or removed.
  • If you fail to choose an option in any of the menus, there will be a script error instead of a nice error message. I’m not sure how to add the logic to make a nicer error message.

Steps

  • script

    // Reads files back to drafts given uuids in json
    
    let fmCloud = FileManager.createCloud();
    let file = fmCloud.readString("/JSON/files.json");
    let json = JSON.parse(file);
    let workspaces = Object.keys(json);
    
    // Create prompt to choose workspace
    let p = Prompt.create();
    p.title = "Choose workspace to overwrite with text files";
    p.addSelect("ws","Available Workspaces",workspaces,[""],false);
    p.addButton("OK");
    
    if (p.show())
    {
        // Grab the listings of filtered files
        let ws = p.fieldValues["ws"];
        let filters = Object.keys(json[ws]);
        
        // Create prompt to choose which list
        let q = Prompt.create();
        q.title = "Which filtered list to overwrite with text files?";
        q.addSelect("f","Available Filters",filters,[""],false);
        q.addButton("OK");
        if (q.show())
        {
            // Get chosen filter
            let selFilter = q.fieldValues["f"];
    
            // Get names of text files
            let txtFiles = Object.keys(json[ws][selFilter]["Drafts"]);
    
            // Get file extension
            let ext = json[ws][selFilter]["TextFileExtensionName"];
    
            // Loop through text files
            for (d of txtFiles)
            {
                let drafter = json[ws][selFilter]["Drafts"][d];
                let content = fmCloud.readString("/Files/"+ws+"/"+selFilter+"/"+d+ext);
                let a = Draft.find(drafter);
                a.content = content;
                a.update();
            }
    
        }
        else
        {
            context.cancel();
        }
    
    }
    else
    {
        context.cancel();
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.