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
//

No comments:

Post a Comment