/* psConvertText.mel J. Adrian Herbez www.purplestatic.com This script was created to aid in writing meta-level code. To use, paste some MEL code into the text field. Clicking on the "up a level" button will automatically escape all quotes and backslashes in the text, making it ready for use at a higher level of use. The script also provides a quick and easy way to replace specific components (such as the names of objects) with variable names. To use that, be sure to select the "Insert variables" checkbox. Expand the replacement options tab. Enter the target text (such as transform1 or nurbsSphere12) into "Text to replace" field, and the name of the variable you want to use ($objectName, perhaps) into the "Variable name" section, and click the "Add new replace text" button. You'll see the target and replacement value appear in the lists to the right. To clear the lists, use the "Clear replace list" button. The script surrounds the literal portions with quotes and places each variable on a separate line, with plus signs inbetween the literals and variables. The script can optionally escape whitespace characters (newlines, carriage returns, and tabs), compressing the input code into a single long string. This is especially useful when creating script nodes and expressions, as it ensures that the text in the expression editor will be formatted exactly as it appears. If at any time you want to go back to an earlier version of the input text, click on the "back" button. Enjoy! */ global string $oldText[]; global int $otSize; global proc psConvertText() { global string $oldText[]; global int $otSize; $oldText = {}; $otSize = 0; string $winName = `window -title "Text Converter" -rtf 1`; columnLayout; scrollField -width 500 -height 200 inText; rowLayout -nc 3 -columnWidth3 200 150 100; button -width 180 -label "Up a level" -command "changeText()"; checkBox -label "Escape whitespace" -value 0 wSpace; button -width 100 -label "Back" -command "revert()"; setParent..; frameLayout -label "Variable replacement" -cll 1 -cl 0 -width 500 replacer; rowLayout -nc 3 -columnWidth3 300 100 100; columnLayout; checkBox -label "Insert variables" -value 1 varSwitch; rowLayout -nc 2 -columnWidth2 80 220; text -label "Text to replace: "; textField -width 200 newName; setParent..; rowLayout -nc 2 -columnWidth2 80 220; text -label "Variable name: "; textField -width 200 newVar; setParent..; button -width 150 -label "Add new replace text" -command "addText()"; button -width 150 -label "Clear replace list" -command "cleartext()"; setParent..; columnLayout; text -label "Text to Replace:"; textScrollList -width 75 -height 100 nameList; setParent..; columnLayout; text -label "Variable names: "; textScrollList -width 75 -height 100 varList; setParent..; setParent..; setParent..; showWindow $winName; } global proc addText() { string $theName = `textField -q -text newName`; string $varName = `textField -q -text newVar`; textScrollList -e -append $theName nameList; textScrollList -e -append $varName varList; textField -e -text "" newName; textField -e -text "" newVar; } global proc cleartext() { textScrollList -e -ra nameList; textScrollList -e -ra varList; } global proc changeText() { global string $oldText[]; global int $otSize; string $new = `scrollField -q -text inText`; $oldText[$otSize] = $new; $otSize++; $new = substituteAllString($new,"\\","\\\\"); $new = substituteAllString($new,"\"","\\\""); if (`checkBox -q -value wSpace`) { $new = substituteAllString($new,"\t","\\t "); $new = substituteAllString($new,"\n","\\n "); $new = substituteAllString($new,"\r","\\r "); } string $names[] = `textScrollList -q -ai nameList`; string $vars[] = `textScrollList -q -ai varList`; if (`checkBox -q -value varSwitch`) { $new = ("\"" + $new + "\""); for ($i=0; $i<(size($names)); $i++) { string $repTarget = ("\"\r+\r" + $vars[$i] + "\r+\r\""); $new = replaceAll($names[$i], $new, $repTarget); } } scrollField -e -text $new inText; } global proc revert() { global string $oldText[]; global int $otSize; $otSize--; if ($otSize < 0) error("No more changes to undo"); scrollField -e -text $oldText[$otSize] inText; $oldText[$otSize+1] = ""; } global proc string replaceAll(string $input, string $target, string $new) { int $done = 1; string $temp; int $test; while ($done) { $temp = substitute($input, $target, $new); $test = strcmp($temp, $target); $target = $temp; if ($test == 0) $done = 0; } return $target; }