Skip to main content
Participant
May 16, 2017
Question

Script to anchor all images in document to the closest paragraph.

  • May 16, 2017
  • 2 replies
  • 3743 views

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?

This topic has been closed for replies.

2 replies

Community Expert
February 4, 2021

Hi DudiQ,

could it be that there is an image placed that is not linked to a file?

E.g. you copy/pasted a selection of pixels from PhotoShop to InDesign.

Or where the link to the file is broken?

 

( Just a guess… )

 

Regards,
Uwe Laubender

( ACP )

Participant
February 4, 2021

I tested it a bit more and the script worked for one INDD file out of 5 that I tested, the most basic one. So I guess the script somehow covers the most basic or intended scenario of placing images. But I will check if any linked images are broken or missing, that's a good idea. Thanks

James Gifford—NitroPress
Legend
May 16, 2024

@James Gifford—NitroPress

 

But ePUB is a predetermined set of "rules".

 

Text needs to be formatted in a specific way - images as well. 

 

It's just a case of applying those "rules"... 

 


You do know how to carve a statue of an elephant, right?

Just get a block of stone and chip away everything that doesn't look like elephant!

 

Maybe all publication is "just rules," but you have to choose and adapt what rules to apply. Unless your goal is some completely vanilla result, like a Government Printing Office report on corn diseases. Which, for all I know, this is.

 

You can automate processes. You can't automate design.

Gregorio Pellegrino
Participant
October 14, 2020

I think it's a bit late... But I fixed the script

 

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[j];
		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
	// Copy image into the anchored frame. Didn't find a better way  
	var imagePath = imageRect.images[0].itemLink.filePath;
	var image = imageRect.images[0];
	myAnchoredFrame.place(File(imagePath));
	myAnchoredFrame.geometricBounds = [imBounds[0] - frBounds[0], imBounds[1] - frBounds[1],
		imBounds[2] - frBounds[0], imBounds[3] - frBounds[1]
	];
	// 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;
	myAO.anchoredObjectSettings.pinPosition = false;
	return myAO
}
Participant
February 4, 2021

Hm, I am getting invalid parameter on line 37: 

myAnchoredFrame.place(File(imagePath));
Any ideas what could be wrong?
Gregorio Pellegrino
Participant
February 4, 2021

Did you change the code?