Adding iCloud Folders As Part of `getClient`

As I mentioned, I added an ability to add iCloud folders for a new client as part of my getClient action. While this functionality involves too many interdependencies to be appropriate for sharing via the Drafts Action Directory, I’ve been asked to share how it works. So if you’d like to add this for yourself, here’s how you can do it:

Step 1

Create a new Drafts action. Call the action something like add folders. The action should contain a single script step, which includes just the follow script (a single function):

function addFolders(client) {
    
    // ask if want to create folders
    let folderPrompt = Prompt.create();
    folderPrompt.title = "Folders?";
    folderPrompt.message = "Do you want to create iCloud folders and an empty comlog for " + client.nickname;
    folderPrompt.addButton("add folders");
    folderPrompt.addButton("skip making folders");
                
    folderPrompt.show();
                
    if (folderPrompt.buttonPressed == "add folders") {
                
        // invoke Scriptable to create folder structure and comlog
        const baseURL = "scriptable:///run";
        var cb = CallbackURL.create();
        cb.baseURL = baseURL;
        cb.addParameter("scriptName", "new client folders and comlog");
        cb.addParameter("client", JSON.stringify(client));
                
        let success = cb.open();
            if (!success) {
                context.fail("Failed to create new folders");
                console.log(cb.status);
            } 
            
    } else {
        console.log("User chose not to add folders");
    }
    
}

Step 2

Next, you’ll need to make two modifications to your getClient action.

First, add an “Include Action” step to the beginning of the getClient action. The action you want to include is the add folders action you created in Step 1. Make sure this step is before the main script step.

Second, edit the main script step by adding the following at line 82 (just after the line clientList.clients.push(newClient);):

    // give option to add folders
    addFolders(newClient);

(Note: You could just incorporate the function from Step 1 directly into the getClient script step. But it can make the whole script a bit unwieldy. Better to compartmentalize it as a separate action step.)

Step 3

Now you’ll have to go into Scriptable.

First, go into Scriptable’s settings and add a File Bookmark for the root level of your iCloud Drive and name the bookmark iCloud Drive. (You could name it something different, but then you’ll need to adjust the script below accordingly).

Next, create a script and call it new client folders and comlog. The content of that script should be as follows:

// this script is intended to be called by Drafts
// when a new client is added via the "getClient"
// function
//
// It will create an iCloud directory for the client
// with subfolders for admin, cliend documents, and working files 


// get client and success parameters from Drafts URL
const client = JSON.parse(args.queryParameters.client); 
const successURL = args.queryParameters["x-success"];


// set up basics
const fm = FileManager.iCloud();
const root = fm.bookmarkedPath("iCloud Drive");
const clientDirectoryPath = root + "/• Work/clients/" + client.nickname + " — " + client.number + "/";


// create folders
fm.createDirectory(clientDirectoryPath + "working files/", true);  // "true" ensures creation of intermediate directories
fm.createDirectory(clientDirectoryPath + "admin/");
fm.createDirectory(clientDirectoryPath + "client documents/");


// create a comlog (with an internal hashtag for 1Writer and similar)
fm.writeString(clientDirectoryPath + "working files/" + client.nickname + " comlog.md", "\n\n#comlog");
fm.addTag(clientDirectoryPath + "working files/" + client.nickname + " comlog.md", "comlog");


// return to Drafts
Safari.open(successURL);

Step 4

Cross your fingers, go back into Drafts, and use the get client number action to add a new client. After you tap “add”, you should be prompted to decide whether you want to add new folders, and, once you do, Drafts should open up Scriptable, which should add the folders, and then return you to Drafts. There may well be a few permission dialogs the first time or two that you run these scripts. But after you grant the appropriate permissions, it should all work quite smoothly.

Please note that I’ve created the folders and files in a structure that works for me. You’ll want to mess around with the folder paths and file names to suit your own uses. If you have any issues, please feel free to reach out for help.

Enjoy!

Ciaran Connelly @ciaran