Find/Change Paragraph Style to Cell Style
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Find/Change Paragraph Style to Cell Style
- This topic has 7 replies, 3 voices, and was last updated 9 years ago by Poleroad.
-
AuthorPosts
-
-
March 26, 2015 at 4:13 pm #74189PoleroadMember
Sorry to say, I know very little VB programming as such, we are after all publishers, right?
However, it would be helpful to have a VB Script to Find/Change from Paragraph Style to Cell Style in a Table.
I found something to the effect that would change Cell Height based on the Paragraph style (which, when I tried it, bombs on my first line). Could I ask you for help?
Below is the posted script. What are the variables that have to be replaced with my own parameters? And what am I missing in order to make it work? I am using InDesign CS6.
Sorry for being so inept, but not everybody is good at everything.
Martin
Function findTable(obj) {
while (obj.constructor.name != “Table”) {
obj = obj.parent;
if (obj.constructor.name == “Application”) {
throw “Can’t find table”
}
}
return obj
}var myTable = 0;
if (app.documents.length > 0 && app.selection.length > 0) {
myTable = findTable(app.selection[0]);//Find styles cells formater
if (myTable.constructor.name == “Table”) {
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = “Heading 2”;
var myCellStyle = myTable.findText();
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}
if (myCellStyle.length>0)
{
for (var c = 0; c<myCellStyle.length; c++) {
myCellStyle[c][/c].parent.parentRow.height = “20p”; //Change row height as per your requirement } } else{alert(“Not found”);} } -
March 30, 2015 at 6:28 am #74236PoleroadMember
OK, no take on this question, but is there a way to use plain Find/Change to apply Cell Styles based on Paragraph styles?
Thank you,
Martin -
March 30, 2015 at 7:59 am #74238David BlatnerKeymaster
Looks like our indesign scripters are not online at this time. You cannot use InDesign’s normal find/change to do that, no. I assume it could be done with scripting, though.
-
March 31, 2015 at 3:01 am #74260Theunis De JongMember
Well, for starters it’s not Visual Basic but Javascript ;)
It stops on the first line because Javascript is case sensitive and “Function” should be “function”. Apart from that it should work nicely if you change the operation from changing the Row Height to setting the cell style. In the script below, the name of the applied cell style should be in “myCellStyle”.
When copying your code I found all single and double quotes were transformed into typographically correct “curly” ones. Javascript cannot work with these, they must be straight ones. It’s possible this forum changes them, so please make sure to check them after copying.
Adobe’s own ExtendScript Toolkit Editor, and other Javascript editors as well (I use TextPad) will apply syntax highlighting to keywords and properly formed text strings. You can use this to check; ‘function’, for example, should blue, just as “while” and “if” and other keywords. A text string without proper quotes appears ‘black’ (i.e., as regular text), but with the proper opening and closing quotes it appears in dark red.
Also, watch out for runaway comments. The very last line in your post contains one after the ‘//’, and this indicates the comment runs until the end of the line. However, there is a return missing and the ‘else’ part including the closing brackets should be on a new line.
function findTable(obj)
{
while (obj.constructor.name != "Table")
{
obj = obj.parent;
if (obj.constructor.name == "Application")
{
throw "Can’t find table";
}
}
return obj;
}var myTable = 0;
if (app.documents.length > 0 && app.selection.length > 0)
{
myTable = findTable(app.selection[0]);//Find styles cells formater
if (myTable.constructor.name == "Table")
{
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
app.findTextPreferences.appliedParagraphStyle = "Heading 2";
var myCellStyle = myTable.findText();
app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
}
if (myCellStyle.length > 0)
{
for (var c = 0; c < myCellStyle.length; c++)
{
myCellStyle[c][/c].parent.appliedCellStyle = "myCellStyle";
}
} else
{
alert("Not found");
}
}
-
April 1, 2015 at 10:37 am #74297PoleroadMember
Thank you Jongware, making progress !
Got past up to line 27 :)
but on line 28, this one:
myCellStyle[c][/c].parent.appliedCellStyle = “Header Cell”;I got the following error: Offending Text: /
I assume there is something with [/c] entity that is not accepted.
Thank you again,
Martin -
April 1, 2015 at 4:48 pm #74310Theunis De JongMember
Well paint me fifty shades of red. I checked my script right after posting it, just to make sure the items I mentioned were correct. But- the weird thing is, I saw this same (wrong) construction in your original script and deleted it without thinking too much about it.
The forum software must be doing something … unexpected here!
It should look like this .. oh wait, I cannot show it :P It should be just the first set of square brackets with a single c inside. The second set with the slash inside should NOT be there.
-
April 2, 2015 at 5:40 am #74314PoleroadMember
I think you deserve 50 shades of Thank you!
After some tinkering to figure out what were the variable-names and what were my InDesign styles and match them — voila: it worked.
Now, if I may ask for a bit more of an improvement. As I discovered, one needs to manually place the cursor into the table, is there a way to run the script over an entire document with hundreds of tables. That would be the biggest help.
Ditto: same for Table Styles. Find all Tables and apply Table Style.
Thank you again,
Martin -
April 3, 2015 at 11:30 am #74384PoleroadMember
Hello Jongware,
I managed to find the script that I was looking for above. I found a script posted by B. Eenkhoorn. That is Convert Paragraph to Table and Apply Table Style. If you have another minute for hand-holding, the script bombs on line 15 and also on the following line:
for (var i = result.length-1; i >= 0; i-)Thank you again,
MartinHere it is:
***********************************************************************book = app.activeBook;
for (var i=0; i<book.bookContents.length; i++)
{
var currentDoc = app.open (book.bookContents[i].fullName, false);// DO THINGS HERE FOR EVERY BOOK
currentDoc.close(SaveOptions.YES);
}//Script to convert Paragraph to Table and apply table style
//==================
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = app.documents.firstItem().paragraphStyles.itemByName = “Heading 3″;var result = app.documents.firstItem().findGrep();
for (var i = result.length-1; i >= 0; i-){
var a_text =(result[i].characters.lastItem().contents == ‘\r’)
? result[i].parentStory.characters.itemByRange(result[i].characters.firstItem().index, result[i].characters.lastItem().index -1)
: result[i];
var a_table = a_text.convertToTable( );
}app.findGrepPreferences = null;
var d = app.activeDocument;
var myTableStyle = myDisplayDialog ();d.stories.everyItem().tables.everyItem().appliedTableStyle = myTableStyle;
d.stories.everyItem().tables.everyItem().clearTableStyleOverrides( true );function myDisplayDialog(){
var myDialog = app.dialogs.add({name:”Avakov Table Stripes”});
with(myDialog.dialogColumns.add()){
var myTsDropDown = dropdowns.add({stringList: d.tableStyles.everyItem().name, selectedIndex:d.tableStyles.length-1});
}
var myResult = myDialog.show();
if(myResult == true){
var tS = d.tableStyles[myTsDropDown.selectedIndex];
myDialog.destroy();
}
else{
myDialog.destroy();
exit()
}
return tS;
}
-
-
AuthorPosts
- You must be logged in to reply to this topic.