• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 컀뮀니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
May 16, 2017 May 16, 2017

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

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
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

Hm, I am getting invalid parameter on line 37: 

myAnchoredFrame.place(File(imagePath));
Any ideas what could be wrong?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

Did you change the code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

No, I didn't modify anything. But I am on Mac if this makes a difference.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 05, 2021 Feb 05, 2021

Copy link to clipboard

Copied

LATEST

Would you share to me (privately) an extract of a file, to debug?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines