Drafts Action Directory

EDAPI

Posted by @olivierg, Last updated: 2018-07-04 14:39:36 UTC

Extended Drafts: a collection of utilities for Drafts scripts, mostly aimed at generating actions and importing them in Drafts.

Meant to be used via “Include Action” action step.

See this topic for details.

Install

Steps

  • script

    // ED stands for Extended Drafts
    
    class EDUtils {
      
      static buildUUID() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
          var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
          return v.toString(16).toUpperCase();
        });
      }
      
      static toBoolean(value, defaultValue) {
        return (typeof(value) == typeof(true)) ? value : defaultValue;
      }
      
      static escapeQuote(str) {
        return str.replace(/"/g, '\\\"');
      }
      
      static includeScript(path) {
        var fm = FileManager.createCloud();
        var code = fm.read(path);
        return eval(code);
      }
      
    }
    
    class EDPrompt {
    
      static confirm(title, message, button) {
        var p = Prompt.create();
        p.title = title;
        p.message = message;
        p.addButton(button);
        return p.show();
      }  
      
      static choice(title, message, choices) {
        var p = Prompt.create();
        p.title = title;
        p.message = message;
        choices.forEach(c => p.addButton(c));
        if (p.show()) return p.buttonPressed;
        else return false;
      }
      
    }
    
    class EDAction {
      
      constructor(name, options) {
        if (options == undefined) options = {};
        this.uuid = EDUtils.buildUUID();
        this.name = name;
        this.description = (options.description != undefined) ? options.description : "";
        this.icon =  (options.icon != undefined) ? options.icon : "";
        this.tintColor =  (options.tintColor != undefined) ? options.tintColor : "none";
        this.steps = [];
      }
      
      addStep(step) {
        this.steps.push(step);
      }
      
      get jsRepresentation() {
        return {
          uuid: this.uuid,
          name: this.name,
          actionDescription: this.description,
          icon: this.icon,
          tintColor: this.tintColor,
          steps: this.steps.map(s => s.jsRepresentation),
          shortName: "",
          shouldConfirm: false,
          disposition: 3,
          keyCommand: {
            optionKey: false,
            input: "",
            controlKey: false,
            commandKey: false,
            type: "action",
            discoverabilityTitle: this.name,
            shiftKey: false
          },
          logLevel: 2,
          notificationType: 2,
          keyUseIcon: false,
          visibility: 2,
          supportedPlatform: "any",
          groupDisposition: 0
        };
      }
      
      get json() {
        return JSON.stringify(this.jsRepresentation);
      }
      
      get url() {
        return "drafts5://action?data=" + encodeURIComponent(this.json);
      }
      
      addToDrafts() {
        return app.openURL(this.url);
      }
      
      static script(name, options, dependencies, script) {
        var a = new EDAction(name, options);
        if (!Array.isArray(dependencies)) dependencies = [dependencies];
        dependencies.forEach(d => a.addStep(EDActionStep.includeAction(d)));
        a.addStep(EDActionStep.script(script));
        return a;
      }
      
      static includeScript(name, options, paths) {
        var a = new EDAction(name, options);
        a.addStep(EDActionStep.includeAction('EDAPI'));
        if (!Array.isArray(paths)) paths = [paths];
        paths.forEach(p => a.addStep(EDActionStep.includeScript(p)));
        return a;
      }
      
    }
    
    class EDActionStep {
      
      constructor(type, data) {
        this.uuid = EDUtils.buildUUID();
        this.type = type;
        this.data = data;
      }
      
      get jsRepresentation() {
        return {
          uuid: this.uuid,
          type: this.type,
          data: this.data
        };
      }
      
      static script(script) {
        return new EDActionStep("script", {
          script: script
        });
      }
      
      static includeScript(path) {
        return EDActionStep.script("EDUtils.includeScript('" + path + "');");
      }
      
      static includeAction(name) {
        return new EDActionStep("includeAction", {
          name: name
        });
      }
      
      static url(template, options) {
        if (options == undefined) options = {};
        return new EDActionStep("url", {
          template: template,
          useSafari: EDUtils.toBoolean(options.useSafari, true),
          encodeTags: EDUtils.toBoolean(options.encodeTags, true)
        });
      }
      
      static callbackURL(template, options) {
        if (options == undefined) options = {};
        return new EDActionStep("callbackUrl", {
          template: template,
          waitForResponse: EDUtils.toBoolean(options.waitForResponse, true),
          encodeTags: EDUtils.toBoolean(options.encodeTags, true)
        });
      }
      
    }
    

Options

  • After Success Default
    Notification None
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.