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?
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
}
Copy link to clipboard
Copied
Hm, I am getting invalid parameter on line 37:
myAnchoredFrame.place(File(imagePath));
Copy link to clipboard
Copied
Did you change the code?
Copy link to clipboard
Copied
No, I didn't modify anything. But I am on Mac if this makes a difference.
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 )
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
Copy link to clipboard
Copied
Would you share to me (privately) an extract of a file, to debug?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I poked in here not realizing the discussion was several years old, but I would be amazed at any script that could do an even 90% job of this task, on an otherwise un-optimized document, and produce something that will export to a successful EPUB.
Note that "successful"/"readable"/"user acceptable" in this context =/= "functional."
Copy link to clipboard
Copied
I poked in here not realizing the discussion was several years old, but I would be amazed at any script that could do an even 90% job of this task, on an otherwise un-optimized document, and produce something that will export to a successful EPUB.
Note that "successful"/"readable"/"user acceptable" in this context =/= "functional."
By @James Gifford—NitroPress
Anchoring - not a problem - already done - any way you want.
ePUB export - needs polishing.
Copy link to clipboard
Copied
The issue, as with so many (often script-centric) requests to fix some badly-planned or -executed project, isn't whether the technical goal can be achieved as to whether it's any useful end in itself, or even a progression towards a useful result.
Yes, I'm sure a script could anchor all the images to the immediately prior paragraphs.
Which means the designer simply needs to go through and make sure each of the 300(?) images works in every other way. Unless they, and their usage, and their connection to the text is completely consistent, no generic fix will accomplish much. It will be pretty much a one-at-a-time review of their export to a sample EPUB, then correcting scaling, position, spacing etc. and repeating the export. Just possibly an Object Style or two could streamline the process, but it won't be automatic in any way.
Copy link to clipboard
Copied
Sure, James. But the point here is to have a workable starting point with the images anchored where they belong. It is a tedious work to anchor hundreds of images in a bad-designed document.
Copy link to clipboard
Copied
Then RT probably has your solution.
I maintain the (possibly harsh) viewpoint that some faults in poor planning and execution can't be readily fixed with automation, and if in the end, you don't spend considerable time fixing the anchors themselves on some percentage of the images, I will be somewhat surprised.
You'd be done with the fix by now if you'd just gotten down to it, and for all the tedium of anchoring each image — using designer judgment on each — you'd also be done with the (all but essential) step of optimizing each with either an applied Object Style or more design judgment.
But twój cyrk, twoje małpy. 🙂
Copy link to clipboard
Copied
[...]
Which means the designer simply needs to go through and make sure each of the 300(?) images works in every other way. Unless they, and their usage, and their connection to the text is completely consistent, no generic fix will accomplish much. It will be pretty much a one-at-a-time review of their export to a sample EPUB, then correcting scaling, position, spacing etc. and repeating the export. Just possibly an Object Style or two could streamline the process, but it won't be automatic in any way.
By @James Gifford—NitroPress
It doesn't have to be one-at-a-time.
As long as it doesn't require "I like it better on the left" - then it's perfectly doable.
"Scaling, position, spacing, etc." can still be described as a set of "rules":
- if image is - from a specific folder, partial name, size, applied ObjectStyle, layer, applied Parent Page, etc.
- if anchor location in text has specific Char / ParaStyle applied, is in the table, etc.
- width of the screen of the target reader,
- etc.
- images can be automatically resampled / resized, vectors rasterised if needed - based on the scale / size, CMYK->RGB, cropped, etc.
It all can be "conditionally described" - so only small % will require manual "corrections".
Copy link to clipboard
Copied
As a one-shot fix, yes. I am as usual unconvinced that creating such an elaborate fix-tool is time better spent than just going through the doc doing the work directly.
This is, after all, a project of significant length that was done without observing a primary rule for EPUB layout. I suspect it could use a thorough pass and review far beyond the capabilities of automated fixes.
Copy link to clipboard
Copied
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"...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You can automate processes. You can't automate design.
By @James Gifford—NitroPress
My point was about how each element should be "translated" from INDD to ePUB.
Each piece of text / object needs to be described in at least basic form.
But its original location will determine its more "precise" description.
Like with HTML - piece of text can be as <p>, <div> or <span> - so "design" determines which tag should be used to "describe" this piece of text.
With "short run" publications - you are right, it might need a lot of manual fine tuning - but for a newspaper - it can be precisely configured.
Copy link to clipboard
Copied
I'm not sure how this sub-discussion relates to the topic except that the OP needs to fix up the doc for EPUB export. I wouldn't try and take it to an EPUB export function.
I suppose simple linear documents could be said to fit into your model — Heading 1, export to <h1>, Body Text, export to <p> and so forth — but that's just the simplest, core body content. It gets considerably more complex from there.
Consider that Adobe is fifteen years in and still doesn't have it all right. 🙂