Copy link to clipboard
Copied
I'm very new to ID scripting here, please be nice 😉 I can't get my head around to text wrap an image placed above a text frame.
Here's my sample code:
var myDocument = app.documents.add(0);
var myPage = myDocument.pages.item(0);
var myStory = myPage.place(File("<filepath>"));
var myTextFrame = myDocument.pages.item(0);
var imgPlace = myTextFrame.place(File("<filepath>"));
Image.textWrapPreferences = TextWrapSideOptions.BOTH_SIDES;
Where am I wrong?
Thank you!
1 Correct answer
Oops. "Image" is a built-in name, the name of the Image Class: http://jongware.mit.edu/idcs6js/pc_Image.html‌!
You may never use built-in names for variables, just like you cannot use 'var return = 1;'. I guess that is why Adobe's own Introduction to Javascript (InDesign Help | Scripting) always uses the 'my' prefix on all variables.
Pick any other variable name instead of "Image" and it ought to do better (minding what Jarek says, can't check right now).
Copy link to clipboard
Copied
I don't see where you get the 'Image' variable from -- is it (as it should be) 'imgPlace[0]'? (Please ignore if your code works and you indeed get to see your image!)
The problem is this line:
Image.textWrapPreferences = TextWrapSideOptions.BOTH_SIDES;
You cannot set one property in 'preferences' this way. Either address it through textWrapMode:
Image.textWrapPreferences.textWrapMode = TextWrapSideOptions.BOTH_SIDES;
or explicitly set properties, which accepts an object contain the key/values to change:
Image.textWrapPreferences.properties = { textWrapMode: TextWrapSideOptions.BOTH_SIDES };
Copy link to clipboard
Copied
Hi Jongware, thank you for the quick reply.
That's the whole of my code. I don't know where the Image variable came from
I changed as suggested to Image.textWrapPreferences.textWrapMode = TextWrapSideOptions.BOTH_SIDES;
but see an error "undefined is not an object" Would you mind putting it together for me a running code 🙂 Thank you!
var myDocument = app.documents.add(0);
var myPage = myDocument.pages.item(0);
var myStory = myPage.place(File("/C/users/username/desktop/text.txt"));
var myTextFrame = myDocument.pages.item(0);
var imgPlace = myTextFrame.place(File("/C/users/username/desktop/333000.jpg"));
Image.textWrapPreferences.textWrapMode = TextWrapSideOptions.BOTH_SIDES;
Copy link to clipboard
Copied
The trick with writing a script that does "a lot" is test each command one by one. I assumed you only had a problem with the wrapping and so you elided the rest of the (working) script. But if no image appears to begin with, there is nothing to wrap ...
Untested, but this ought to do better:
var myDocument = app.documents.add(0);
var myPage = myDocument.pages.item(0);
var myStory = myPage.place(File("/C/users/username/desktop/text.txt"));
var myTextFrame = myDocument.pages.item(0);
var imgPlace = myTextFrame.place(File("/C/users/username/desktop/333000.jpg"));
// 'place' returns an array of placed objects, so pick only one:
Image = imgPlace[0];
Image.textWrapPreferences.textWrapMode = TextWrapSideOptions.BOTH_SIDES;
Note that the script makes ID do "what you tell it to", and in this case you are telling it to import a text file into a new text frame, and immediately importing an image into that same frame. I cannot change the script to do what it should because I'm not sure what that is.
Copy link to clipboard
Copied
Thank you [Jongware]‌ that seems to have got hold of the image reference but I'm getting an error on line 7 "Image is read only"
my image file is a jpg and has both r/w access on the machine. I've searched around but couldn't figure out what that means.
Copy link to clipboard
Copied
Hi,
Even if you pass through placing images ==> notice two properties of .textWrapPreferences differ;
textWrapMode (with values):
- TextWrapModes.NONE
- TextWrapModes.JUMP_OBJECT_TEXT_WRAP
- TextWrapModes.NEXT_COLUMN_TEXT_WRAP
- TextWrapModes.BOUNDING_BOX_TEXT_WRAP
- TextWrapModes.CONTOUR
textWrapSide (with values):
- TextWrapSideOptions.BOTH_SIDES
- TextWrapSideOptions.LEFT_SIDE
- TextWrapSideOptions.RIGHT_SIDE
- TextWrapSideOptions.SIDE_TOWARDS_SPINE
- TextWrapSideOptions.SIDE_AWAY_FROM_SPINE
- TextWrapSideOptions.LARGEST_AREA
Dont mix them and notice that they can be set to image and image container as well
Jarek
Copy link to clipboard
Copied
Oops. "Image" is a built-in name, the name of the Image Class: http://jongware.mit.edu/idcs6js/pc_Image.html‌!
You may never use built-in names for variables, just like you cannot use 'var return = 1;'. I guess that is why Adobe's own Introduction to Javascript (InDesign Help | Scripting) always uses the 'my' prefix on all variables.
Pick any other variable name instead of "Image" and it ought to do better (minding what Jarek says, can't check right now).
Copy link to clipboard
Copied
Thank you [Jongware]‌ it's working now, Yay!! And thanks Jarek for pointing out the difference
Here's the working example:
var myDocument = app.documents.add(0);
var myPage = myDocument.pages.item(0);
var myStory = myPage.place(File("/C/users/username/desktop/text.txt"));
var myTextFrame = myDocument.pages.item(0);
var imgPlace = myTextFrame.place(File("/C/users/username/desktop/333000.jpg"));
// 'place' returns an array of placed objects, so pick only one:
myImage = imgPlace[0];
myImage.textWrapPreferences.textWrapMode = TextWrapModes.CONTOUR;

