Skip to main content
bobd7221748
Inspiring
March 30, 2023
Answered

Script to add document size in cm

  • March 30, 2023
  • 1 reply
  • 644 views

Hello Everyone,

 

I currently use a script to add the filename as a text layer on my document. I'm wondering if someone could suggest a script, or adjust my existing one below, to add the document width & height in cm.

 

// this script is a variation of the script addTimeStamp.js that is installed with PH7

if ( documents.length > 0 )
{
	var originalDialogMode = app.displayDialogs;
	app.displayDialogs = DialogModes.ERROR;
	var originalRulerUnits = preferences.rulerUnits;
	preferences.rulerUnits = Units.PIXELS;
	
	try
	{
		var docRef = activeDocument;

		// Now create a text layer at the front
		var myLayerRef = docRef.artLayers.add();
		myLayerRef.kind = LayerKind.TEXT;
		myLayerRef.name = "Filename";
		
		var myTextRef = myLayerRef.textItem;
		
		// strip the extension off
		var fileNameNoExtension = docRef.name;
		fileNameNoExtension = fileNameNoExtension.split( "." );
		if ( fileNameNoExtension.length > 1 ) {
			fileNameNoExtension.length--;
		}
		fileNameNoExtension = fileNameNoExtension.join(".");
			
		myTextRef.contents = fileNameNoExtension;
		
		// off set the text to be in the middle
		myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
		myTextRef.size = 20;
	}
	catch( e )
	{
		// An error occurred. Restore ruler units, then propagate the error back
		// to the user
		preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
		throw e;
	}

	// Everything went Ok. Restore ruler units
	preferences.rulerUnits = originalRulerUnits;
	app.displayDialogs = originalDialogMode;
}
else
{
	alert( "You must have a document open to add the filename!" );
}


So, if a document is 5000px x 10000px at 254 dpi, I would ideally like '50 x 100 cm' to output as a text layer.

 

I'm not worried about where it is placed or how it is formatted, as I can sort that afterwards in the action (which I do for the filename currently).

 

Any suggestions would be vastly appreciated!

 

Thanks in advance 👍

This topic has been closed for replies.
Correct answer Stephen Marsh

@bobd7221748 

 

Try this:

 

// this script is a variation of the script addTimeStamp.js that is installed with PH7

if ( documents.length > 0 )
{
	var originalDialogMode = app.displayDialogs;
	app.displayDialogs = DialogModes.ERROR;
	var originalRulerUnits = preferences.rulerUnits;
	preferences.rulerUnits = Units.PIXELS;

	try
	{
		var docRef = activeDocument;

		// Now create a text layer at the front
		var myLayerRef = docRef.artLayers.add();
		myLayerRef.kind = LayerKind.TEXT;
		var myTextRef = myLayerRef.textItem;
		var theDims = ((docRef.width.value / docRef.resolution) * 2.54).toFixed(2) + " x " + ((docRef.height.value / docRef.resolution) * 2.54).toFixed(2) + " cm";
		var simplifiedDims = theDims.replace(/\.00/g, "");
		myTextRef.contents = simplifiedDims;
		
		// off set the text to be in the middle
		myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
		myTextRef.size = 20;
	}
	catch( e )
	{
		// An error occurred. Restore ruler units, then propagate the error back
		// to the user
		preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
		throw e;
	}

	// Everything went Ok. Restore ruler units
	preferences.rulerUnits = originalRulerUnits;
	app.displayDialogs = originalDialogMode;
}
else
{
	alert( "You must have a document open to add the filename!" );
}

 

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
April 6, 2023

@bobd7221748 

 

Try this:

 

// this script is a variation of the script addTimeStamp.js that is installed with PH7

if ( documents.length > 0 )
{
	var originalDialogMode = app.displayDialogs;
	app.displayDialogs = DialogModes.ERROR;
	var originalRulerUnits = preferences.rulerUnits;
	preferences.rulerUnits = Units.PIXELS;

	try
	{
		var docRef = activeDocument;

		// Now create a text layer at the front
		var myLayerRef = docRef.artLayers.add();
		myLayerRef.kind = LayerKind.TEXT;
		var myTextRef = myLayerRef.textItem;
		var theDims = ((docRef.width.value / docRef.resolution) * 2.54).toFixed(2) + " x " + ((docRef.height.value / docRef.resolution) * 2.54).toFixed(2) + " cm";
		var simplifiedDims = theDims.replace(/\.00/g, "");
		myTextRef.contents = simplifiedDims;
		
		// off set the text to be in the middle
		myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
		myTextRef.size = 20;
	}
	catch( e )
	{
		// An error occurred. Restore ruler units, then propagate the error back
		// to the user
		preferences.rulerUnits = originalRulerUnits;
        app.displayDialogs = originalDialogMode;
		throw e;
	}

	// Everything went Ok. Restore ruler units
	preferences.rulerUnits = originalRulerUnits;
	app.displayDialogs = originalDialogMode;
}
else
{
	alert( "You must have a document open to add the filename!" );
}

 

 

bobd7221748
Inspiring
April 7, 2023

Thanks @Stephen Marsh, helpful as ever!

 

As I didn't get any immediate replies, I thought I'd try ChatGPT... It staggered me how easy it was to generate scripts like this. I ended up going down a bit of a rabbit hole seeing how much I could achieve with my very limited scripting knowledge - which ended up being surprisingly far! I also ended up getting a better grasp of how the scripts were functioning too, so I intuitively learnt a fair bit very in the process 😊

 

Thanks again!