Drafts Action Directory

Blink Affiliate Search

Last update 4 months ago - Unlisted

This script was made to work with the app Blink: Better Affiliate Links to generate affiliate links from Apple’s App and iTunes stores. The script does a few different things.

  1. If text is selected in Drafts’ editor, the selected text will become the search term in Blink. If not, a prompt will appear asking you to enter a search term.
  2. The script will ask you if you want to use Blink’s Plain or Markdown link style. This does not tell Blink which style to use as Blink has no way of accessing that function programatically. Instead, it tells Drafts how to format the link when it is placed into the editor. Selecting Plain Link will keep the term you selected in the editor and searched for, but wrap it in a markdown link. Selecting Markdown link will completely replace your search term with the official title from Apple’s store and enclose that in a markdown link.
  3. The script will insert the link in the style that you previously selected once it receives the link from Blink.

This script works best as a keyboard action instead of in an action list.

Install

Steps

  • script

    // Blink Affiliate Search
    
    const baseURL = "blink://x-callback-url/search"
    
    var sel = editor.getSelectedText();
    var selRange = editor.getSelectedRange();
    
    // Create a callback search and return the affiliate URL from Blink. The variable created when this is called will be used in the second function below to use the selection to search.
    
    function searchBlink(searchTerm) {
    	var cb = CallbackURL.create()
    	cb.baseURL = baseURL;
    	cb.addParameter("q", searchTerm);
    	// Open and wait for result
    	var success = cb.open();
    	if (success) {
    		var response = cb.callbackResponse;
    		var affLink = response["result"];
    		return affLink
    	}
    }
    
    // If nothing is selected, the user will be prompted to enter a search term. If there is a selection, that will be the search term. This will then call the searchBlink() function.
    
    function selectAndSearch() {
    	if (!sel || sel.length == 0) {
    		var p = Prompt.create();
    		p.title = "Search Term";
    		p.message = "What would you like to search Blink for?";
    		p.addTextField("Query", "Search", "");
    		p.addButton("Done");
    		p.show();
    		var search = p.fieldValues["Query"];
    	}
    	else {
    		var search = sel
    	}
    	var affiliate = searchBlink(search);
    	return affiliate
    }
    
    function linkStyle() {
    	var ps = Prompt.create();
    	ps.title = "Link Style";
    	ps.message = "Use Blink's Standard or Markdown Link?";
    	ps.addButton("Markdown Link");
    	ps.addButton("Plain Link");
    	ps.show();
    	var style = ps.buttonPressed;
    	
    	if (style == "Markdown Link") {
    		var link = selectAndSearch();
    		editor.setSelectedText(link);
    		editor.setSelectedRange(selRange[0]+1,0);
    	}
    	else {
    		var link = selectAndSearch();
    		editor.setSelectedText("["+sel+"](" + link + ")");
    		editor.setSelectedRange(selRange[0]+selRange[1]+link.length+4,0);
    		}
    }
    
    var complete = linkStyle();

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.