This is a cool function that I use often. When you select content and invoke the function, it either creates a comment from the selection, or if your selection is a comment, the second function will uncomment the content. Why this is cool is becuase, you may have some content that you want removed. But you may be unsure. Instead of deleting or saving a backup copy, I like to make it a comment. Comments are for human readers only. Editors and composition engines will ignore comments.
The following two functions, (commentSelection and uncommentSelection) was written by Clay Helberg. I added to my menu, “Tech Data”. I will have a future article on how to write menus. So below is just a snipnet of a menu.
-cmd {js_eval("commentSelection()") }
-active "cut_valid()"
-help "Wrap selected content in an SGML comment to hide it"
menu_add ".TechData." "Uncomment out selection"
-cmd {js_eval("uncommentSelection()") }
-active "cut_valid()"
-help "Unwrap selected content in an SGML comment to show it"
Making a Comment
To make a “comment function”, copy this code function below and place it in a file called comment.js. The file should be placed in your <arbortext-installation>/custom/scripts folder.
// delimiters, and then pastes the resulting
// comment (supplied by Clay Helberg)
function commentSelection() {
// get current selection as string
var doc = Application.activeDocument;
var selection = doc.textSelection;
var selection_text = selection.toMarkupString();
// remember where we are
var start = selection.startContainer;
var offset = selection.startOffset;
// generate the comment element
var comment = doc.createComment(selection_text);
// paste the comment back in
selection.deleteContents();
selection.setStart(start,offset);
selection.insertNode(comment);
}
Make a selection (it must be a spot where your selection would not make the file invalid when the content is removed), in your document. For my example, I chose a chapter.

Make a valid selection
If you are using the menu, select comment out selection.

Menu Item Comment out selection
If you want to use the command line, it is a bit verbose (good reason to make a menu item).

Command line comment selection

Results - one long comment
Returning a Commented Section
To return a comment back into valid content, place the code below into a file called uncomment.js. The file should be placed in your <arbortext-installation>/custom/scripts folder.
// delimiters, and then pastes the resulting
// content (supplied by Clay Helberg)
function uncommentSelection() {
COMMENT_NODE = 8; // W3C enumeration for node type
// get current selection
var doc = Application.activeDocument;
var selection = doc.textSelection;
var selection_text = selection.toMarkupString();
// if it’s not a comment node, abort
// the operation
if (selection_text.substring(0,4) != "<!–") {
Application.alert("Selection is not a comment: ‘" + selection_text + "’");
return;
}
// remove the comment delimiters
var content = selection_text.substring(4,selection_text.length()-3);
// paste the comment content back in as markup
selection.deleteContents();
selection.insertParsedString(content);
}
Returning a comment back to content is pretty much the reverse of making the comment. Select the comment, and from the command line, js_eval(“uncommentSelection()”) or if using the menu, select Uncomment out selection.
