Highlighted

help with existing dimensions lines script - change ft to mm

Explorer ,
Jun 07, 2020

Copy link to clipboard

Copied

i've found this great script here in this forum. it gives dimentions lines in one click around a selected object.

Can someone help me change the ft units to mm?

(and the colour from green to black)

(change txt to jsx)

(link to original thread where ive found the script:

https://community.adobe.com/t5/indesign/markup-dimensions-in-indesign-file/td-p/10754216?page=1

)

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

I gave two changes to be made you did only one change, that was the issue. To cut down the back and forth i am posting the edited script below

//DESCRIPTION:Label image with its size
// Jongware, 13-Jan-2012
// No Guarantees, Etc.

if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
	if (app.version < 6)
		labelItem(app.selection[0]);
	else
		app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
	alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}

function labelItem (item)
{
	// Save current selected layer because adding one will make it active!
	activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
	// Make sure the layer "ImageSize" exists
	try {
		app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [0,0,0], printable: false});
	} catch (_) { }
	reportLayer = app.activeDocument.layers.item("ImageLabel");

	// Make sure the swatch "ImageGreen" exists
	reportGreen = app.activeDocument.swatches.item("Black");

	// Make sure the paragraph style "ImageSize" exists
	try {
		app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", pointSize:12, fillColor:reportGreen, justification: Justification.CENTER_ALIGN});
	} catch (_) { }
	reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

	// Make sure measurement units are in inches
	hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
	vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	// .. and Smart Quotes is Off
	sqt = app.activeDocument.textPreferences.typographersQuotes;
	app.activeDocument.textPreferences.typographersQuotes = false;
	// Save all of the previous settings to be able to restore them later :slightly_smiling_face:

	// fetch width and height for the numbers, left and bottom for the lines
	gb = item.geometricBounds;
	// gb now holds [ top, left, bottom, right ] values -- get shorthand values
	top = gb[0];
	left = gb[1];
	bottom = gb[2];
	right = gb[3];

	width = Math.round(Math.abs(right - left)*1000)/1000;
	height= Math.round(Math.abs(bottom - top)*1000)/1000;

	// Retrieve the current page.
	// Actually, in CS5 and newer, page items have a property for this (parentPage),
	// but I don't have that one near, and it's also nicer to make it work in CS4
	// (and possibly even in older versions).
	pg = item.parent;
	while (1)
	{
		if (pg instanceof InsertionPoint)
			pg = pg.parentTextframes[0];
		if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
			break;
		pg = pg.parent;
	}
	// Draw text frame #1: Height
	// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
	frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top, left - 0.7, top + height, left - 0.2 ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frh.contents = String(Math.round(height * 25.4));

	// Draw text frame #2: Width
	// Put it at 0.2" off the bottom of the selection, 0.25" high, full width for convenience
	frw = pg.textFrames.add(reportLayer, {geometricBounds:[ bottom + 0.2, left, bottom + 0.2 + 0.25, left + width ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frw.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frw.contents = String(Math.round(width * 25.4));

	// Add vertical (height) line
	lnh = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// It appears in a default position so move it to the right one
	lnh.paths[0].entirePath = [ [left - 0.15, top ], [left - 0.15, bottom ] ];
	// add top stroke; it's 0.05" wide so offset left and right with 0.025"
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, top ], [left-0.15 + 0.025, top] ] });
	// add bottom stroke
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, bottom ], [left-0.15 + 0.025, bottom] ] });

	// Add horizontal (width) line
	lnw = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// Again we move it to the right position
	lnw.paths[0].entirePath = [ [left, bottom + 0.15 ], [right, bottom + 0.15 ] ];
	// Add end caps as above
	lnw.paths.add ({ entirePath:[ [left, bottom+0.15 - 0.025 ], [left, bottom+0.15 + 0.025] ] });
	lnw.paths.add ({ entirePath:[ [right, bottom+0.15 - 0.025 ], [right, bottom+0.15 + 0.025] ] });

	// At this point we added four items: 2 text frames and 2 graphic lines.
	// Group them together for convenience
	app.activeDocument.groups.add ( [ frh, frw, lnh, lnw ] );

	// Restore old layer selection
	app.activeDocument.layoutWindows[0].activeLayer = activeLayer;

	// Restore previous global settings
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
	app.activeDocument.textPreferences.typographersQuotes = sqt;

}

 

