JavaScript in XUI manipulates main document

Code by Clay Helberg Last Updated: 2006-06-18

This simple example by Clay Helberg shows how an XUI widget can contain JavaScript code that manipulates the contents of the main edit window.

You'll need to use scripts in your XUI widgets to do things with the document. In this case, Application.activeDocument is your friend. That will (usually) get you the document in the main edit window, and once you have that you can traverse the document and have your wicked way with it. :-)

Here's an example of a XUI button that takes the highlighted text in the main document and converts it to all uppercase:

   // get document doc = Application.activeDocument; // get selection sel = doc.textSelection; start = sel.startContainer; offset = sel.startOffset; // convert to uppercase caps = sel.toString.toUpperCase; // replace selection with the converted text sel.deleteContents; sel.setStart(start,offset); sel.insertParsedString(caps); // close dialog dlgdoc = Application.event.target.ownerDocument; dlgdoc.close;

Note: this is very unpolished code, and actually somewhat dangerous, as it doesn't do any error checking before deleting the selection and trying to replace it with what may or may not be valid markup. You would want to tighten this up with error-handling code and so on before actually using it in a production system, but hopefully it illustrates how to access and manipulate the editor document pretty clearly.