2009-11-03

Remove Column of Text from Adobe InDesign or Adobe InCopy Story

Parent function from a prior post

A prior post listed the removeColumn function which can remove a delimited column from a string. This is the wrapper that calls that function to remove a column of text from a specified set of paragraphs within an Adobe InDesign or Adobe InCopy story.

If you want it to work on a set of selected paragraphs then pass it:
app.selection[0].paragraphs
which will provide the paragraphs of the selection even if the user does an imperfect (read faster ) selection. The sample as shown would do the entire story.

As an alternative, check out the paragraphs collection method which is oddly named
.itemByRange(start,stop)
as that allows you to point to a specific range of paragraphs.

As with with the removeColumn function, this is great for cleaning up sports agate text where you need to remove columns from a set of paragraphs.


//
function removeColumnFromParagraphs ( pgraphs , colToKill , delimiter ) {
//-------------------------------------------------------------------------
//-- R E M O V E C O L U M N F R O M P A R A G R A P H S
//-------------------------------------------------------------------------
//-- Generic: Yes for Adobe InDesign or Adobe InCopy CS3 or newer
//-------------------------------------------------------------------------
//-- Purpose: To remove a column from some tab delimited text in a
//-- collection of paragraphs.
//-------------------------------------------------------------------------
//-- Arguments: 3
//-- pgraphs: an Adobe InDesign or Adobe InCopy collection of
//-- paragraphs. This is can be generated by something like this:
//-- var pgraphs = story[0].paragraphs ;
//-- colToKill: An integer of zero based column of text to remove.
//-- in tab delimited columns
//-- delimiter: OPTIONAL. If not specified a tab will be used.
//-------------------------------------------------------------------------
//-- Calls: the removeColumn function -- see prior post
//-------------------------------------------------------------------------
//-- Returns: nothing, but alters the text of tha passed text.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ removeColumnFromParagraphs ( app.selection[0].parentStory.paragraphs , 3 )
//~ removeColumnFromParagraphs ( app.selection[0].parentStory.paragraphs , 0 )
//~ removeColumnFromParagraphs ( app.selection[0].parentStory.paragraphs , -1 )
//-------------------------------------------------------------------------
//-- Notes: Can alter formatting of the paragraphs if they are not
//-- consistently formatted. Uses regular expressions and some array
//-- methods which are not format friendly
//-------------------------------------------------------------------------
//-- Edited: 2009.11.01 by Jon S. Winters of electronic publishing support
//-- in the business center at CLT while waiting on a flight.
//-- The basis for the posted function was taken from a long running
//-- section of code.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- check the third parameter and if not passed
if ( delimiter == undefined ) {
delimiter = '\t' ; // or ' '
}
//-- Get a count of the number of pargraphs in the collection
var numPgraphs = pgraphs.length ;
//-- Loop through every paragraph
for ( var pIndex = numPgraphs-1 ; pIndex >= 0 ; pIndex-- ) {
var textWithColumnRemoved = removeColumn ( pgraphs[pIndex].contents , colToKill , delimiter ) ;

//-- See the original had a return and if the final didn't.
//-- Note the order is important to maximize the speed
if ( ( ! new RegExp ( '\r' ).test ( textWithColumnRemoved ) ) && ( new RegExp ( '\r' ).test ( pgraphs[pIndex].contents ) ) ) {
//-- Add the return back in as the paragraph contents is replaced
pgraphs[pIndex].contents = textWithColumnRemoved + '\r' ;
}
else {
//-- Just set contents to the text without the column
pgraphs[pIndex].contents = textWithColumnRemoved ;
}
//-- continue with loop
}
}
//

2009-11-02

Remove Column of Text

Great for Sports Agate

The following function is something I regularly use for removing a column of text in sports agate cleanup scripts. It is very generic. Pass it a string, a zero based column index, and the delimiter (usually a tab specified as '\t' ) and the function will remove that column.

Note, you would need to call this with something that can process all the paragraphs. As this will only handle one paragraph at a time unless you get real fancy with the calls.

Look for another function to call this one in an upcoming post.

//
function removeColumn ( orig, col, delimiter ) {
//-------------------------------------------------------------------------
//-- R E M O V E C O L U M N
//-------------------------------------------------------------------------
//-- Generic: Yes. Should work for any ECMAScript based languages such as
//-- ExtendScript and JavaScript.
//-------------------------------------------------------------------------
//-- Purpose: To remove a 'delimiter' separated 'col'umn of text from the
//-- 'orig'inal passed string.
//-------------------------------------------------------------------------
//-- Arguments: 3
//-- orig: A string which delimited columns
//-- col: A column number. This can be negative to work from the back
//-- delimiter: a string which is used to delimit columns
//-------------------------------------------------------------------------
//-- Calls: Nothing, but requires the .split(), .splice(), and .join()
//-------------------------------------------------------------------------
//-- Returns: The original string with the specified column removed.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ removeColumn ( "a\tb\tc\td\t\e\tf", -1, '\t' ) ; // "a\tb\tc\td\t\e"
//~ removeColumn ( "a\tb\tc\td\t\e\tf", 2, '\t' ) ; // "a\tb\td\t\e\tf"
//-------------------------------------------------------------------------
//-- Notes: Works great with negative column indexes. -1 is the last column
//-- If removing the last column be careful if you need the text to
//-- retain the final return or line break as this will delete it.
//-------------------------------------------------------------------------
//-- Edited: 2009.11.01 by Jon S. Winters of electronic publishing support
//-- Originally an internal function of a larger function.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- break the string into an array using the passed delimiter
var sa = orig.split(delimiter) ;
//-- Call inbuilt splice method to remove the specified element of the array
sa.splice(col,1) ;
//-- Join the array into a string using the passed delimater.
return sa.join(delimiter) ;
} //-- end of internal function.
//