@tag(date) template

Script key

Posted by @nahumck

Install

Details

Label @tag(date) 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 nowdate = new Date();
var yyyy = nowdate.getFullYear();
var mm = nowdate.getMonth()+1;
var dd = nowdate.getDate();

if(dd < 10) {
    dd = '0' + dd;
} 

if(mm < 10) {
    mm = '0' + mm;
}

var today = yyyy+'-'+mm+'-'+dd+'';
var modifiedItem = " "+selTag+"("+today+")";

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