Copy link to clipboard
Copied
Hi,
I found this code to align line objects to baseline from this forum. But its throwing error (null object).
var fp = app.selection[0].graphics[0].itemLink.filePath;
var f = File(fp);
f.open('r');
var fs = f.read();
f.close();
var baseline = fs.match(/%%Baseline\s+(\S+)/)[1];
app.selection[0].anchoredObjectSettings.anchorYoffset = -Number(baseline);

Can anyone guide me...
Thanks in advance,
Sudha K
Copy link to clipboard
Copied
Hi,
Sorry.. When the document contains missing links its throwing this error.
Copy link to clipboard
Copied
@Sudha – what did you expect?
The script is trying to open the linked EPS file and reading out the first PostScript comment starting with the string "%%Baseline" to extract the substring after a whitespace followed by a substring that is not a whitespace.
What can go wrong with that:
1. There is no EPS file properly linked; so it cannot be opened.
2. If it is properly linked, the EPS file does not contain a readable PostScript comment
3. The PostScript comment is there, but does not contain a substring that could be converted to a number.
So you should test for all the above cases and react with an alert or something.
Uwe
Copy link to clipboard
Copied
Hi,
I thought its work for all images without checking whether its missing or not. But the image is missing, as you said we cant read tat file.
So we can check the 1st point by missing links... how to check the point 2 and 3???
- Sudha K
Copy link to clipboard
Copied
@Sudha – do something like that:
try{
var baseline = fs.match(/%%Baseline\s+(\S+)/)[1];
}catch(e){
//Give alert:
alert("No match found:"+"\r"+e.message);
//Exit and end the script at this point:
exit();
};
Uwe
Copy link to clipboard
Copied
Hi,
Ok thank you...
is it for both point 2 and 3???
Copy link to clipboard
Copied
@Sudha – it is basically for the case where fs.match() is causing null.
If a substring is returned you could test for NaN (Not a Number) before assigning it as a value for the anchorYoffset property of anchoredObjectSettings of your selection.
if(isNaN(Number(baseline))){
alert("Found substring cannot be converted to a number.")
exit();
};
Uwe
Copy link to clipboard
Copied
HI,
Thank you...
When im using this logic, some of the inline objects are aligned correctly to baseline. Some is not aligned correctly... its moved down to baseline. Don't know the reason...
Before run the code the image was like this.

After run the code the image is like this.

I run the code for all objects in document. Some objects aligned correctly. Some having this problem. How can i fix this issue??
How the value baseline in postscript comment is related to inline value of anchor object settings?? Can u pls explain this???
- Sudha K
Copy link to clipboard
Copied
@Sudha – no, I cannot explain this, because I do not know the PostScript code in your EPS files. For me it seems, that assigning just the negative of a number that is used in a PostScript comment may be too simplistic to get what you want.
Uwe
Copy link to clipboard
Copied
Oh, and looking at your example: Why did you run the code on this?
As far as I can see the position of the equation was good before running the code…
Uwe
Copy link to clipboard
Copied
Hi,
InDesign document having lots of equations moved to up position. I used this script to move that equation to baseline without any check. So, Some of the Images are align correctly but some equations are moved to down not in baseline. This is my prob. I dono wat to do..
- Sudha K
Copy link to clipboard
Copied
I think, you have to find out on what occasion an equation is aligned right and on what occasion it is not.
Since you can know what is the value of "baseline" after reading the EPS, you can do a check with an EPS that is freshly placed. Maybe this test will always involve visually looking at the line, maybe you can find a rule how these EPSes react on a given situation…
$.writeln(baseline);
would write the value of baseline to the JavaScript Console of the ESTK.
var myCharacterThatIsRepresentingTheEPS = app.selection[0].parent.getElements()[0];
would be the character that is representing the placed EPS in the text.
And you could check for any baseline shift or other properties that influence the position of the placed graphic.
Point size would come to my mind.
Oh: And make sure, you do not run the script more than one time on a placed graphic.
Maybe the better idea is to change the baseline of the character that holds the EPS:
app.selection[0].parent.getElements()[0].baselineShift = -1;
Instead of changing the anchored object settings.
Uwe
Find more inspiration, events, and resources on the new Adobe Community
Explore Now