Action

Write 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

    // Write Projects to JSON and text files, so that they can be read back into Drafts using the JSON UUID key values
    
    // Get an array of all workspaces for populating the prompt below
    let get = Workspace.getAll();
    let workspaces = [];
    for (let space of get)
    {
        workspaces.push(space.name);
    }
    
    
    // Create prompt for choosing workspace, filter, and file extension
    
    let p = Prompt.create();
    p.title = "Save Files";
    p.message = "Choose Workspace, filter, and file extension for text files";
    
    p.addSelect("ws","Workspace",workspaces,[""],false);
    p.addSelect("filter","Filter",["inbox","archive","flagged","all","trash"],[""],false);
    p.addSelect("ext","File Extension",["md","txt","other"],[""],false);
    p.addTextField("cExt","Custom Extension (don't include period)","");
    p.addButton("OK");
    
    if (p.show())
    {
        // Create the cloud manager for saving files & the json object for names/uuids
        let fmCloud = FileManager.createCloud();
        
        // Get JSON file or create a new one if it doesn't exist
        var jsonFile = fmCloud.readString("/JSON/files.json")
        if (jsonFile)
        {
            var json = JSON.parse(jsonFile);
        }
        else
        {
            var json = {};
        }
        
    
        // Get the selected workspace
        let spaceName = (p.fieldValues["ws"]);
        let ws = Workspace.find(spaceName);
    
        // Either erase the existing workspace uuids from the json or create the object if this is the first time saving this workspace
        let filter = p.fieldValues["filter"];
        if (Object.hasOwnProperty(json[spaceName]) == true)
        {
            json[spaceName] = {};
            
        }
        
        json[spaceName][filter] = {};
        json[spaceName][filter]["Drafts"] = {};
    
    
        // Query for the drafts in the selected workspace
        let files = ws.query(filter);
    
        // Get the extension value
        var ext = "";
        if (p.fieldValues["cExt"])
        {
            ext = "." + p.fieldValues["cExt"];
        }
        else
        {
            ext = "." + p.fieldValues["ext"];
        }
    
        // Grab each draft in workspace
        let j = 0; // title-less drafts counter
        for (d of files)
        {
            // Create title if draft has a blank first line
            let i = d.processTemplate("[[safe_title]]").replace("# ",""); // Feel free to change this replace formula if your files don't start with a markdown hash header
            if (i.length == 0)
            {
                i = "Draft " + j;
                j++
            }
    
    
            let uuid = d.uuid; // get draft uuid
            let title = i; 
            let content = d.content; // get draft content
            fmCloud.writeString("/Files/" + spaceName + "/" + filter + "/" + title + ext,content); // write draft to Files folder, workspace name subfolder, in iCloud drive
            json[spaceName][filter]["Drafts"][title] = uuid; // create key for draft title and uuid in a json object keyed to workspace name
            json[spaceName][filter]["TextFileExtensionName"] = ext;
        }
        
        // Write the JSON file
        let output = JSON.stringify(json);
        let success = fmCloud.writeString("/JSON/files.json",output);
        }
        
    else
    {
        context.cancel();
    }
    
    

Options

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