Copy link to clipboard
Copied
Hello,
I need to adjust the geometricbounds of an Image within a rectangle via a JS script. But when I get the Geometric bounds for my image, it is not using the pixels unit for measurement, its using picas. However, if I manually adjust the preferences in my Desktop Indesign to Pixels, the script runs correctly.
How can I change the preferences to "Pixels" from within my script so that I don't have to check manually?
Thanks in advance!!
Lloyd
There's nothing wrong with changing the prefs. Just change it back afterwards. Messing with unit conversions is more trouble than it's worth (in my opinion).
If you wrap your script in a try/catch/finally (changing it back in the finally), that should change back the units 99.9% of the time.
I'm just advocating using the visible prefs, so you know if changing it back fails (and will not break legacy scripts which rely on the UI measurements)...
Harbs
Copy link to clipboard
Copied
Yes. It's nice (I've wanted something like this for a long time), but it can badly break previously written scripts, so be careful...
Harbs
Copy link to clipboard
Copied
I'd recommend doing it like this:
doc.viewPreferences.horizontalMeasurementUnits = doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
Harbs
Copy link to clipboard
Copied
You might be able to do it like this.
var mySevenHundredGuides= Array ("-8.2505in","-7.5005in","0.1245in","0.4995in","0.6245","0.7495in","1.1245in","8.75in","9.5in");
for (var i=0; i<mySevenHundredGuides.length; i++){
myDocument.guides.add(myDocument.layers.item(infoLayer),{orientation:HorizontalOrVertical.vertical, location:(mySevenHundredGuides)});
}
I haven't tried it for setting bounds but it might work. The app does the conversion when you put "in" at the end of the number. In your case px.
Copy link to clipboard
Copied
I know about nothing about adobe javascript, do know a bit of other languages. Im bugging with the standard script which comes along with indesign. Its the AdjustLayout.jsx. Its setup in Points, but it want it to show millimeters from the start. So i changed all the items which where points. I dont know how but as soon as the input field is set to a variable its seem to be back at Points. How is this possible when all my settings are in MM the document settings as well as main app settings when nothing is open is in MM
var units = 'millimeters'; // 0-inches, 1-milllimeters, 2-points
//app.preferences.setIntegerPreference("rulerType", units)
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits[units];
app.activeDocument.viewPreferences.verticalMeasurementUnits= MeasurementUnits[units];
main();
function main(){
//Make certain that user interaction (display of dialogs, etc.) is turned on.
// app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if(app.documents.length != 0){
if(app.activeDocument.pageItems.length != 0){
myDisplayDialog();
}
else{
alert("Document contains no page items.");
}
}
else{
alert("Please open a document and try again.");
}
}
function myDisplayDialog(){
var myLabelWidth = 70;
var myDialog = app.dialogs.add({name:"Adjust Layout"});
var myPageNames = myGetPageNames();
with(myDialog.dialogColumns.add()){
with(borderPanels.add()){
with(dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Start Page:", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myStartPageDropdown = dropdowns.add({stringList:myPageNames, selectedIndex:0});
}
}
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"End Page:", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
var myEndPageDropdown = dropdowns.add({stringList:myPageNames, selectedIndex:myPageNames.length-1});
}
}
}
}
with(borderPanels.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Even Pages", minWidth:myLabelWidth});
staticTexts.add({staticLabel:"Horizontal:", minWidth:myLabelWidth});
staticTexts.add({staticLabel:"Vertical:", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
staticTexts.add({staticLabel:""});
var myEvenXField = measurementEditboxes.add({editValue:-3, editUnits:MeasurementUnits.millimeters});
var myEvenYField = measurementEditboxes.add({editValue:0, editUnits:MeasurementUnits.millimeters});
// alert(myEvenXField)
// , Units:MeasurementUnits.millimeters
// alert(measurementEditboxes.value)
}
}
}
with(borderPanels.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Odd Pages", minWidth:myLabelWidth});
staticTexts.add({staticLabel:"Horizontal:", minWidth:myLabelWidth});
staticTexts.add({staticLabel:"Vertical:", minWidth:myLabelWidth});
}
with(dialogColumns.add()){
staticTexts.add({staticLabel:""});
var myOddXField = measurementEditboxes.add({editValue:3, editUnits:MeasurementUnits.millimeters});
var myOddYField = measurementEditboxes.add({editValue:0, editUnits:MeasurementUnits.millimeters});
}
}
}
}
var myResult = myDialog.show();
if(myResult == true){
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var myStartPageName = myPageNames[myStartPageDropdown.selectedIndex];
var myEndPageName = myPageNames[myEndPageDropdown.selectedIndex];
if(myCheckPageRange(myStartPageName, myEndPageName) == true){
// alert(myEvenXField.editValue)
var myEvenX = myEvenXField.editValue;
var myEvenY = myEvenYField.editValue;
var myOddX = myOddXField.editValue;
var myOddY = myOddYField.editValue;
alert(myEvenX)
myDialog.destroy();
myAdjustPages(myEvenX, myEvenY, myOddX, myOddY, myStartPageName, myEndPageName);
}
else{
myDialog.destroy();
alert("Invalid page range.");
}
}
else{
myDialog.destroy();
}
}
function myAdjustPages(myEvenX, myEvenY, myOddX, myOddY, myStartPageName, myEndPageName){
var myPage, myPageAdjust;
// Set the transform content property to true so that content will move with frames.
//myOldTransformContent = app.transformPreferences.transformContent;
//app.transformPreferences.transformContent = true;
// var myOldXUnits = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
// var myOldYUnits = app.activeDocument.viewPreferences.verticalMeasurementUnits;
// //Set the measurement units to points.
// app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;
// app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;
//Save the old page numbering
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var myOldPageNumbering = app.generalPreferences.pageNumbering;
app.generalPreferences.pageNumbering = PageNumberingOptions.section;
var myStartPage = app.activeDocument.pages.item(myStartPageName);
var myEndPage = app.activeDocument.pages.item(myEndPageName);
//Set page numbering to absolute
app.generalPreferences.pageNumbering = PageNumberingOptions.absolute;
//Does the document start with an even page?
if(myCheckPageStart(app.activeDocument) == false){
myPageAdjust = 1;
}
else{
myPageAdjust = 0;
}
for(var myCounter = (myStartPage.documentOffset-1); myCounter < myEndPage.documentOffset; myCounter++){
myPage = app.activeDocument.pages.item(myCounter);
var myPageValue = myPage.documentOffset;
myPageValue = myPageValue + myPageAdjust;
if(myPageValue % 2 == 0){
//Page is an even page.
myAdjustPage(myPage, myEvenX, myEvenY);
}
else{
//Page is an odd page.
myAdjustPage(myPage, myOddX, myOddY);
}
}
//Reset the transform content and measurement units to their original values.
// app.activeDocument.viewPreferences.horizontalMeasurementUnits = myOldXUnits;
// app.activeDocument.viewPreferences.verticalMeasurementUnits = myOldYUnits;
// //app.transformPreferences.transformContent = myOldTransformContent;
// app.generalPreferences.pageNumbering = myOldPageNumbering;
}
function myAdjustPage(myPage, myX, myY){
var myPageItem;
var myResetItemLock = false;
var myResetLayerLock = false;
for(var myCounter = 0; myCounter < myPage.pageItems.length; myCounter ++){
myPageItem = myPage.pageItems.item(myCounter);
if(myPageItem.locked == true){
myPageItem.locked = false;
myResetItemLock = true;
}
if(myPageItem.itemLayer.locked == true){
myPageItem.itemLayer.locked = false;
myResetLayerLock = true;
}
myPageItem.move(undefined, [myX, myY]);
if(myResetItemLock == true){
myPageItem.locked = true;
}
if(myResetLayerLock == true){
myPageItem.itemLayer.locked = true;
}
}
AutoEnum.AUTO_VALUE
alert(AutoEnum.AUTO_VALUE)
}
function myGetPageNames(){
var myPageNames = new Array;
for(myCounter = 0; myCounter < app.activeDocument.pages.length; myCounter ++){
myPageNames.push(app.activeDocument.pages.item(myCounter).name);
}
return myPageNames;
}
function myCheckPageStart(myDocument){
var mySection = myDocument.sections.item(0);
if(mySection.continueNumbering == true){
//Starting page number is defined by other documents in the book
return true;
}
if(mySection.pageNumberStart % 2 == 0){
//Starting page number is an even page.
return false;
}
else{
//Starting page number is an odd page.
return true;
}
}
function myCheckPageRange(myStartPageName, myEndPageName){
var myStartIndex = app.activeDocument.pages.item(myStartPageName).documentOffset;
var myEndIndex = app.activeDocument.pages.item(myEndPageName).documentOffset;
if(myStartIndex <= myEndIndex){
return true;
}
else{
return false;
}
}