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
That's odd. Another bug to report -- when a script is running it should always start with using the current measurement units!
Try putting this at the front of your script:
app.scriptPreference.measurementUnit = MeasurementUnits.PIXELS;
That's "the measurement unit used during script processing". I've never used it (I'm still on terra firma, a.k.a. CS4), but the documentation says it ought to work.
Copy link to clipboard
Copied
awesome, I will give it a try! thanks for the quick response!!
Copy link to clipboard
Copied
Why is it odd?
The measurement units is always what's set in InDesign.
I would not advise changing the scriptPreferences measurement units unless they are changed back to AutoEnum.AUTO_VALUE afterwards. It has the potential to cause other scripts to suddenly stop working...
Harbs
Copy link to clipboard
Copied
... unless they are changed back to AutoEnum.AUTO_VALUE afterwards ...
Hm. Wasn't this preference added just for the current script then? The documentation is as terse as always ...
And of course it's odd: in CS4, and all previous CSes, the measurement units when starting a script are what they appear like in the UI. Now we all know adding pixels to ID was a dirty hack, but at the very least it should be used in a script as what-they-actually-are: points. This suggests the script engine thinks, "Wot are Pixels? I'm gonna use pica's instead".
Copy link to clipboard
Copied
Maybe it suppose to, maybe it's a bug. It makes sense to have it active just during script run, not to change units globally.
--
tomaxxi
Copy link to clipboard
Copied
I read the OP very differently than you.
If I read it correctly, it does use pixels when it's set in the UI. He was just asking how to set in in a script...
Harbs
Copy link to clipboard
Copied
Foot-in-mouth. On re-reading, you are correct.
Ah -- on setting the script preferred measurements: ... well ... it's possible ID maintains yet another global variable: either use 'automatic' measurement units in scripts, or use the one you designated some hours earlier in a totally unrelated script.
The Help says, "The measurement unit used during script processing" but it doesn't specify during what script UserInteractionLevel uses the exact same wording, and that's one that's famously infamous for driving script users nutty.
Can you try this out? Okay, thanks for the heads up. Or heads down. *Sigh*.
Copy link to clipboard
Copied
> The Help says, "The measurement unit used during script processing" but it doesn't specify during what script
How could ID know what script?
Copy link to clipboard
Copied
"The current one running" would have been quite a reliable guess ...
It's the same, erm, "unexpected" issue with UserInteractionLevel. When running a script, you'd want to have the choice to display dialogs et al. or not, but I cannot think of any circumstances you'd want that same behavior in the User Interface itself.
Copy link to clipboard
Copied
[Jongware] wrote:
"The current one running" would have been quite a reliable guess ...
But that wouldn't work for AS and VB scripts not run from the ID Scripts panel.
Copy link to clipboard
Copied
In the InDesign UI, if you open up the TextFrame Options dialog box for a given text frame, and change the column width from "6p0" to "5 pts" it will automatically convert it to picas (or whatever the preference is set at). So if I leave the preferences alone, and keep it at "picas" is there anyway in a script to add "5 pts" to a geometric bounds value? You would think it would maybe work the same in that sense and maybe behind the scenes do its own "conversion". I could be way off though, but if it would work that would be a good solution to changing the global preferences.
Any thoughts?
Copy link to clipboard
Copied
It should be possible to test a document for its measurement units then use unit values to do the conversion… This was with a quick play NOT very well thought through I must admit but it may be the way to go on this…
#target indesign app.activate(); var docRef = app.activeDocument; with (docRef) { var uU = userUnits(docRef); $.writeln(uU); var rb = spreads[0].rectangles[0].geometricBounds; $.writeln(rb); // 9.99999999999999,9.99999999999999,80,110 as millimeters my prefs var a = new UnitValue(5,'pt'); $.writeln(a.as(uU)); // 1.76388888888889 var nb = new Array(rb[0], rb[1], rb[2]+a.as(uU), rb[3]+a.as(uU)); $.writeln(nb); // 9.99999999999999,9.99999999999999,81.7638888888889,111.763888888889 spreads[0].rectangles[0].geometricBounds = nb; } // I only bothered to check in one direction… function userUnits(doc) { switch (doc.viewPreferences.horizontalMeasurementUnits) { case 2054188905 : uU = 'pt' break; case 2054187363 : uU = 'pc' break; case 2053729891 : uU = 'inch' break; case 2053729892 : uU = 'inch' break; case 2053991795 : uU = 'mm' break; case 2053336435 : uU = 'cm' break; case 2053335395 : uU = 'ci' break; case 1131639917 : uU = '?????'; // What do with this? } return uU; }
It would appear to give me the same results as using mixed measures in the GUI but I've not tested much…
Copy link to clipboard
Copied
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
Harbs, I do change the prefs then put them back I have no problem with that. ID scripters should be thankful for that ability… Illustrator scripting works in points only (ARPITA), quite a few Photoshop scripters prefer to use unit values. But the OP did want a solution that didn't use this method and I thought it may work it did make me laugh just a little bit…
Copy link to clipboard
Copied
It might be a nice exercise to create a proper conversion utility...
Here's an interesting discussion on this topic from some time ago...
http://forums.adobe.com/message/2220992#2220992
Harbs
Copy link to clipboard
Copied
You should have posted that link earlier and saved my some muckin about… It may be of use for AI so thanks…
Copy link to clipboard
Copied
Your post about UnitValue reminded me of Bob's function...
Copy link to clipboard
Copied
After asking about making the scripting forum searches static… I should make better use of them me thinks… Always a muppet…
Copy link to clipboard
Copied
[Jongware] wrote:
Hm. Wasn't this preference added just for the current script then? The documentation is as terse as always ...
I have not tested, but I seriously doubt it. All the other script preferences are global...
Harbs
Copy link to clipboard
Copied
I just tested it, it's for all scripts, so it's Global. Also unit conversion for "AGATES" does not work. Hope they will fix it for next version.
--
tomaxxi
Copy link to clipboard
Copied
tomaxxi wrote:
Also unit conversion for "AGATES" does not work...
Funny. I also used Agates to test, and it worked fine...
Copy link to clipboard
Copied
I tried it like this:
alert(UnitValue(1,MeasurementUnits.AGATES).as(MeasurementUnits.MILLIMETERS));
// or
alert(UnitValue(1,MeasurementUnits.MILLIMETERS).as(MeasurementUnits.AGATES));
but with other units it works fine.
--
tomaxxi
Copy link to clipboard
Copied
I just tested, and it is indeed global.
I stand by my suggestion to NEVER use it unless you are absolutely positive that you set it back to auto afterwards, unless you never use scripts that rely on the measurement units...
Harbs
Copy link to clipboard
Copied
Nice!
It's very useful hint!
--
tomaxxi
http://indisnip.wordpress.com/