Action

Bear: Retrieve Tags and Send

Posted by @derekvan, Last update over 5 years ago

This action requests the list of used tags from Bear, then presents a picker for you choose tags from. Great for enforcing tag discipline so you don’t end up with “note” “notes” and “Notes.” After you’ve chosen the tags, they will be put at the bottom of the draft and the whole thing will get sent to Bear.

The first time you run it, it will request your Bear API tokens for all devices, so have those handy. You can retrieve the token for each device from Bear in Settings—>General—>API Token. Unfortunately, each device you use will use a different token. Hopefully you only have one iPhone and one iPad—if you have multiple of either I’m pretty sure this won’t work right.

Steps

  • script

    // bear tag picker
    
    // Get Bear tokens for all devices
    
    var credential = Credential.create("Bear Tokens", "Bear requires these tokens to retrieve tags using the x-callback url requests. Unfortunately, the tokens are different on iPhone and iPad even with the same iCloud account so you will need to paste both in here when requested.");
    
    credential.addPasswordField("iphone", "iPhone token");
    credential.addPasswordField("ipad", "iPad token");
    
    credential.authorize();
    
    var model = device.model;
    var iphone = credential.getValue("iphone");
    var ipad = credential.getValue("ipad");
    
    // test whether device is iPhone or iPad to use the correct token
    
    if (model == "iPhone") {
    	var beartoken = iphone;
    }
    else if (model == "iPad") {
    	var beartoken = ipad;
    }
    
    // start callback
    
    const baseURL = "bear://x-callback-url/tags?";
    
    var cb = CallbackURL.create();
    	cb.baseURL = baseURL;
    	cb.addParameter("token", beartoken);
    
    var success = cb.open();
    	if (success) {
    		console.log("tags requested");
    	}
    	else { // something went wrong or was cancelled
    	  	console.log(cb.status);
    	  	if (cb.status == "cancel") {
    			context.cancel();
    		}
    		else {
    			context.fail();
    		}
    	}
    
    // Get callback response
    let response = cb.callbackResponse;
    
    
    // Parse JSON response into an array for the prompt below, getting the array in the "tags" key
    
    let result = JSON.parse(response.tags);
    
    //create a new empty array called "tags", then iterate over the "result" array pushing each property for "name" into that "tags" array.
    
    let tags = [];
    
    for (let item in result) {
    	
    	tags.push(result[item].name);
    	
    }
    
    // multiple selections prompt
    
    var p = Prompt.create();
    p.title = "choose tag";
    
    p.addSelect("s2", "Select multiple...", tags, [], true);
    
    p.addButton("OK");
    
    // append the selected tags with hashes to the bottom of the draft
    
    if (p.show()) {
    	var final="";
    	for (let t of p.fieldValues["s2"]) {
    		final += "#" + t + " ";
    	}
    }
    
    draft.content = draft.content + "\n\n" + final;
    draft.update();
    
    // now send the whole note to Bear. Obviously, delete this part or comment it out if you'd rather stay in Drafts for this action and send the note later.
    
    const url = "bear://x-callback-url/create?"
    
    var xb = CallbackURL.create();
    	xb.baseURL = url;
    	xb.addParameter("text", draft.content);
    
    var success = xb.open();
    	if (success) {
    		console.log("note created");
    	}
    	else { // something went wrong or was cancelled
    	  	console.log(xb.status);
    	  	if (xb.status == "cancel") {
    			context.cancel();
    		}
    		else {
    			context.fail();
    		}
    	}
    
    draft.update();

Options

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