Set Selected Range Issue

I wrote a script that works with Blink affiliate linking app and creates a markdown link in a draft similar to Greg’s initial markdown link action. That action can be found here.

The issue I’m having is if I have more than one affiliate link in the draft. The first one correctly replaces the selected text with the link. Any after that will add on to the end of the Draft instead of replacing the selected text. What have I done wrong here?

From looking at your script, it looks like you’re just replacing selected text with the link created by the action. That will only replace the selected text.

If you want the action to do an find and replace for all instances of an app name, you’ll have to add logic to do that longer find and replace on the entire draft, rather than just replacing the selected text.


I’m not trying to find all instances. I’m running the action again with a new selection. This causes the text to be inserted at the end instead of replacing the selection.

Sorry my first post wasn’t clear.

Here’s the script if it makes it easier for anyone to reference figure out. Help anyone?

// 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 =;
	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", "");
		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");;
	var style = ps.buttonPressed;
	if (style == "Markdown Link") {
		var link = selectAndSearch();
	else {
		var link = selectAndSearch();
		editor.setSelectedText("["+sel+"](" + link + ")");

var complete = linkStyle();

Hmmm, I’m not sure where the problem is. I don’t have Blink so I can’ t really test out your action.

If this really only happens when the action is run a second time on a draft, then that suggests there’s some sort of persistant variable or bug that isn’t getting cleaned out when the action finishes the first time.

But I’d want to make sure that the different results are not coming from making some different choices in how to run the action. (Did you have a selection the first time, but not the second? Did you choose the same link format both times? Did you use the same search term?)

Everything is the same except sometimes the search term. I’ve had it happen with the same search term and with different ones. It didn’t matter which.

@agiletortoise I hate to call in the big guns and try and get your attention, but variables shouldn’t persist between different actions and that’s the only theory we have right now as to why this is happening. Any thoughts?

I think I see what’s happening here…it’s a bug with the callback responses not getting cleared after an action runs. Will investigate. If you reload the draft it should clear.

1 Like

Found this. It will be fixed in version 5.0.3.