Copy link to clipboard
Copied
I am actually looking to modify a set of open FM files,. However, my script works fine only for the active document. I am unable to apply my script to other open documents. So, how to activate a document in extendscript? Please help me.
If the documents are already open, you can loop through them and get the Doc object for each one. Something like this:
var doc;
doc = app.FirstOpenDoc;
while (doc.ObjectValid () === 1) {
// Call a function, passing in doc as the Doc to process.
doc = doc.NextOpenDocInSession;
}
Copy link to clipboard
Copied
If the documents are already open, you can loop through them and get the Doc object for each one. Something like this:
var doc;
doc = app.FirstOpenDoc;
while (doc.ObjectValid () === 1) {
// Call a function, passing in doc as the Doc to process.
doc = doc.NextOpenDocInSession;
}
Copy link to clipboard
Copied
Actually that was the way I had tried. But the formatting is not happening in the docs: I have shared the code below: Could you please help me?
var i=0;
var ind=getFontIndex ("Calibri");
var ptag, ptagFormat, ptagFormatName, ptagsSize;
var newpara, oldpara;
var ctags =[];
var ptags =[];
var ctag, ctagFormat, ctagFormatName, ctagsSize;
var newchar, oldchar;
var dc=app.FirstOpenDoc;
while(dc.ObjectValid())
{
changePgFormat(dc);
changeChFormat (dc);
dc=dc.NextOpenDocInSession;
}
function changePgFormat(DocName)
{
ptagFormat= DocName.FirstPgfFmtInDoc ;
while (ptagFormat.ObjectValid() == true)
{
ptagFormatName = ptagFormat.Name;
// alert(ptagFormatName);
ptags.push(ptagFormatName);
ptagFormat=ptagFormat.NextPgfFmtInDoc;
}
ptagsSize = ptags.length;
//alert ("Number of formats in array before processing: " +ptagsSize);
for(i=0; i<ptagsSize; i++)
{
ptagFormatName = ptags[i];
newpara = doc.GetNamedPgfFmt (ptagFormatName);
newpara.FontFamily=130;
}
for(i=0; i<ptagsSize; i++)
{
ptags.pop();
}
ptagsSize = ptags.length;
//alert ("Number of formats in array after processing: " +ptagsSize);
}
function changeChFormat(DocName)
{
ctagFormat= DocName.FirstCharFmtInDoc;
while (ctagFormat.ObjectValid() == true)
{
ctagFormatName = ctagFormat.Name;
ctags.push(ctagFormatName);
ctagFormat=ctagFormat.NextCharFmtInDoc;
}
ctagsSize = ctags.length;
for(i=0; i<ctagsSize; i++)
{
ctagFormatName = ctags[i];
if(ctagFormatName!="Symbol"){
newchar= doc.GetNamedCharFmt (ctagFormatName);
newchar.FontFamily=ind;
}
}
for(i=0; i<ctagsSize; i++)
{
ctags.pop();
}
}
function getFontIndex(fontName)
{
var fname=app.FontFamilyNames;
var fLen = fname.length;
for (i=0; i<fLen; i++)
{
if(fname[i]== fontName)
{
return i;
}
}
}
Copy link to clipboard
Copied
How do you know that the change is not being made?
Copy link to clipboard
Copied
Because when I define define the variable DC as app.ActiveDoc the paragraph and character properties are changing. However when I assign app.FirstOpenDoc to DC and iterate over all the other open docs in session, the para and character properties are not changing. That is why I have posted the code.
Copy link to clipboard
Copied
Your function call is using dc as the Doc object, which is fine. Your functions are receiving the Doc object as DocName, which is fine (although, I would just use dc all the way through). However, when you attempt to get your PgfFmt and CharFmt objects, you are using "doc", which is not a valid object. Here, you would need to use DocName.
Also, I am not sure why you are copying your format names into an array first. You can process the objects directly in the original loops.
Copy link to clipboard
Copied
Thank u so much. You are truly an expert. I really appreciate you for taking time to guide people like me. I am truly grateful.
Copy link to clipboard
Copied
Please mark my answer as correct if you can. Thank you.