Try this :
var fn = function() {
var doc, fgp = app.findGrepPreferences.properties,
found, n, errors = 0, hu, vu;
if ( !app.documents.length) return;
doc = app.activeDocument;
hu = doc.viewPreferences.horizontalMeasurementUnits;
vu = doc.viewPreferences.verticalMeasurementUnits;
doc.viewPreferences.horizontalMeasurementUnits =
doc.viewPreferences.verticalMeasurementUnits =
MeasurementUnits.POINTS;
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = “d+cm”;
found = doc.findGrep();
n = found.length;
while ( n– ) {
!doResize ( found[n] ) && errors++;
}
app.findGrepPreferences.properties = fgp;
doc.viewPreferences.horizontalMeasurementUnits = hu;
doc.viewPreferences.verticalMeasurementUnits = vu;
alert( errors? errors+” errors occured on “+found.length+” occurences.” : “Everything was fine.” );
}
var doResize = function(text) {
const ap = AnchorPoint.TOP_LEFT_ANCHOR,
cs = CoordinateSpaces.INNER_COORDINATES,
rm = ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH;
var ptf, uv, w, props;
if ( !text.parentTextFrames.length
|| !text.parentTextFrames[0].isValid
|| !/.+cm$/i.test(text.contents) ) return false;
uv = new UnitValue ( text.contents.slice (0, -2), “cm” ).as(“pt”);
ptf = text.parentTextFrames[0];
props = getPageItemProperties ( ptf );
ptf.resize ( cs,ap, rm, [uv, props.height] );
return true;
}
var getPageItemProperties = function(pi) {
var points = pi.paths[0].entirePath,
n = points.length, i = 0,
dummy, bounds, w, h;
const cs = CoordinateSpaces.INNER_COORDINATES;
var newPoints = [];
while ( i < n ) {
newPoints [ i ] = pi.resolve ( points[i], cs )[0];
i++;
}
dummy = app.activeDocument.polygons.add();
dummy.paths[0].entirePath = newPoints;
bounds = dummy.geometricBounds;
w = Math.round ( Math.abs ( bounds[3]-bounds[1])*1000)/1000;
h = Math.round ( Math.abs ( bounds[2]-bounds[0])*1000)/1000;
dummy.remove();
return {width:w, height:h};
};
fn();
Loic
Accueil