@tag (template)

Script key

Posted by @nahumck

Install

Details

Label @tag (template)
Voice Over
Script
// Append the specified tag (@tag format) to each line selected 
//or insert the tag if not text is selected

// set your preferred tag here
var selTag = "@tag";
var modifiedItem = " "+selTag;

var lnRange = getSelectedLineRange();
var ln = getTextInRange(lnRange[0], lnRange[1]);
var selRange = getSelectedRange();

if (ln == "") {
  setSelectedText(modifiedItem);
  setSelectedRange(selRange[0] + modifiedItem.length, 0);
} else {
  var lines = ln.split('\n');
  var charsAdded = 0;

  for (var ix=0; ix < lines.length; ix++) {
    if (lines[ix].length > 0) {
      if (lines[ix][0] != selTag) {
        var prefix = selTag;
        if (lines[ix][0] != " ") {
          prefix = modifiedItem;
        }
        lines[ix] = lines[ix] + prefix;
        charsAdded = charsAdded + prefix.length;
      }
    }
  }
  setTextInRange(lnRange[0], lnRange[1],lines.join("\n"));
  if (lines.length > 2) {
    setSelectedRange(lnRange[0]+lnRange[1]+charsAdded, 0);
  }
  else {
    setSelectedRange(selRange[0]+charsAdded, selRange[1]);
  }
}

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus