Action

BearAPI

Posted by @olivierg, Last update over 6 years ago

Script API to allow easy manipulation of Bear notes.

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

See this topic for details

Steps

  • script

    class Bear {
      
      static get bearBaseURL() { return "bear://x-callback-url/"; }
      
      static formatNewNote(tags) {
        if (tags == undefined) tags = [];
        else if (!Array.isArray(tags)) tags = [tags];
        return "# " + draft.content + "\n\n---\n(from a draft created on " + draft.createdAt + ")\n" + tags.map(t => "#" + t).join(" ") + "\n";
      }
      
      static isNoteID(str) {
        return /^[A-Z0-9]+(\-[A-Z0-9]+)*$/.test(str);
      }
      
      static paramNameFor(note) {
        if (Bear.isNoteID(note)) return "id";
        else return "title";
      }
      
      static executeAction(action, parameters) {
        if (parameters == undefined) parameters = {};
        var cb = CallbackURL.create();
      	cb.baseURL = Bear.bearBaseURL + action;
      	cb.parameters = parameters;
        // open and wait for result
      	var success = cb.open();
      	if (success) {
      	  return cb.callbackResponse;
      	}
        else { // something went wrong or was cancelled
    	  	console.log(cb.status);
    	  	if (cb.status == "cancel") context.cancel();
      		else context.fail();
          return false;
      	}
      }
      
      static executeActionOn(action, note, parameters) {
        if (parameters == undefined) parameters = {};
        parameters[Bear.paramNameFor(note)] = note;
        return Bear.executeAction(action, parameters);
      }
      
      static open(note) {
        var url = Bear.bearBaseURL + "open-note?" + Bear.paramNameFor(note) + "=" + encodeURIComponent(note);
        return app.openURL(url);
      }
      
      static create(text, options) {
        if (options == undefined) options = {};
        if (options.pin == undefined) options.pin = false;
        return Bear.executeAction("create", {
          title: options.title,
      	  text: text,
          pin: options.pin ? "yes" : "no"
      	});
      }
      
      static createFromCurrentDraft(tags, options) {
        return Bear.create(Bear.formatNewNote(tags), options);
      }
      
      static addTextTo(note, mode, text) {
        if (text == undefined) text = draft.content;
        return Bear.executeActionOn("add-text", note, {
          mode: mode,
          text: text,
          exclude_trashed: 'yes'
        });
      }
      
      static prependTo(note, template, text) {
        if (text == undefined) text = draft.content;
        switch (template) {
          case "list":
            text = "- " + text;
            break;
          case "section":
            text = text + "\n---\n";
            break;
          case "plain":
          default:
            text = text;
        }
        return Bear.addTextTo(note, "prepend", text);
      }
      
      static appendTo(note, template, text) {
        if (text == undefined) text = draft.content;
        switch (template) {
          case "list":
            text = "- " + text;
            break;
          case "section":
            text = "\n---\n" + text;
            break;
          case "plain":
          default:
            text = text;
        }
        return Bear.addTextTo(note, "append", text);
      }
      
    }
    
    class BearNote {
      
      constructor(data) {
        this._identifier = data.identifier;
        this._content = data.note;
        this._title = data.title;
        this._isTrashed = data.is_trashed == "yes";
        this._creationDate = new Date(data.creationDate);
        this._modificationDate = new Date(data.modificationDate);
      }
      
      get identifier() { return this._identifier; }  
      get title() { return this._title; }
      get isTrashed() { return this._isTrashed; }
      get creationDate() { return this._creationDate; }
      get modificationDate() { return this._modificationDate; }
      
      get content() { return this._content; }
      
      set content(newContent) {
        this.addText("replace_all", newContent);
      }
      
      trash() {
        var result = Bear.executeActionOn("trash", this.identifier);
        this._isTrashed = true;
        this._modificationDate = Date.now();
        return result;
      }
      
      addText(mode, text) {
        var result = Bear.addTextTo(this.identifier, mode, text);
        this._title = result.title;
        this._content = result.note;
        this._modificationDate = Date.now();
        return result;
      }
      
      prepend(text) {
        return this.addText("prepend", text);
      }
      
      append(text) {
        return this.addText("append", text);
      }
      
      static fetch(note) {
        return new BearNote(Bear.executeActionOn("open-note", note));
      }
      
      static create(content) {
        return BearNote.fetch(Bear.create(content).identifier);
      }
    
    }
    

Options

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