-Manan

Topics

Scripting

Views

484

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

help with existing dimensions lines script - change ft to mm

Explorer ,
Jun 07, 2020

Copy link to clipboard

Copied

i've found this great script here in this forum. it gives dimentions lines in one click around a selected object.

Can someone help me change the ft units to mm?

(and the colour from green to black)

(change txt to jsx)

(link to original thread where ive found the script:

https://community.adobe.com/t5/indesign/markup-dimensions-in-indesign-file/td-p/10754216?page=1

)

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

I gave two changes to be made you did only one change, that was the issue. To cut down the back and forth i am posting the edited script below

//DESCRIPTION:Label image with its size
// Jongware, 13-Jan-2012
// No Guarantees, Etc.

if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
	if (app.version < 6)
		labelItem(app.selection[0]);
	else
		app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
	alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}

function labelItem (item)
{
	// Save current selected layer because adding one will make it active!
	activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
	// Make sure the layer "ImageSize" exists
	try {
		app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [0,0,0], printable: false});
	} catch (_) { }
	reportLayer = app.activeDocument.layers.item("ImageLabel");

	// Make sure the swatch "ImageGreen" exists
	reportGreen = app.activeDocument.swatches.item("Black");

	// Make sure the paragraph style "ImageSize" exists
	try {
		app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", pointSize:12, fillColor:reportGreen, justification: Justification.CENTER_ALIGN});
	} catch (_) { }
	reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

	// Make sure measurement units are in inches
	hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
	vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	// .. and Smart Quotes is Off
	sqt = app.activeDocument.textPreferences.typographersQuotes;
	app.activeDocument.textPreferences.typographersQuotes = false;
	// Save all of the previous settings to be able to restore them later :slightly_smiling_face:

	// fetch width and height for the numbers, left and bottom for the lines
	gb = item.geometricBounds;
	// gb now holds [ top, left, bottom, right ] values -- get shorthand values
	top = gb[0];
	left = gb[1];
	bottom = gb[2];
	right = gb[3];

	width = Math.round(Math.abs(right - left)*1000)/1000;
	height= Math.round(Math.abs(bottom - top)*1000)/1000;

	// Retrieve the current page.
	// Actually, in CS5 and newer, page items have a property for this (parentPage),
	// but I don't have that one near, and it's also nicer to make it work in CS4
	// (and possibly even in older versions).
	pg = item.parent;
	while (1)
	{
		if (pg instanceof InsertionPoint)
			pg = pg.parentTextframes[0];
		if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
			break;
		pg = pg.parent;
	}
	// Draw text frame #1: Height
	// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
	frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top, left - 0.7, top + height, left - 0.2 ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frh.contents = String(Math.round(height * 25.4));

	// Draw text frame #2: Width
	// Put it at 0.2" off the bottom of the selection, 0.25" high, full width for convenience
	frw = pg.textFrames.add(reportLayer, {geometricBounds:[ bottom + 0.2, left, bottom + 0.2 + 0.25, left + width ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frw.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frw.contents = String(Math.round(width * 25.4));

	// Add vertical (height) line
	lnh = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// It appears in a default position so move it to the right one
	lnh.paths[0].entirePath = [ [left - 0.15, top ], [left - 0.15, bottom ] ];
	// add top stroke; it's 0.05" wide so offset left and right with 0.025"
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, top ], [left-0.15 + 0.025, top] ] });
	// add bottom stroke
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, bottom ], [left-0.15 + 0.025, bottom] ] });

	// Add horizontal (width) line
	lnw = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// Again we move it to the right position
	lnw.paths[0].entirePath = [ [left, bottom + 0.15 ], [right, bottom + 0.15 ] ];
	// Add end caps as above
	lnw.paths.add ({ entirePath:[ [left, bottom+0.15 - 0.025 ], [left, bottom+0.15 + 0.025] ] });
	lnw.paths.add ({ entirePath:[ [right, bottom+0.15 - 0.025 ], [right, bottom+0.15 + 0.025] ] });

	// At this point we added four items: 2 text frames and 2 graphic lines.
	// Group them together for convenience
	app.activeDocument.groups.add ( [ frh, frw, lnh, lnw ] );

	// Restore old layer selection
	app.activeDocument.layoutWindows[0].activeLayer = activeLayer;

	// Restore previous global settings
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
	app.activeDocument.textPreferences.typographersQuotes = sqt;

}

 

