How to build the add project tag (from Preferences)


Items marked as done can be moved to the Archive project, and with a Preferences option enabled the project in which they resided can be added in a @project tag when doing this.

I would like to move entries to projects different from Archive (e.g. a “Maybe Later” project) and have the same feature to add the project tag.

How do I go about creating a script that does that?

Thank you!!



Here’s TaskPaper’s code for implementing “Archive Done”:

archiveDone = (editor) ->
  outline = editor.outline
  selection = editor.selection
  startItem = selection.startItem
  endItem = selection.endItem
  archive = outline.evaluateItemPath("//@text = Archive:")[0]
  doneItems = Item.getCommonAncestors(outline.evaluateItemPath("//@done except //@text = Archive://@done"))
  removeExtraTags = Birch.preferences.get('BRemoveExtraTagsWhenArchivingDone')
  addProjectTag = Birch.preferences.get('BIncludeProjectWhenArchivingDone')

  outline.groupUndoAndChanges ->
    unless archive
      outline.root.appendChildren(archive = outline.createItem('Archive:'))
    for each in doneItems
      if removeExtraTags
        for eachName in each.attributeNames
          if eachName.indexOf('data-') is 0 and eachName isnt 'data-type' and eachName isnt 'data-done'
      if addProjectTag
        if projects = (eachProject.bodyContentString for eachProject in outline.evaluateItemPath('ancestor::@type=project', each)).join(' / ')
          each.setAttribute('data-project', projects)
      if (each is startItem or each.contains(startItem)) or (each is endItem or each.contains(endItem))
        if previousItem = editor.getPreviousDisplayedItem(startItem)
          selection = startItem: previousItem, startOffset: -1
          selection = start: 0
    archive.insertChildrenBefore(doneItems, archive.firstChild)

Note that the code is Coffee Script, but that’s translated into JavaScript during compile process. So syntax will be different when converting to JavaScript, but all the same API should be present. Anyway I hope that gets you started.