Action

Insert Ref & Copy URL

Posted by @sylumer, Last update over 5 years ago - Unlisted

Steps

  • script

    //Settings
    const RID_SEPARATOR = "_";
    const REF_SEPARATOR = " | ";
    const REF_DELIMITER_START = "<!--{";
    const REF_DELIMITER_END = "}//-->";
    
    //Insert a reference (that won't be shown when converted to HTML), has a unique ID and also
    //  return a Drafts x-callback URL that can be used to locate the unique ID (using another action)
    function insertReference()
    {
    	let refPrompt = Prompt.create();
    
    	refPrompt.title = "Reference";
    	refPrompt.message = "Enter some explanatory text";
    	refPrompt.addTextField("RefExplain", "", "",
    	{
    		"wantsFocus": true
    	});
    	refPrompt.addButton("OK");
    
    	if (refPrompt.show())
    	{
    		if (refPrompt.buttonPressed == "OK")
    		{
    
    			let strID = draft.uuid + RID_SEPARATOR + (new Date).getTime(); //Unique position ID
    			
    			//Build the text to insert
    			let strInsert = REF_DELIMITER_START;
    			strInsert += strID + REF_SEPARATOR + refPrompt.fieldValues["RefExplain"]; //Position ID & Description
    			strInsert += REF_DELIMITER_END;		
    			//Insert the reference
    			editor.setSelectedText(strInsert);
    			editor.activate();
    			
    			//Return the URL
    			return "drafts5://x-callback-url/runAction?text=" + strID + "&action=OpenRef";
    		}
    	}
    }
    
    //Copy URL to clipboard but we could programmatically do something else here
    //e.g. Viticci probably sets a reminder such that he can trigger the URL from GoodTasks 3
    //	(reminders does not support simple actionable URLs in notes natively) 
    app.setClipboard(insertReference());
    

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.