Skip to main content
Bedazzled532
Inspiring
October 19, 2024
Answered

Convert a Paragraph Style to outline

  • October 19, 2024
  • 1 reply
  • 1060 views

Hi

I have written a script which will convert a selected paragraph style to outlines. For some reason it is giving error.

 

//Get handle to a document
var myDoc = app.activeDocument;

//Get all paragraph styles in a document
var paraStyles = myDoc.paragraphStyles.everyItem();

//myDoc.allParagraphStyles  [this is not working for some reason]

//Create a Dialog
var main_dialog = app.dialogs.add({name:"Convert Selected Paragraph Style to Outline"});

//Create a column and add to the main dialog
var dialog_column = main_dialog.dialogColumns.add();

//Create a row and add it to the main dialog
var dialog_rows = dialog_column.dialogRows.add();

//Add a static text and a dropdown in the row
dialog_rows.staticTexts.add({staticLabel:"Paragraph Styles",minWidth:100});
var dialog_ddl = dialog_rows.dropdowns.add({minWidth:100});

//Populate dropdown list with paragraph styles
dialog_ddl.stringList = paraStyles.name;

//Change the selected index to 1
dialog_ddl.selectedIndex=1;

//Show the dialog and save the result in a variable
resChoice = main_dialog.show();

//Save the selected choice in a vairable
dlgChoice = dialog_ddl.stringList[dialog_ddl.selectedIndex];

alert(dlgChoice);
dlgChoice.createOutlines();

 

The error I am getting is this:

Error Number: 24
Error String: dlgChoice.createOutlines is not a function

Thanks

 

This topic has been closed for replies.
Correct answer rob day

@Robert at ID-Tasker Thanks. Here is the updated code :

 

//Get handle to a document
var myDoc = app.activeDocument;

//Get all paragraph styles in a document
var paraStyles = myDoc.paragraphStyles.everyItem();

//myDoc.allParagraphStyles  [this is not working for some reason]

//Create a Dialog
var main_dialog = app.dialogs.add({name:"Convert Selected Paragraph Style to Outline"});

//Create a column and add to the main dialog
var dialog_column = main_dialog.dialogColumns.add();

//Create a row and add it to the main dialog
var dialog_rows = dialog_column.dialogRows.add();

//Add a static text and a dropdown in the row
dialog_rows.staticTexts.add({staticLabel:"Paragraph Styles",minWidth:100});
var dialog_ddl = dialog_rows.dropdowns.add({minWidth:100});

//Populate dropdown list with paragraph styles
dialog_ddl.stringList = paraStyles.name;

//Change the selected index to 1
dialog_ddl.selectedIndex=1;

//Show the dialog and save the result in a variable
resChoice = main_dialog.show();

//Save the selected choice in a vairable
dlgChoice = dialog_ddl.stringList[dialog_ddl.selectedIndex];

//Convert the selected choice to outline
var style = dlgChoice;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = style;
list = app.activeDocument.findGrep();  

if ( list.length < 0 ) 
{
	alert( 'style ' + style.name + ' not found' );  
	app.findGrepPreferences = null;
}
else{
	for(i=0;i<list.length;i++){
	list[i].createOutlines();
}
}
app.findGrepPreferences = null;

 

Now one issue left. How do I remove the [No paragraph style] from the list. 

 

This line is giving error:

paraStyles.shift();

Hi @Bedazzled532 , If the document happened to contain groups of styles, myDoc.paragraphStyles.everyItem() would not get all of the styles–it would skip styles inside of the groups.

 

You can use myDoc.allParagraphStyles, which returns an array (not a collection) of all the document paragraph styles. This uses the allParagrahStyles array and skips the first [No Paragraph Style]:

 

makeDialog();
var psSel;
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Dialog Components", canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Paragraph Styles:"});
                }
                with(dialogColumns.add()){
                    psSel = dropdowns.add({stringList:getAllPStyles(app.activeDocument.allParagraphStyles), selectedIndex:0, minWidth:80});
                } 
        }
        if(theDialog.show() == true){
            psSel = app.activeDocument.allParagraphStyles[psSel.selectedIndex+1];
            main();
            theDialog.destroy();
        }
    }
}

function main(){
    alert("\rChosen Paragraph Style: " + psSel.name);

}

/**
* @ param the paragraph style array 
* @ return array of names (skips [No Paragraph Style])
*/
function getAllPStyles(arr){
    var a = new Array;
    for(var i = 1; i < arr.length; i++){
        a.push(arr[i].name);
    }
    return a
}

 

 

 

 

1 reply

Robert at ID-Tasker
Legend
October 19, 2024

Your

dlgChoice

 

is just a JS variable that you've defined in your code - and it stores INDEX to a selected item on your list - not a reference to the selected item - object in the InDesign.

 

Bedazzled532
Inspiring
October 19, 2024

@Robert at ID-Tasker Thanks for the reply. Actually dlgChoice is outputting the name of the paragraph style (screenshot attached).  If that is the index, then how can I get the actual name of the paragraph style ?

 

Also there is a second problem. Dropdown shows "No paragraph Style". To remove that I tried paraStyles.shift() but that is also giving error. 

rob day
Community Expert
Community Expert
October 19, 2024

@Robert at ID-Tasker Thanks. Here is the updated code :

 

//Get handle to a document
var myDoc = app.activeDocument;

//Get all paragraph styles in a document
var paraStyles = myDoc.paragraphStyles.everyItem();

//myDoc.allParagraphStyles  [this is not working for some reason]

//Create a Dialog
var main_dialog = app.dialogs.add({name:"Convert Selected Paragraph Style to Outline"});

//Create a column and add to the main dialog
var dialog_column = main_dialog.dialogColumns.add();

//Create a row and add it to the main dialog
var dialog_rows = dialog_column.dialogRows.add();

//Add a static text and a dropdown in the row
dialog_rows.staticTexts.add({staticLabel:"Paragraph Styles",minWidth:100});
var dialog_ddl = dialog_rows.dropdowns.add({minWidth:100});

//Populate dropdown list with paragraph styles
dialog_ddl.stringList = paraStyles.name;

//Change the selected index to 1
dialog_ddl.selectedIndex=1;

//Show the dialog and save the result in a variable
resChoice = main_dialog.show();

//Save the selected choice in a vairable
dlgChoice = dialog_ddl.stringList[dialog_ddl.selectedIndex];

//Convert the selected choice to outline
var style = dlgChoice;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = style;
list = app.activeDocument.findGrep();  

if ( list.length < 0 ) 
{
	alert( 'style ' + style.name + ' not found' );  
	app.findGrepPreferences = null;
}
else{
	for(i=0;i<list.length;i++){
	list[i].createOutlines();
}
}
app.findGrepPreferences = null;

 

Now one issue left. How do I remove the [No paragraph style] from the list. 

 

This line is giving error:

paraStyles.shift();

This line is giving error: paraStyles.shift()

 

Also the reason shift() is throwing the error is because

 

 


var paraStyles = myDoc.paragraphStyles.everyItem();

 

 

is returning a Collection, and shift() is an array method. For shift() to work you would need to convert the collection into an array like this:

 

 

var paraStyles = myDoc.paragraphStyles.everyItem().getElements();
paraStyles.shift()