Copy link to clipboard
Copied
I can't find a way to set the unit preferences for Illustrator CS6/CC using JavaScript.
I found that this works (setting units for stroke):
var units = 2; // 0-inches, 1-milllimeters, 2-points
app.preferences.setIntegerPreference("strokeUnits", units)
But I want to set the ruler units ("General" in the interface), and this does not work:
app.preferences.setIntegerPreference("rulerUnits", units)
Any help?
Peter
Hi,
We can set ruler unit using "rulerType" key not "rulerUnit".
Check below script:
var units = 0; //(0 to 6)
app.preferences.setIntegerPreference("rulerType", units);
/*Unit list
0 : point
1 : pica
2 : inch
3 : mm
4 : cm
5 : H/Q
6 : px
*/
Ten
Copy link to clipboard
Copied
Hi,
We can set ruler unit using "rulerType" key not "rulerUnit".
Check below script:
var units = 0; //(0 to 6)
app.preferences.setIntegerPreference("rulerType", units);
/*Unit list
0 : point
1 : pica
2 : inch
3 : mm
4 : cm
5 : H/Q
6 : px
*/
Ten
Copy link to clipboard
Copied
Ten,
Yes, this works, finally! Thank you very much with this simple but poorly documented feature.
I noticed that the unit mapping is: 0: inches, 1: mm, 2: points, 3: pica, 4: cm, 5: custom, 6: pixels
Regards,
Peter
Copy link to clipboard
Copied
Oops! Apologies for my mistake.
I'll update my documentation, Thanks.
Ten
Copy link to clipboard
Copied
Where can I find the documentation for all preference set via the setIntegerPreference and similar methods on app.preferences ?
Copy link to clipboard
Copied
There are no documentation by Adobe.
But, You can reference below script:
https://github.com/ten-A/Extend_Script_experimentals/blob/master/preferencesKeeper.jsx
Ten
Copy link to clipboard
Copied
Unfortunately I just can't get this one to work, I'm on Ai 21.1.0
To confirm- the expected result is to see the active ai's documents to be changed right after the script command is executed?
Copy link to clipboard
Copied
yes, the expected result is to see units changed in the general preference window. Still works in Win CC2017
Copy link to clipboard
Copied
That's funny, it does affect the dropdown selection in the preferences dialog, but does not change the active document's ruler units. Changing the selection on the preferences dialog does set the active document, but only when the unit is different than the one you just set via script, I think because it thinks it's already applies so it says no change needed.
Copy link to clipboard
Copied
Additionally, We can access document level unit preference from Javascript.
But its READ ONLY...
Copy link to clipboard
Copied
Unfortunately.. so the only current way of changing a document's current ruler units is GUI scripting with send-keys, or creating a completely new document and pasting all art inside?
Copy link to clipboard
Copied
Again, possible way can be: save document uncompressed then modify the file directly.
%AI5_RulerUnits: 2
Change 2 to 0-6.
app.preferences.setBooleanPreference ('aiFileFormat/enableContentRecovery', true);
app.preferences.setBooleanPreference ('aiFileFormat/dumpPGFwithoutShortcut', true);
var d = app.activeDocument,
fn = d.fullName,
f = File(d.path + '/_' + d.name),
unit = 3;
d.close();
app.open(fn);
app.activeDocument.close();
parseData(f, unit);
app.open(f);
// then save as origin name with compression if you like.
app.preferences.setBooleanPreference ('aiFileFormat/dumpPGFwithoutShortcut', false);
function parseData(f, unit) {
f.encoding = 'BINARY';
f.open('e');
var re = /%AI5_RulerUnits: \d/;
while (!f.eof) {
if (re.test(f.readln())) {
f.seek(-1 - (f.lineFeed == 'windows' ? 2 : 1), 1);
f.write(unit);
f.close();
break;
}
}
}
Copy link to clipboard
Copied
Unit properties follow ExtendScipt Tool
/*Unit list -
Unknown: - 1
Points - 4 :
Picas - 5 :
Inches - 2 :
Millimeters - 6 :
Centimeters - 3
Pixel - 8
Qs - 7
/*
Copy link to clipboard
Copied
Sorry for bugging here, but im loosing my mind here with scripting. I want to change the default AdjustLayout.jsx script to metric using millimeters, so change all points references in the file. But when the popup comes it keeps calculating my input to points. How is this even possible?
I know this is illustrator but the scripting is same language right?