Script to anchor all images in document to the closest paragraph.
Hello,
I've asked this question in the general forum and they said that I should ask here.
I need to export a document to ePub, and this document has at least 200 images.
I know that I have to anchor them to the text because if not they will all be at the end of the ePub.
I've found a script in a post 6 years ago that seems to be almost what I want.
main();
function main()
{
var doc = app.activeDocument;
var count=0;
// loop through all pages
for(i=0; i<doc.pages.length;i++)
{
var page = doc.pages.item(i);
if( page.textFrames.length<1) continue;
var textFrame = page.textFrames.item(0);
if(page.rectangles.length<1) continue;
// loop through all rectangles in the page
for(j=0;j<page.rectangles.length;j++)
{
var imageRect=page.rectangles
;
if(imageRect.images.length<1) continue;
var myAnchoredFrame=anchorImage(doc, textFrame, imageRect);
var pos=[imageRect.geometricBounds[1],imageRect.geometricBounds[0]]
imageRect.remove();
j--;
textFrame.recompose();
var k=0;
// Reposition the anchored image. This is done repeatedly because the first call not moves the frame to the correct position
do
{
myAnchoredFrame.move(pos);
k++;
}
while(k !=5);
count++;
}
}
alert("Fixed "+count+" images");
}
function anchorImage(doc, textFrame, imageRect)
{
var myAnchoredFrame=CreateAnchor (doc, textFrame);
var imBounds = imageRect.geometricBounds;
var frBounds=textFrame.geometricBounds
myAnchoredFrame.geometricBounds = [imBounds[0]-frBounds[0], imBounds[1]-frBounds[1],
imBounds[2]-frBounds[0], imBounds[3]-frBounds[1]];
// Copy image into the anchored frame. Didn't find a better way
var imagePath=imageRect.images[0].itemLink.filePath;
var image=imageRect.images[0];
var filePath="D:\\im";
image.exportFile(image.imageTypeName,filePath);
myAnchoredFrame.place(File(filePath));
// Resize image
var newImBoundX=myAnchoredFrame.geometricBounds[1]-( imageRect.geometricBounds[1]-image.geometricBounds[1]);
var newImBoundY=myAnchoredFrame.geometricBounds[0]-( imageRect.geometricBounds[0]-image.geometricBounds[0]);
var newImBoundX1=newImBoundX+( image.geometricBounds[3]-image.geometricBounds[1]);
var newImBoundY1=newImBoundY+( image.geometricBounds[2]-image.geometricBounds[0]);
myAnchoredFrame.images[0].geometricBounds=[newImBoundY,newImBoundX,newImBoundY1,newImBoundX1];
//Set textWrapPreferences of the images
myAnchoredFrame.textWrapPreferences.textWrapMode=imageRect.textWrapPreferences.textWrapMode;
myAnchoredFrame.textWrapPreferences.textWrapOffset =imageRect.textWrapPreferences.textWrapOffset ;
return myAnchoredFrame;
}
function CreateAnchor( doc, textFrame)
{
var inPoint=textFrame.insertionPoints[0];
var anchProps = doc.anchoredObjectDefaults.properties;
var anchCont = anchProps.anchorContent;
var myAO = inPoint.rectangles.add();
// Make new object with correct default settings
// Make new object right kind of object
myAO.contentType =ContentType.graphicType;
// Recompose parent story so geometricBounds make sense
inPoint.parentStory.recompose();
//save users measurement preferences
userHoriz = doc.viewPreferences.horizontalMeasurementUnits;
userVert = doc.viewPreferences.verticalMeasurementUnits;
doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
doc.viewPreferences.horizontalMeasurementUnits = userHoriz;
doc.viewPreferences.verticalMeasurementUnits = userVert;
myAO.applyObjectStyle(anchProps.anchoredObjectStyle);
if (anchProps.anchorContent == ContentType.textType) {
try { // might be null
myAO.parentStory.appliedParagraphStyle = anchProps.anchoredParagraphStyle;
} catch (e) {}
}
myAO.anchoredObjectSettings.properties = doc.anchoredObjectSettings.properties;
myAO.anchoredObjectSettings.anchoredPosition=AnchorPosition.anchored;
return myAO
}
But this script is giving me the error "The specified object does not support the desired export format".
Any ideas how to fix this?
