Hi, I am not interested to script elements from a library at the moment, but I’m interested in playing with dialogs. Here is the next and maybe last version:
// ApplyStylesToLists.jsx
// Kai Rübsamen, www.ruebiarts.de
// Vers. 1.1
// DESCRIPTION: Search for lists and apply styles
if ( app.scriptPreferences.version >= 6 ) {
app.doScript( main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Search and style lists" );
}
else {
main();
}
function main() {
// make certain that user interaction (display of dialogs, etc.) is turned on
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if ( app.documents.length == 0 ) {
alert ( "Open a document!" );
exit();
}
var curDoc = app.activeDocument;
// COLLECT ALL STYLES IN THE DOCUMENT
// ---
// Create a list of names and paragraph styles
var allParaStyleNamesList = [];
var allParaStylesList = [];
// all styles outside of groups
for ( var i = 0; i < curDoc.paragraphStyles.length; i++ ) {
var curParaStyle = curDoc.paragraphStyles;
allParaStyleNamesList.push( curParaStyle.name );
allParaStylesList.push( curParaStyle );
}
// all styles inside of groups
for ( var i = 0; i < curDoc.paragraphStyleGroups.length; i++ ) {
var curStyleGroup = curDoc.paragraphStyleGroups;
for ( var j = 0; j < curStyleGroup.paragraphStyles.length; j++ ) {
allParaStyleNamesList.push( curStyleGroup.paragraphStyles .name + " ( " + curStyleGroup.name + " )" );
allParaStylesList.push( curStyleGroup.paragraphStyles );
}
}
// the slice() method returns a copy of the array into a new array object
var displayNames = allParaStyleNamesList.slice(1);
// insert a new element at the beginning
displayNames.unshift( "Choose a style" );
// CREATE THE DIALOG
// ---
var dlg = app.dialogs.add({ name: "Style lists" }); // the dialog container
var min_width_left = 140;
var min_width_right = 210;
with ( dlg.dialogColumns.add() ) { // at least one column
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Find what:" });
}
with ( borderPanels.add() ) { // border
with (dialogColumns.add()) { // column
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Paragraph style:" , minWidth: min_width_left });
var search_style = dropdowns.add({ stringList: displayNames, selectedIndex: 0 , minWidth: min_width_right });
} // end dialogRows
with ( dialogRows.add() ) { // row
staticTexts.add( {staticLabel: "Real list:", minWidth: min_width_left} );
var cb_realList = checkboxControls.add({staticLabel: "", checkedState: false, minWidth: min_width_right});
} // end dialogRows
with (dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Manual bullets:", minWidth: min_width_left });
var text_edit_box = textEditboxes.add({ minWidth: min_width_right -120});
staticTexts.add( {staticLabel: "( E.g.: -\\s+ or •\\t )"} );
} // end dialogRows
} // end dialogCols
} // end borderPanels
with ( borderPanels.add() ) { // border
with (dialogColumns.add()) { // column
} // end dialogCols
} // end borderPanels
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Change to:" });
}
with ( borderPanels.add() ) { // border
with (dialogColumns.add()) { // column
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Main paragraph style:" , minWidth: min_width_left });
var main_style = dropdowns.add({ stringList: displayNames, selectedIndex: 0 , minWidth: min_width_right });
} // end dialogRows
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "First paragraph style:" , minWidth: min_width_left });
var first_style = dropdowns.add({ stringList: displayNames, selectedIndex: 0 , minWidth: min_width_right });
}
with ( dialogRows.add() ) { // row
staticTexts.add({ staticLabel: "Last paragraph style:" , minWidth: min_width_left });
var last_style = dropdowns.add({ stringList: displayNames, selectedIndex: 0 , minWidth: min_width_right });
} // end dialogRows
with ( dialogRows.add() ) { // row
staticTexts.add( {staticLabel: "Remove bullets:", minWidth: min_width_left} );
var cb_removeBullets = checkboxControls.add({staticLabel: "", checkedState: false, minWidth: min_width_right});
} // end dialogRows
} // end dialogCols
} // end borderPanels
} // end dialogCols
// show the dialog and save users choice
if( dlg.show() ){
var searchParaIndex = search_style.selectedIndex;
var mainParaIndex = main_style.selectedIndex;
var firstParaIndex = first_style.selectedIndex;
var lastParaIndex = last_style.selectedIndex;
var dlgStr = text_edit_box.editContents;
var cbList = cb_realList.checkedState;
var cbRemove = cb_removeBullets.checkedState;
dlg.destroy();
}
else {
dlg.destroy();
exit();
}
// SEARCH AND STYLE THE PARAGRAPHS
// ---
// test the choice for find what
if ( searchParaIndex == 0 && cbList == false && dlgStr == "" ) {
alert("You must enter something!");
exit();
}
app.findGrepPreferences = app.changeGrepPreferences = null;
if ( searchParaIndex != 0 && cbList == false && dlgStr == "" ) { // style, no list
var searchStyle = allParaStylesList[searchParaIndex];
app.findGrepPreferences.properties = { appliedParagraphStyle: searchStyle };
}
else if ( searchParaIndex != 0 && cbList == true ) { // style and list
var searchStyle = allParaStylesList[searchParaIndex];
app.findGrepPreferences.properties = { appliedParagraphStyle: searchStyle, bulletsAndNumberingListType: ListType.BULLET_LIST };
}
else if ( searchParaIndex == 0 && cbList == true && dlgStr == "" ) { // only list
app.findGrepPreferences.properties = { bulletsAndNumberingListType: ListType.BULLET_LIST };
}
else if ( searchParaIndex != 0 && dlgStr != "" && cbList == false ) { // style and grep
var complGREP = "(" + dlgStr + "(.|\\n)+\\r)+";
alert ( complGREP );
app.findGrepPreferences.findWhat = complGREP ;
var searchStyle = allParaStylesList[searchParaIndex];
app.findGrepPreferences.properties = { appliedParagraphStyle: searchStyle };
}
else if ( dlgStr != "" && cbList == false ) { // only grep
var complGREP = "(" + dlgStr + "(.|\\n)+\\r)+";
alert ( "Notice:\r Your grep will be expanded to find the whole list\r" + complGREP );
app.findGrepPreferences.findWhat = complGREP ;
}
else if ( dlgStr != "" && cbList == true ) { // warning list or grep, not both
alert ( "Stop!\rYou can only search for manual bullets\rOR automatic lists, not both at the same time!" );
exit();
}
var allFounds = app.findGrep(true); // it’s important to search backwards if later manual bullets should be deleted !
// test the choice for change to
if ( mainParaIndex != 0 ) {
var mainStyle = allParaStylesList[mainParaIndex];
}
else {
alert ( "Choose at least the main style for all paragraphs!" );
exit();
}
for ( var i = 0; i < allFounds.length; i++ ) {
var curFound = allFounds;
var allParas = curFound.paragraphs;
allParas.everyItem().appliedParagraphStyle = mainStyle; // whole paragraphs
if ( firstParaIndex != 0 ) {
allParas[0].appliedParagraphStyle = allParaStylesList[firstParaIndex]; // first paragraph
}
if ( lastParaIndex != 0 ) {
allParas[allParas.length-1].appliedParagraphStyle = allParaStylesList[lastParaIndex]; // last paragraph
}
if ( dlgStr != "" && cbRemove == true ) {
app.findGrepPreferences.findWhat = dlgStr;
curFound.changeGrep();
}
}
app.findGrepPreferences = app.changeGrepPreferences = null;
} // end main
... View more