-Manan

Topics

Scripting

Views

485

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 07, 2020

Copy link to clipboard

Copied

These lines: 

 

app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;

 

Should be this: 

 

app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

Thanks, but now nothing happens. i just get a line without text labels and with out those two small penpendicular lines that show the edges.

and for some reason theres no space between the line and the shape.

YT.jpg

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

Don't make that change, it messes with the whole calculations of the document instead just change the following

 

frh.contents = String(height)+'"';

 

to

 

frh.contents = String(Math.round(height * 25.4))+'"';

 

and

 

frw.contents = String(width)+'"';

 

 to

 

frw.contents = String(Math.round(width * 25.4))+'"';

 

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

Great! Thanks!

What should i change to replace """ (inch symbol) with "cm"?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

On the same two lines, change '"' to 'cm'

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

ok thanks. i just ommited the " " " / "cm" beacuse i tried to type ס"מ which means "cm" on my language (hebrew) but the script fails to except hebrew letter as much as i understand.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 07, 2020

Copy link to clipboard

Copied

In addition to Brian's answer, i see you have other requirments as well

Change color from green to black

For changing the layer color change the following line

app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [90,192,90], printable: false});

to the following

app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [0,0,0], printable: false});

 

For the box color change the following

try {
	app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.RGB, colorValue:[90,192,90], colorModel:ColorModel.PROCESS});
} catch (_) { }
reportGreen = app.activeDocument.swatches.item("ImageLabel");

to the following

reportGreen = app.activeDocument.swatches.item("Black");

 I did not get your last point of converting txt to jsx

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

Thank again. it doesnt work. the text and the lines is still green

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

Paste the whole code you are using here on the forum, let me test it

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

here is it:

 

//DESCRIPTION:Label image with its size
// Jongware, 13-Jan-2012
// No Guarantees, Etc.

if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
if (app.version < 6)
labelItem(app.selection[0]);
else
app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}

function labelItem (item)
{
// Save current selected layer because adding one will make it active!
activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
// Make sure the layer "ImageSize" exists
try {
app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [90,192,90], printable: true});
} catch (_) { }
reportLayer = app.activeDocument.layers.item("ImageLabel");

// Make sure the swatch "ImageGreen" exists
try {
app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.RGB, colorValue:[90,192,90], colorModel:ColorModel.PROCESS});
} catch (_) { }
reportGreen = app.activeDocument.swatches.item("ImageLabel");

// Make sure the paragraph style "ImageSize" exists
try {
app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", pointSize:12, fillColor:reportGreen, justification: Justification.CENTER_ALIGN});
} catch (_) { }
reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

// Make sure measurement units are in inches
hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
// .. and Smart Quotes is Off
sqt = app.activeDocument.textPreferences.typographersQuotes;
app.activeDocument.textPreferences.typographersQuotes = false;
// Save all of the previous settings to be able to restore them later :slightly_smiling_face:

// fetch width and height for the numbers, left and bottom for the lines
gb = item.geometricBounds;
// gb now holds [ top, left, bottom, right ] values -- get shorthand values
top = gb[0];
left = gb[1];
bottom = gb[2];
right = gb[3];

width = Math.round(Math.abs(right - left)*1000)/1000;
height= Math.round(Math.abs(bottom - top)*1000)/1000;

// Retrieve the current page.
// Actually, in CS5 and newer, page items have a property for this (parentPage),
// but I don't have that one near, and it's also nicer to make it work in CS4
// (and possibly even in older versions).
pg = item.parent;
while (1)
{
if (pg instanceof InsertionPoint)
pg = pg.parentTextframes[0];
if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
break;
pg = pg.parent;
}
// Draw text frame #1: Height
// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top, left - 0.7, top + height, left - 0.2 ],
textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
ignoreWrap: true } });
// ... and put text into it.
frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
frh.contents = String(Math.round(height * 25.4));

// Draw text frame #2: Width
// Put it at 0.2" off the bottom of the selection, 0.25" high, full width for convenience
frw = pg.textFrames.add(reportLayer, {geometricBounds:[ bottom + 0.2, left, bottom + 0.2 + 0.25, left + width ],
textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
ignoreWrap: true } });
// ... and put text into it.
frw.insertionPoints[0].appliedParagraphStyle = reportStyle;
frw.contents = String(Math.round(width * 25.4));

// Add vertical (height) line
lnh = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
// It appears in a default position so move it to the right one
lnh.paths[0].entirePath = [ [left - 0.15, top ], [left - 0.15, bottom ] ];
// add top stroke; it's 0.05" wide so offset left and right with 0.025"
lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, top ], [left-0.15 + 0.025, top] ] });
// add bottom stroke
lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, bottom ], [left-0.15 + 0.025, bottom] ] });

// Add horizontal (width) line
lnw = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
// Again we move it to the right position
lnw.paths[0].entirePath = [ [left, bottom + 0.15 ], [right, bottom + 0.15 ] ];
// Add end caps as above
lnw.paths.add ({ entirePath:[ [left, bottom+0.15 - 0.025 ], [left, bottom+0.15 + 0.025] ] });
lnw.paths.add ({ entirePath:[ [right, bottom+0.15 - 0.025 ], [right, bottom+0.15 + 0.025] ] });

// At this point we added four items: 2 text frames and 2 graphic lines.
// Group them together for convenience
app.activeDocument.groups.add ( [ frh, frw, lnh, lnw ] );

// Restore old layer selection
app.activeDocument.layoutWindows[0].activeLayer = activeLayer;

// Restore previous global settings
app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
app.activeDocument.textPreferences.typographersQuotes = sqt;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

I don't see the changes i mentioned in the response https://community.adobe.com/t5/indesign/help-with-existing-dimensions-lines-script-change-ft-to-mm/m... made in the script you just posted then how is it going to work?

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

sorry i pasted the wrong code:

 

//DESCRIPTION:Label image with its size
// Jongware, 13-Jan-2012
// No Guarantees, Etc.

if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
if (app.version < 6)
labelItem(app.selection[0]);
else
app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}

function labelItem (item)
{
// Save current selected layer because adding one will make it active!
activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
// Make sure the layer "ImageSize" exists
try {
app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [0,0,0], printable: false});
} catch (_) { }
reportLayer = app.activeDocument.layers.item("ImageLabel");

// Make sure the swatch "ImageGreen" exists
try {
app.activeDocument.colors.add({name:"ImageLabel", space:ColorSpace.RGB, colorValue:[90,192,90], colorModel:ColorModel.PROCESS});
} catch (_) { }
reportGreen = app.activeDocument.swatches.item("ImageLabel");

// Make sure the paragraph style "ImageSize" exists
try {
app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", pointSize:12, fillColor:reportGreen, justification: Justification.CENTER_ALIGN});
} catch (_) { }
reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

// Make sure measurement units are in inches
hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
// .. and Smart Quotes is Off
sqt = app.activeDocument.textPreferences.typographersQuotes;
app.activeDocument.textPreferences.typographersQuotes = false;
// Save all of the previous settings to be able to restore them later :slightly_smiling_face:

// fetch width and height for the numbers, left and bottom for the lines
gb = item.geometricBounds;
// gb now holds [ top, left, bottom, right ] values -- get shorthand values
top = gb[0];
left = gb[1];
bottom = gb[2];
right = gb[3];

width = Math.round(Math.abs(right - left)*1000)/1000;
height= Math.round(Math.abs(bottom - top)*1000)/1000;

// Retrieve the current page.
// Actually, in CS5 and newer, page items have a property for this (parentPage),
// but I don't have that one near, and it's also nicer to make it work in CS4
// (and possibly even in older versions).
pg = item.parent;
while (1)
{
if (pg instanceof InsertionPoint)
pg = pg.parentTextframes[0];
if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
break;
pg = pg.parent;
}
// Draw text frame #1: Height
// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top, left - 0.7, top + height, left - 0.2 ],
textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
ignoreWrap: true } });
// ... and put text into it.
frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
frh.contents = String(Math.round(height * 25.4));

// Draw text frame #2: Width
// Put it at 0.2" off the bottom of the selection, 0.25" high, full width for convenience
frw = pg.textFrames.add(reportLayer, {geometricBounds:[ bottom + 0.2, left, bottom + 0.2 + 0.25, left + width ],
textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
ignoreWrap: true } });
// ... and put text into it.
frw.insertionPoints[0].appliedParagraphStyle = reportStyle;
frw.contents = String(Math.round(width * 25.4));

// Add vertical (height) line
lnh = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
// It appears in a default position so move it to the right one
lnh.paths[0].entirePath = [ [left - 0.15, top ], [left - 0.15, bottom ] ];
// add top stroke; it's 0.05" wide so offset left and right with 0.025"
lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, top ], [left-0.15 + 0.025, top] ] });
// add bottom stroke
lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, bottom ], [left-0.15 + 0.025, bottom] ] });

// Add horizontal (width) line
lnw = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
// Again we move it to the right position
lnw.paths[0].entirePath = [ [left, bottom + 0.15 ], [right, bottom + 0.15 ] ];
// Add end caps as above
lnw.paths.add ({ entirePath:[ [left, bottom+0.15 - 0.025 ], [left, bottom+0.15 + 0.025] ] });
lnw.paths.add ({ entirePath:[ [right, bottom+0.15 - 0.025 ], [right, bottom+0.15 + 0.025] ] });

// At this point we added four items: 2 text frames and 2 graphic lines.
// Group them together for convenience
app.activeDocument.groups.add ( [ frh, frw, lnh, lnw ] );

// Restore old layer selection
app.activeDocument.layoutWindows[0].activeLayer = activeLayer;

// Restore previous global settings
app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
app.activeDocument.textPreferences.typographersQuotes = sqt;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

I gave two changes to be made you did only one change, that was the issue. To cut down the back and forth i am posting the edited script below

//DESCRIPTION:Label image with its size
// Jongware, 13-Jan-2012
// No Guarantees, Etc.

if (app.documents.length && app.selection.length == 1 && app.selection[0].hasOwnProperty("paths"))
{
	if (app.version < 6)
		labelItem(app.selection[0]);
	else
		app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size");
} else
{
	alert ("No valid selection.\nPlease select one single object with the black arrow before running this script.");
}

function labelItem (item)
{
	// Save current selected layer because adding one will make it active!
	activeLayer = app.activeDocument.layoutWindows[0].activeLayer;
	// Make sure the layer "ImageSize" exists
	try {
		app.activeDocument.layers.add({name:"ImageLabel", ignoreWrap:true, layerColor: [0,0,0], printable: false});
	} catch (_) { }
	reportLayer = app.activeDocument.layers.item("ImageLabel");

	// Make sure the swatch "ImageGreen" exists
	reportGreen = app.activeDocument.swatches.item("Black");

	// Make sure the paragraph style "ImageSize" exists
	try {
		app.activeDocument.paragraphStyles.add({name:"ImageLabel", appliedFont:"Arial", pointSize:12, fillColor:reportGreen, justification: Justification.CENTER_ALIGN});
	} catch (_) { }
	reportStyle = app.activeDocument.paragraphStyles.item("ImageLabel");

	// Make sure measurement units are in inches
	hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits;
	vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits;
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES_DECIMAL;
	// .. and Smart Quotes is Off
	sqt = app.activeDocument.textPreferences.typographersQuotes;
	app.activeDocument.textPreferences.typographersQuotes = false;
	// Save all of the previous settings to be able to restore them later :slightly_smiling_face:

	// fetch width and height for the numbers, left and bottom for the lines
	gb = item.geometricBounds;
	// gb now holds [ top, left, bottom, right ] values -- get shorthand values
	top = gb[0];
	left = gb[1];
	bottom = gb[2];
	right = gb[3];

	width = Math.round(Math.abs(right - left)*1000)/1000;
	height= Math.round(Math.abs(bottom - top)*1000)/1000;

	// Retrieve the current page.
	// Actually, in CS5 and newer, page items have a property for this (parentPage),
	// but I don't have that one near, and it's also nicer to make it work in CS4
	// (and possibly even in older versions).
	pg = item.parent;
	while (1)
	{
		if (pg instanceof InsertionPoint)
			pg = pg.parentTextframes[0];
		if (pg instanceof Document || pg instanceof Spread || pg instanceof Page)
			break;
		pg = pg.parent;
	}
	// Draw text frame #1: Height
	// Put it at the left of the selection, 0.5" wide and 0.2" off the left side
	frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top, left - 0.7, top + height, left - 0.2 ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frh.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frh.contents = String(Math.round(height * 25.4));

	// Draw text frame #2: Width
	// Put it at 0.2" off the bottom of the selection, 0.25" high, full width for convenience
	frw = pg.textFrames.add(reportLayer, {geometricBounds:[ bottom + 0.2, left, bottom + 0.2 + 0.25, left + width ],
			textFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN,
			ignoreWrap: true } });
	// ... and put text into it.
	frw.insertionPoints[0].appliedParagraphStyle = reportStyle;
	frw.contents = String(Math.round(width * 25.4));

	// Add vertical (height) line
	lnh = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// It appears in a default position so move it to the right one
	lnh.paths[0].entirePath = [ [left - 0.15, top ], [left - 0.15, bottom ] ];
	// add top stroke; it's 0.05" wide so offset left and right with 0.025"
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, top ], [left-0.15 + 0.025, top] ] });
	// add bottom stroke
	lnh.paths.add ({ entirePath:[ [left-0.15 - 0.025, bottom ], [left-0.15 + 0.025, bottom] ] });

	// Add horizontal (width) line
	lnw = pg.graphicLines.add (reportLayer, {strokeColor:reportGreen, strokeWeight:0.5});
	// Again we move it to the right position
	lnw.paths[0].entirePath = [ [left, bottom + 0.15 ], [right, bottom + 0.15 ] ];
	// Add end caps as above
	lnw.paths.add ({ entirePath:[ [left, bottom+0.15 - 0.025 ], [left, bottom+0.15 + 0.025] ] });
	lnw.paths.add ({ entirePath:[ [right, bottom+0.15 - 0.025 ], [right, bottom+0.15 + 0.025] ] });

	// At this point we added four items: 2 text frames and 2 graphic lines.
	// Group them together for convenience
	app.activeDocument.groups.add ( [ frh, frw, lnh, lnw ] );

	// Restore old layer selection
	app.activeDocument.layoutWindows[0].activeLayer = activeLayer;

	// Restore previous global settings
	app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu;
	app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu;
	app.activeDocument.textPreferences.typographersQuotes = sqt;

}

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

Thank you so much.

btw, this script doesnt work on illustrator. is illustrator uses another language for scripts?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

Same language, but entirely different Object Model (the methods and properties you use to access objects). There's some crossover, but not much. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
danezeq LATEST
Explorer ,
Jun 08, 2020

Copy link to clipboard

Copied

i see. thanks.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...