Action

Send multiple to iA Writer

Last update over 1 year ago

Sends multiple drafts as separate files to iA Writer using the new x-callback-URL scheme

Allows you to choose the folder to send the drafts to. Note that the folder must exist or the action will fail

As written, drafts are chosen from the inbox, the search criteria can be changed. See the draft.query documentation (https://reference.getdrafts.com/objects/Draft.html)

Filename is set to the file safe version of the draft title. Note: The action uses the iA Writer ‘create’ option, which renames duplicates rather than replacing

Requires auth token, which can be found in iA Writer -> Settings -> URL Commands

The auth token will be requested on the first run of the action and will be stored for future use.

Steps

  • script

    
    const baseURL = "ia-writer://x-callback-url/write/";
    
    var credential = Credential.create("IA-Writer", "IA-Writer Token");
    
    credential.addTextField("token", "Token");
    
    var result = credential.authorize();
    
    if (!result) {
    
       alert("Failed to obtain credentials. Please check and retry");
       context.cancel("Failed to obtain credentials");
    
    }
    else {
    
       // Find all drafts in the inbox - could be changed to another folder, or to include tags
       var inboxDrafts = Draft.query('','inbox',[]);
    
       var myDrafts = [];
    
       // Put all the title lines in an array for the prompt
       for (d in inboxDrafts) {
          myDrafts.push(inboxDrafts[d].title);
       }
    
       var p = Prompt.create();
       p.title = "Choose Drafts"
       p.addTextField("path", "Save Path", '/', {})
       p.addSelect("theDrafts","Drafts",myDrafts,[],true);
       
       p.addButton("Ok");
       p.show();
    
       if (p.buttonPressed == "Ok") {
    
          var chosenDrafts = p.fieldValues["theDrafts"];
          if (chosenDrafts.length == 0) {
             alert("No Drafts were chosen");
          }
          else {
    
    			basePath = p.fieldValues["path"]
    			if (basePath.slice(-1) != "/") {
    				basePath += "/"
    			}
    			for (var d=0;d < chosenDrafts.length; d++) {
    
                var a = myDrafts.indexOf(chosenDrafts[d]);
                // create and configure callback object
                var cb = CallbackURL.create();
                cb.baseURL = baseURL;
                
                cb.addParameter("auth-token", credential.getValue("token"));
                cb.addParameter("path", basePath+inboxDrafts[a].processTemplate("[[safe_title]]"+".txt"));
                cb.addParameter("text", inboxDrafts[a].content);
    
                var success = cb.open();
    
                if (success) {
                   console.log("File created: "+ cb.callbackResponse.path);
                }
                else {
                   if (cb.status == "cancel") {
                      context.cancel();
                   }
                   else {
                      context.fail(cb.callbackResponse.errorMessage);
                   }
                }
             }
          }
       }
    }

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.