-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreserveLocalFormatting.jsx
132 lines (119 loc) · 4.44 KB
/
PreserveLocalFormatting.jsx
1
//DESCRIPTION: Convert local formatting to equivalent character styles /* This script works on the current text selection if there is one. If not, it checks with the user */if (app.documents.length == 0) { exit() }theRange = checkSelection();if (theRange == null) exit();convertFormatting(theRange);function convertFormatting(range) { var myStyles = ["Italic", "Bold", "Bold Italic"]; var myPosns = ["superscript", "subscript"]; var aDoc = app.documents[0]; switch (Number(String(app.version).split(".")[0])) { case 3 : case 4 : processStylesPreCS3(range, myStyles, aDoc); processPosnsPreCS3(range, myPosns, aDoc); break; case 5 : processStylesCS3(range, myStyles, aDoc); processPosnsCS3(range, myPosns, aDoc); } } // end convertFormattingfunction processStylesPreCS3(range, myStyles, aDoc) { var noCharStyle = aDoc.characterStyles[0]; for (var j = 0; myStyles.length > j; j++) { var myCharStyle = aDoc.characterStyles.item(myStyles[j]); if (myCharStyle == null) { aDoc.characterStyles.add({name:myStyles[j],fontStyle:myStyles[j]}); } app.findPreferences = app.changePreferences = null; range.search("", false, false, undefined, {appliedCharacterStyle:noCharStyle, fontStyle:myStyles[j]}, {appliedCharacterStyle:myStyles[j]}); }}function processStylesCS3(range, myStyles, aDoc) { var noCharStyle = aDoc.characterStyles[0]; for (var j = 0; myStyles.length > j; j++) { var myCharStyle = aDoc.characterStyles.item(myStyles[j]); if (myCharStyle == null) { aDoc.characterStyles.add({name:myStyles[j],fontStyle:myStyles[j]}); } app.findTextPreferences = app.changeTextPreferences = null; app.findTextPreferences.appliedCharacterStyle = noCharStyle; app.findTextPreferences.fontStyle = myStyles[j]; app.changeTextPreferences.appliedCharacterStyle = myStyles[j]; searchText(range, "", undefined, false, false, true, true, true, false, true); }}function processPosnsPreCS3(range, myPosns, aDoc) { var noCharStyle = aDoc.characterStyles[0]; for (var j = 0; myPosns.length > j; j++) { var myCharStyle = aDoc.characterStyles.item(myPosns[j]); if (myCharStyle == null) { aDoc.characterStyles.add({name:myPosns[j], position:Position[myPosns[j]]}); } app.findPreferences = app.changePreferences = null; range.search("", false, false, undefined, {appliedCharacterStyle:noCharStyle, position:Position[myPosns[j]]}, {appliedCharacterStyle:myPosns[j]}); }}function processPosnsCS3(range, myPosns, aDoc) { var noCharStyle = aDoc.characterStyles[0]; for (var j = 0; myPosns.length > j; j++) { var myCharStyle = aDoc.characterStyles.item(myPosns[j]); if (myCharStyle == null) { aDoc.characterStyles.add({name:myPosns[j], position:Position[myPosns[j]]}); } app.findTextPreferences = app.changeTextPreferences = null; app.findTextPreferences.appliedCharacterStyle = noCharStyle; app.findTextPreferences.position = Position[myPosns[j]]; app.changeTextPreferences.appliedCharacterStyle = myPosns[j]; searchText(range, "", undefined, false, false, true, true, true, false, true); }}function checkSelection() { if (app.selection.length == 0) { if (!confirm("Process the whole document?")) { exit(); } theRange = app.activeDocument; } else { theRange = app.selection[0]; try { if (theRange.constructor.name.indexOf("TableCell") != -1 && theRange.insertionPoints.length == 1) { theRange = getParentTextFlow(theRange); } } catch (e) { // selection is not text so app.selection = null; checkSelection(); return null; } } return theRange} // end checkSelectionfunction getParentTextFlow(theTextRef) { if (theTextRef.parent.constructor.name == "Cell") { return theTextRef.parent.texts[0]; } else { return theTextRef.parentStory; }}function searchText(range, from, to, caseSens, WholeWord, foots, hidLayers, lockLayers, lockStories, masterPages) { app.findTextPreferences.findWhat = from; try {app.changeTextPreferences.changeTo = to} catch(e) {}; with (app.findChangeTextOptions) { caseSensitive = (caseSens == null ? false : caseSens); wholeWord = (WholeWord== null ? false : WholeWord); includeFootnotes = (foots == null ? false : foots); includeHiddenLayers = (hidLayers == null ? false : hidLayers); includeLockedLayersForFind = (lockLayers == null ? false : lockLayers); includeLockedStoriesForFind = (lockStories == null ? false : lockStories); includeMasterPages = (masterPages == null ? false : masterPages); } return(range.changeText());}