Thread View: adobe.scripting.indesign
15 messages
15 total messages
Started by John_Kordas
Tue, 17 Mar 2009 13:17
CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Tue, 17 Mar 2009 13:17
Date: Tue, 17 Mar 2009 13:17
24 lines
1379 bytes
1379 bytes
I am trying to put together a script that will detect pages that have 2 types of elements.
1. blank graphic frames
2. graphic frames with images larger than the frame holding it.
I've created a simple 5 page doc and placed images on page 1 and 3. So the frist step is can I flag that page 1 and 3 have image on them.
This is what I have so far
var myDoc = app.activeDocument;
var myPages = myDoc.pages;
var myRecs = myDoc.rectangles;
for (var j = myPages.length - 1; j >= 0; j--) {
if(myPages[j].rectangles.length >= 1){
$.writeln("page "+j+" has a frame.");
}
}
And it returns the following in the console which is correct.
page 2 has a frame.
page 0 has a frame.
Is there a way to check if the frame has an image and if it image fits the frame or not?
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Tue, 17 Mar 2009 13:38
Date: Tue, 17 Mar 2009 13:38
3 lines
730 bytes
730 bytes
Check the length of the frame's images collection. If > 0, it has an image. Then compare the image's bounds with those of the frame. Dave
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Wed, 18 Mar 2009 13:09
Date: Wed, 18 Mar 2009 13:09
14 lines
971 bytes
971 bytes
Thanks Dave,
I've got it to check if there is a graphic and this seems to work but can not seem to get it to select the rectangle.
for (var j = myPages.length - 1; j >= 0; j--) {
if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){
myPages[j].rectangles.select()
}
}
error: myPages[2].rectangles.select is not a function
any suggestion?
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Wed, 18 Mar 2009 13:46
Date: Wed, 18 Mar 2009 13:46
11 lines
943 bytes
943 bytes
Does: app.select(myPages[j].rectangles) work? You need to check the object model to make sure that the method you're trying to use works with the object(s) you're trying to use it with. The search feature in ESTK CS4's OMV is very useful for this. Dave
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Wed, 18 Mar 2009 13:46
Date: Wed, 18 Mar 2009 13:46
3 lines
691 bytes
691 bytes
Ah, you're using CS3. Well, it too has an OMV, but I'm sure then search is so valuable there. Dave
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Fri, 20 Mar 2009 05:35
Date: Fri, 20 Mar 2009 05:35
28 lines
1660 bytes
1660 bytes
Thanks again Dave,
The select part is working but once selected I not able to effect the frame. Using this:
for (var j = myPages.length - 1; j >= 0; j--) {
if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){
var myGraphic = myDoc.select(myPages[j].rectangles);
myGraphic.rectangle.fit (CENTER_CONTENT)
}
}
var myGraphic = myDoc.select(myPages[j].rectangles);
myGraphic is now the variable that is selected correct?
Now if I want to effect that variable I should be able to do this:
myGraphic.rectangle.fit (CENTER_CONTENT)
but I'm getting the following error.
undefined is not an object.
in past creating the rectangle and changing it using:
var myFrame = pages.item(0).rectangles.add({strokeWeight:0, strokeColor:myDocument.swatches.item("None")});
with(myFrame){
geometricBounds = myGetBounds(myDocument, myPage);
myFile = myFrame.place( File(myFile), false );
app.activeWindow.zoom(ZoomOptions.fitPage);
has worked with no problems. I seem to be having problems manipulating rectangles already created.
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Fri, 20 Mar 2009 11:48
Date: Fri, 20 Mar 2009 11:48
9 lines
1228 bytes
1228 bytes
Hi John, I don't understand why you're selecting with your script. You only need to reference the rectangle to operate on it. But you've made up the property "rectangle" for myGraphic. Look in the OMV and you'll see that graphics don't have rectangle properties. I think you want the parent there--remember, a graphic could be in a polygon or oval or even a graphic line. There are a small number of actions that require selecting, but not very many. The main reason for using a selection is to show the user what you just did, but you're in a loop so selecting seems completely pointless. Dave
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Sat, 21 Mar 2009 12:12
Date: Sat, 21 Mar 2009 12:12
15 lines
1333 bytes
1333 bytes
Thanks again Dave I've been working my way through OMV and I think I'm finally getting it. Also went back to the scripting guide.
This is working for me:
for (var j = myPages.length - 1; j >= 0; j--) {
if(myPages[j].rectangles.length >= 1 && myPages[j].allGraphics.length > 0 ){
myPages.item(j).rectangles.item(0).fit(FitOptions.proportionally);
myPages.item(j).rectangles.item(0).fit(FitOptions.centerContent);
}
}
Interestingly I found that if I use
myPages.item(j).rectangles.item(0).geometricBounds = [0,0,144,144];
Then rectangle that is on the right hand page ends up on the left hand page. I would of thought that 0,0 would put the rectangle at the top left corner of the right hand page not the left hand page.
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Sat, 21 Mar 2009 13:05
Date: Sat, 21 Mar 2009 13:05
49 lines
2600 bytes
2600 bytes
I usually switch to spread coordinates to avoid those kinds of issues (although others swear by page coordinates). I use these two functions:
function saveMeasures(doc, units) {
var uPrefs = doc.viewPreferences;
var uMeasures = {
horiz:uPrefs.horizontalMeasurementUnits,
vert:uPrefs.verticalMeasurementUnits,
rule:uPrefs.rulerOrigin,
zp:doc.zeroPoint
}
doc.viewPreferences.properties = {
horizontalMeasurementUnits:units,
verticalMeasurementUnits:units,
rulerOrigin:RulerOrigin.spreadOrigin,
}
doc.zeroPoint = [0,0];
return uMeasures;
}
function restoreMeasures(doc, measures) {
doc.viewPreferences.properties = {
horizontalMeasurementUnits:measures.horiz,
verticalMeasurementUnits:measures.vert,
rulerOrigin:measures.rule
}
doc.zeroPoint = measures.zp;
}
I call these like this--notice the code is inside a function; that makes it possible to use return to exit the script which in turn makes it possible to call the restore function as I exit, using the return in two different places:
function splitElements(aDoc) {
var curPage = app.activeWindow.activePage;
var prevPage = aDoc.pages[curPage.documentOffset - 1];
if (prevPage === aDoc.pages[-1]) {
alert("Don't run this script on the first page of a document.");
return;
}
var uMeasures = saveMeasures(aDoc, MeasurementUnits.points);
var oSized = getOsetElement(prevPage);
if (oSized === null) return restoreMeasures(aDoc, uMeasures); // nothing to do
splitElement(oSized, prevPage, curPage, aDoc);
return restoreMeasures(aDoc, uMeasures);
}
This script needed to convert the measurement units to points because it needs to do calculations involving leading and stroke weights, both of which are provided in points.
Dave
Re: CS3 JS flag which page has empty frame
Author: Dirk_Becker
Date: Sat, 21 Mar 2009 14:45
Date: Sat, 21 Mar 2009 14:45
39 lines
1575 bytes
1575 bytes
The following script is a small modification of Dave's previously posted.
It illustrates two advanced javascript concepts:
* The nested function can refer to outside local variables of its closure.
* Code enclosed by a try-finally will execute the finally clause even if there is a return in between.
function setupMeasures(doc)
{
var vp = doc.viewPreferences.properties;
var zp = doc.zeroPoint;
doc.viewPreferences.properties = {
horizontalMeasurementUnits: MeasurementUnits.points,
verticalMeasurementUnits: MeasurementUnits.points,
rulerOrigin: RulerOrigin.spreadOrigin,
}
doc.zeroPoint = [0,0];
return function ()
{
doc.viewPreferences.properties = vp;
doc.zeroPoint = zp;
alert("Here!");
}
}
(function main()
{
var restoreMeasures = setupMeasures(app.activeDocument);
try {
// do your stuff
return 42;
} finally {
restoreMeasures();
}
})();
Dirk
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Thu, 26 Mar 2009 03:59
Date: Thu, 26 Mar 2009 03:59
16 lines
1347 bytes
1347 bytes
Thanks you both for your very useful code. For the moment I've decided to stick with RulerOrigin.pageOrigin; The page setup is A4 and I have a rectangle bleeding on the top,left and bottom. It is also sitting on a left hand page. What I'd like to do is extend the rectangle so that it become a double page rectangle. using var myBounds = myPages[j].rectangles.item(0).geometricBounds; myPages[j].rectangles.item(0).geometricBounds = [myBounds[0],myBounds[1],myBounds[2],(myBounds[3]+myBounds[3])]; the rectangle only extend to the edge of the page on the right To check I used: $.writeln(myBounds[2]+" "+myBounds[3]); which returned 301.999999999461 209.999999999936 Does geometricBounds only give the bounds within the page not outside.
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Thu, 26 Mar 2009 11:30
Date: Thu, 26 Mar 2009 11:30
3 lines
727 bytes
727 bytes
It gives you the bounds relative to the page the item is considered to be on (or associated with if entirely on the pasteboard). Dave
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Thu, 26 Mar 2009 13:08
Date: Thu, 26 Mar 2009 13:08
5 lines
879 bytes
879 bytes
Is it possible to get the graphic geometricbounds and compare them to the rectangle bounds? I found graphic.geometricBounds in the OMV but when I try var myBounds = myPages[j].item(0).graphic.geometricBounds; I get the error myPage[1] is not a function.
Re: CS3 JS flag which page has empty frame
Author: Dave_Saunders
Date: Thu, 26 Mar 2009 13:13
Date: Thu, 26 Mar 2009 13:13
15 lines
1353 bytes
1353 bytes
John, You're confusing Graphic.geometricBounds in the OMV, which tells you that a graphic object has the geometricBounds property, with how to address said graphic in a specific instance. Pages do not have a graphics (note plural) property, although they do have an allGraphics property (which gives you a list of all of them on the page). Typically, when you're looking for a specific graphic in the fashion you're doing, you need to get at it via the frame that holds it. Assuming it's a rectangle, you might write: myPages[j].rectangles[0].graphics[0].geometricBounds; even though the rectangle can only have one graphic, it is still a collection of one and can't be addressed using the singular form. Dave
Re: CS3 JS flag which page has empty frame
Author: John_Kordas
Date: Thu, 26 Mar 2009 13:23
Date: Thu, 26 Mar 2009 13:23
11 lines
985 bytes
985 bytes
Thanks Dave, I just got it and was going to post I think I worked it out but you beat me to it. This give me the rectangle geometricBounds: var myBounds = myPages[j].rectangles.item(0).geometricBounds; 216 296.999999999461 This gives me the graphic geometricBounds in the rectangle: var myBounds = myPages[j].rectangles.item(0).graphics.item(0).geometricBounds; 216 218.916666666667
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads