Copy link to clipboard
Copied
Hi Experts,
I write a script to change the text frame options when that frame contains particular style(s), but I am getting error as object is invalid when I pass the 12th line. Can some one please help me to find out the error?
And can some on please teach me where should I exactly find what error it is on ESTK?
var mystyle=["caption"];
for(k=0;k<mystyle.length;k++)
{
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = mystyle
; var fnd = app.findGrep();
for (a=0; a<fnd.length; a++)
{
var myTextFrame = app.selection[0].textFrames[0];
alert (myTextFrame);
myTextFrame.textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;
myTextFrame.textFramePreferences.minimumFirstBaselineOffset=6;
myTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
}
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
}
sthg along this ... not tested
var styleNames=["caption"]; //array to fill
var doc = app.activeDocument;
for(var p = 0; p < styleNames.length; p++)
{
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames
);
var find = app.findGrep();
if(find.length > 0)setTFProps(find);
}
function setTFProps(find)
{
for(var i = 0; i < find.length; i++)
{
var parentTextframe = find.parentTextFrames[0];//assuming onl
...Copy link to clipboard
Copied
sthg along this ... not tested
var styleNames=["caption"]; //array to fill
var doc = app.activeDocument;
for(var p = 0; p < styleNames.length; p++)
{
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames
);
var find = app.findGrep();
if(find.length > 0)setTFProps(find);
}
function setTFProps(find)
{
for(var i = 0; i < find.length; i++)
{
var parentTextframe = find.parentTextFrames[0];//assuming only one textFrame per caption
with( parentTextframe)
{
textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;
}
pT.fit(FitOptions.FRAME_TO_CONTENT);
}
}
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
Copy link to clipboard
Copied
Hi -hans-
it works perfectly. Can you please let me know what went wrong on coding? This will help me to learn further script writing.
Thanks
pr
Copy link to clipboard
Copied
Hy @prtamil, am currently in same problem as you before, please help me send the edited script of the above code that works for you through my email on jamilu19 @ gmail.com.
Note: there's no space in between the email address.
Thanks
Copy link to clipboard
Copied
this refers to a String rather than a paragraphstyle
does not take care of the searchresults, should be
fnd.parentTextFrames[0];//assuming only one textFrame per caption
Copy link to clipboard
Copied
Hi Jameel Bin Abdallah,
does the code Hans provided will not work for you?
Is there an error message if you execute the script?
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Yes @Laubender, it's display an error which I don't understand, please check the code and help out.
Thanks Once Again.
Copy link to clipboard
Copied
What does the error message say?
Please provide a sample document where you see this error if you run the script.
Thanks,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe, I think this is a case of an old thread dropping the loop variables on the forum upgrade. This should work:
var styleNames=["caption"]; //array to fill
var doc = app.activeDocument;
for(var p = 0; p < styleNames.length; p++){
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyles.itemByName(styleNames[p]);
var find = app.findGrep();
if(find.length > 0)setTFProps(find);
}
function setTFProps(find){
for(var i = 0; i < find.length; i++){
var parentTextframe = find[i].parentTextFrames[0];//assuming only one textFrame per caption
with(parentTextframe){
textFramePreferences.firstBaselineOffset=FirstBaseline.ASCENT_OFFSET;
}
parentTextframe.fit(FitOptions.FRAME_TO_CONTENT);
}
}
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
Copy link to clipboard
Copied
Please explain how should I go about using the code, I may be the one using it wrongly.
I copied and saved it into a script file, then select a text frame from my InDesign Document and double click the script. Is that the right way?
Copy link to clipboard
Copied
Paste the code from my post, not the original.
Needs to be saved as plain text, or from the ExtendScript Toolkit app with a .jsx ext. into your ID scripts folder:
Applications ▸ Adobe InDesign 20XX ▸ Scripts ▸ Scripts Panel
Copy link to clipboard
Copied
Dats exactly what I did @rob day , but it also displays an error code exactly as the one that appears in my comment below, please compile the code, test and send it to me via this mail jamilu19 @ gmail.com
Copy link to clipboard
Copied
Does your document have a Paragraph Stylesheet named "caption"? The first line of the script sets an array of Paragraph Style names to search for. The names need to be exact and are case sensitive
//edit the list as needed. Style names need to be in quotes separated by commas
var styleNames=["caption", "style2", "style3"]; //array to fill
Copy link to clipboard
Copied
No, my document has no paragraph style called captions, the paragraph style I want to search for, is named as CASES TITLE, and dats the similar text frame I want to change its text frame options across the whole document.
Therefore, if I get you right, you said I should edit the code, then replace caption with CASES TITLE?
Copy link to clipboard
Copied
Yes.
Copy link to clipboard
Copied
This is the error message I got
Copy link to clipboard
Copied
The image above is a reply to @Laubender
Copy link to clipboard
Copied
Hi Jameel,
ah, yes, this is the error for the older posted code.
I assume that code was damaged by the end of 2019 when this thread was moved from the old InDesign Scripting forum to the new InDesign forum. A typical error. The iterator was dropped by the moving process.
Before:
doc.paragraphStyles.itemByName(styleNames);
Rob corrected that to:
doc.paragraphStyles.itemByName(styleNames[p]);
styleNames as argument made no sense, because the variable styleNames contains an array of style names.
Even if the array contains only one name. The loop is walking through this array, and a single entry in the array must be accessed through the iterator [p].
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Usually the old post problems are with the variable in array loops, but this is also wrong
pT.fit(FitOptions.FRAME_TO_CONTENT);
should be
parentTextframe.fit(FitOptions.FRAME_TO_CONTENT);
Copy link to clipboard
Copied
Please, @Laubender and @rob day . Help me compile the code, test it to confirm that it's working fine, then kindly send it to my email address via jamilu19 @ gmail.com.
I have zero knowledge of scripting.
Thanks