Showing posts with label Paragraph. Show all posts
Showing posts with label Paragraph. Show all posts

2010-05-10

Paragraphs of Selection

Handy for certain tasks
This is another function that I tend to use when needing to work with paragraphs. I recently came across it again and thought it was fine time to share.
The basic premise is that if the user has some text selected and you want to automate something with regard to the whole paragraphs that have any part selected, then you need something like this.
With all the ifs, switches, etc., this code sure does go marching off to the right, but that is just the way it is. It doesn't have to be pretty, it just has to work.
Comments?

//
function getParagraphsOfSelection () {
//----------------------------------------------------------------------------
//-- G E T P A R A G R A P H S O F S E L E C T I O N
//----------------------------------------------------------------------------
//-- Generic: Yes.
//----------------------------------------------------------------------------
//-- Purpose: To return an array of paragraphs relating to the selection
//-- or false if there is no selection or an error.
//----------------------------------------------------------------------------
//-- Written: 10 March 2009 by Jon S. Winters from a prior work.
//-- eps@electronicpublishingsupport.com
//----------------------------------------------------------------------------

//-- Determine is there in an appropriate selection.
try {
//-- Initialize the object
var myObject = false ;
//-- Check for a selection.
if(app.documents.length != 0){
if(app.selection.length != 0){
//Process the objects in the selection to create a list of
//qualifying objects (text frames).
switch(app.selection[0].constructor.name){
case "TextFrame":
myObject = ( app.selection[0].parentStory.paragraphs ) ;
break;
default:
if(app.selection.length == 1){
//If text is selected, then get the parent text frame.
switch(app.selection[0].constructor.name){
case "Text":
case "InsertionPoint":
case "Character":
case "Word":
case "Line":
case "TextStyleRange":
case "Paragraph":
case "TextColumn":
myObject = ( app.selection[0].paragraphs );
break;
} //-- end of switch
} //-- end of if selections == 1
break;
} //-- end of switch
} //-- end of app.selection.length
} //-- end of app.documents.length
} //-- end of try
catch (caughtError) {
return {} ;
}
return myObject ;
}//-- End of function
//

2009-07-14

Listing Paragraph Styles

I'm at a site this week that currently has 947 Paragraph Styles. I needed a list of them. Here is an ExtendScript to list them into a selected text frame in an Adobe InDesign page. This was a quick hack, so no verbose comments.
Note the two different ways of referencing the document. The first method uses a function posted here. Delete that line if you don't want to use it (no real need in this case).

Oh, and here with the 900+ Paragraph Styles, we had to add 7 more broadsheet pages to show the styles.

try {
//see if there is a parentStory. If so, then fill it with the names of the styles
//-- The next line errors if there isn't a proper selection
var storyRef = app.selection[0].parentStory.textContainers[0].parentStory ;

//-- Get a valid reference to the document holding the story
var docRef = returnDocumentReference ( storyRef ) ;
//OR use
var docRef = app.documents[0];

//-- Call the function to add the styles.
listParagraphStyles ( storyRef , docRef )
}
catch (err) {
//-- The next line quits the script.
exit() ;
}
//
function listParagraphStyles ( storyRef , docRef ) {

//-- Clear the contents of the the frame
storyRef.contents = '\r' ;

//-- Get an array of all the Paragraph Styles
var allPS = docRef.allParagraphStyles ;
var numPS = allPS.length ;

//-- Loop through the styles adding the name of the style and applying that style to the story
for ( var pIndex = 0 ; pIndex < numPS ; pIndex++ ) {
storyRef.paragraphs[pIndex].contents = allPS[pIndex].name + '\r ' ; //note the space after the return
//-- Remove the next line if you don't want to see the style name formatted with that
Paragraph Style.
storyRef.paragraphs[pIndex].appliedParagraphStyle = allPS[pIndex] ;
}
}
//