Action

Bear - Tag & Send

Posted by sylumer, Last update over 3 years ago - Unlisted

Based on an original action by @DerekVan
https://actions.getdrafts.com/a/1YM

Modified to cater for Mac, and multiple devices of the same model.
Modified to deal with tags with spaces in them.
Extended the details of when it will and won’t run and modify content.
Modified to only update the draft if necessary.
Modified send to oly send on OK and only if a parameter is set to the main function.

Steps

  • script

    //Generate UUID
    function genUUID(a,b)
    {
    	for(b=a='';a++<36;b+=a*51&52?(a^15?8^Math.random()*(a^20?16:4):4).toString(16):'-');
    	return b;
    }
    
    
    //Get UUID for this device
    //A generated UUID is held in a text file in local storage
    function deviceUUIDforBear()
    {
    	const BEARPATH = "/LocalBearID.txt";
    	let fmLocal = FileManager.createLocal();
    
    	let strID = fmLocal.readString(BEARPATH);
    	if (strID == undefined)
    	{
    		strID = genUUID();
    		fmLocal.writeString(BEARPATH, strID);
        }
    	return strID;
    }
    
    function getTagsAndSendtoBear(p_strAutoSend = true)
    {
        //Get/store the UUID
    	let strDeviceID = deviceUUIDforBear();
        let credBear = Credential.create(`Bear Token (${strDeviceID})`, "API Token for Bear App");
        credBear.addPasswordField("API Token", "Bear API token for this device");
        credBear.authorize();
    	let strAPIToken = credBear.getValue("API Token");
    
    
        //Get the list of tags from Bear and put them in an array
        let cbBear = CallbackURL.create();
        cbBear.baseURL = "bear://x-callback-url/tags?";
        cbBear.addParameter("token", strAPIToken);
        if (cbBear.open()) console.log("Tags requested");
        else
        {
            console.log(cbBear.status);
            if (cbBear.status == "cancel") context.cancel();
            else context.fail();
    	}
    	let aobjTags = JSON.parse(cbBear.callbackResponse.tags);
    
    
        //Populate an array of tag names
        let astrTagNames = [];
        for (let tag in aobjTags)
        {
    		let strName = aobjTags[tag].name.trim();
    		if(strName.length > 0) astrTagNames.push(strName);
        }
    
        //Select tags you want
        let promptTags = Prompt.create();
        promptTags.title = "Select the existing tags that you wish to add to the draft when imported to Bear:";
        promptTags.addSelect("tag_selection", "Select multiple...", astrTagNames, [], true);
        promptTags.addButton("OK");
    
    	if (promptTags.show())
    	{
    		//Cater for tags with spaces and build an array of Bear has tags to add to the draft
    		let astrHashedTags = [];
    		promptTags.fieldValues["tag_selection"].forEach(function(strTagName)
    		{
    			if(strTagName.includes(" ")) astrHashedTags.push("#" + strTagName + "#");
    			else astrHashedTags.push("#" + strTagName);
    		});
    		
    		//If there are hash tags to add, update the draft
    		if (astrHashedTags.length > 0)
    		{
    			draft.content = draft.content + "\n\n" + astrHashedTags.join(" ");
    			draft.update();
    		}
    
    		//Send to Bear if the auto send option was set
    		if (p_strAutoSend)
    		{
    			cbBear.baseURL = "bear://x-callback-url/create?";
    			cbBear.addParameter("text", draft.content);
    			if (cbBear.open()) console.log("Draft imported to Bear");
    			else
    			{
    				console.log(cbBear.status);
    				if (cbBear.status == "cancel") context.cancel();
    				else context.fail();
    			}
    		}
    	}
    }
    
    getTagsAndSendtoBear();

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.