• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Convert a Paragraph Style to outline

Enthusiast ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

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

 

TOPICS
How to , Scripting

Views

321

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 19, 2024 Oct 19, 2024

Hi @shahidr100 , 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", canCan
...

Votes

Translate

Translate
Community Expert ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

@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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

quote

@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 ?


By @shahidr100

 

But that's just the name of the ParaStyle - it's not an actual style. 

 

And you can't outline ParaStyle - you need to outline Text object. 

 

So after user selects some ParaStyle - you need to find all references - text blocks / pieces with this ParaStyle applied - and then outline them. 

 

But because InDesign will do this wrong anyway - you should do it differently - but you've to be more precise in your end goal. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

@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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

I'm sorry but I'm not JS guy - maybe it's not implemented?

 

As a workaround - instead of using .everyItem() - build the list in a loop, using push() - starting from the 2nd element?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

Hi @shahidr100 , 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
}

 

 

Screen Shot 18.png

 

 

Screen Shot 19.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

@rob day Thank you so much. 

You have always been helpful and I am learning by studying your style of coding. It really helps.

You have always been a great mentor for me.

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2024 Oct 20, 2024

Copy link to clipboard

Copied

LATEST

Happy to help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 19, 2024 Oct 19, 2024

Copy link to clipboard

Copied

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()

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines