Copy link to clipboard
Copied
Hi community.
can someone please share a script which I can select all the text in my file, and it will display only the middle X value?
I already do have 2-3 other scripts which display the X & Y Locations of a bouding box of each object. Once i run the script the values are output into a notepad on my desktop which is super helpful. But now if Possible I just need the script to show the X value of the bounding box.
Try this (please use the </> control to paste code next time as the standard forum formatting can break the code):
// 2024 modified for middle center, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersBounds();
alert(theLayers.join("\n"));
// copy text to clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), theLayers.join("\n"));
executeAction(stringIDToTy
...
////////////////////////////////////////////////////////
function get_selected_layers_idx()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"
...
Copy link to clipboard
Copied
It would be helpful if you posted the other scripts so that the appropriate modifications can be made to the unknown variables such as ruler units, text file location etc. Here is something more generic...
var selectionBounds = app.activeDocument.selection.bounds;
var selectionLeft = selectionBounds[0].value;
var selectionTop = selectionBounds[1].value;
var selectionRight = selectionBounds[2].value;
var selectionBottom = selectionBounds[3].value;
var selectionWidth = selectionBounds[2].value - selectionBounds[0].value;
var selectionHeight = selectionBounds[3].value - selectionBounds[1].value;
var selectionXCenter = (selectionLeft) + (selectionWidth / 2);
var selectionYCenter = (selectionTop) + (selectionHeight / 2);
alert(selectionXCenter + ", " + selectionYCenter);
Copy link to clipboard
Copied
Sure here is the latest script I've been using:
// 2020, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersBounds ();
alert(theLayers.join("\n"));
// copy text to clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), theLayers.join("\n"));
executeAction(stringIDToTypeID("textToClipboard"), d, DialogModes.NO);
// write text to file
var txt = theLayers.join("\n");
var textFile = new File('~/Desktop' + '/' + 'layerList.txt');
textFile.open('w');
textFile.encoding = 'UTF-8';
textFile.write(txt);
textFile.close();
}
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = [];
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
// run through selected layers;
for(var i=0;i<c;i++){
try{activeDocument.backgroundLayer;
var theIndex = desc.getReference( i ).getIndex();
}catch(e){var theIndex = desc.getReference( i ).getIndex()+1 };
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top"))];
selectedLayers.push([theName, theseBounds]);
} catch (e) {}
}
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top"))];
selectedLayers = [[theName, theseBounds]];
} catch (e) {}
}
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
}
Copy link to clipboard
Copied
Try this (please use the </> control to paste code next time as the standard forum formatting can break the code):
// 2024 modified for middle center, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var theLayers = collectSelectedLayersBounds();
alert(theLayers.join("\n"));
// copy text to clipboard
var d = new ActionDescriptor();
d.putString(stringIDToTypeID("textData"), theLayers.join("\n"));
executeAction(stringIDToTypeID("textToClipboard"), d, DialogModes.NO);
// write text to file
var txt = theLayers.join("\n");
var textFile = new File('~/Desktop' + '/' + 'layerList.txt');
textFile.open('w');
textFile.encoding = 'UTF-8';
textFile.write(txt);
textFile.close();
}
////// collect bounds of selected layers //////
function collectSelectedLayersBounds() {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = [];
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var desc = executeActionGet(ref);
if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
desc = desc.getList(stringIDToTypeID('targetLayers'));
var c = desc.count;
// run through selected layers;
for (var i = 0; i < c; i++) {
try {
activeDocument.backgroundLayer;
var theIndex = desc.getReference(i).getIndex();
} catch (e) { var theIndex = desc.getReference(i).getIndex() + 1 };
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), theIndex);
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var left = theBounds.getUnitDoubleValue(stringIDToTypeID("left"));
var top = theBounds.getUnitDoubleValue(stringIDToTypeID("top"));
var right = theBounds.getUnitDoubleValue(stringIDToTypeID("right"));
var bottom = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"));
var centerX = (left + right) / 2;
var centerY = (top + bottom) / 2;
selectedLayers.push([theName, [centerX, centerY]]);
} catch (e) { }
}
// if only one:
} else {
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var left = theBounds.getUnitDoubleValue(stringIDToTypeID("left"));
var top = theBounds.getUnitDoubleValue(stringIDToTypeID("top"));
var right = theBounds.getUnitDoubleValue(stringIDToTypeID("right"));
var bottom = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"));
var centerX = (left + right) / 2;
var centerY = (top + bottom) / 2;
selectedLayers = [[theName, [centerX, centerY]]];
} catch (e) { }
}
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
}
Copy link to clipboard
Copied
unfortnately it didnt work to register the X value. Im still getting the registration of the top left corner.
The Script should have the X value of= 175
Copy link to clipboard
Copied
Hmm, it all worked fine for me, I tested on a text layer and a raster layer.
@c.pfaffenbichler – sorry to intrude, but this looks like your original code. Do you have any insights?
Copy link to clipboard
Copied
I suppose a Type Layer’s Text Box can be different from the actual pixel content, which the Bounds would describe. (See the transformation box for a Type Layer and a rasterized copy of it.)
@Stefan Cargoski , please provide the layered file for testing.
Copy link to clipboard
Copied
I got an update, I restarted my PC and the code is working!
But look at this weird little behaviour. For some reason, the script is showing the X value 2 pixels less than the tool bar. So when I position the text on the canvas, it's not sitting 100% where its meant to be, so I just add another 2 pixels, and it falls in place.
Has it got to do something with the bounding box of the text which is live?
Copy link to clipboard
Copied
Duplicate the text layer, rasterize it or convert it to paths, do you get a different result than the live text?
Copy link to clipboard
Copied
Otherwise please provide the layered file for testing.
Copy link to clipboard
Copied
I converted the text to shape and to path, and now Im getting the same values when I run the script, and same values in the tool bar panel. Although I need to giive these values based on live text, I'll just add another 2 pixels in the notepad value.
Thanks for the script Stepen.
Copy link to clipboard
Copied
Although I need to giive these values based on live text, I'll just add another 2 pixels in the notepad value.
Thanks for the script Stepen.
By @Stefan Cargoski
Converting the duped text layer is just a means to an end, the script can dupe/outline/rasterize to get the true value, then you can get the live text value and compare and shift as necessary, deleting the temp duped layer. But if you already know the offset is 2px in a specific direction, then there's no need for all that.
Copy link to clipboard
Copied
Are the Type Layers Point Text or Paragraph Text?
Copy link to clipboard
Copied
It was point text.
Kind regards.
Copy link to clipboard
Copied
Will you ever provide the file for testing?
Anyway … please try this:
// type layer properties;
// based on code by michael l hale;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myDocument = activeDocument;
var theWidth = myDocument.width;
var theHeight = myDocument.height;
// get values;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID('textKey'));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
if (layerDesc.hasKey(stringIDToTypeID('textKey')) == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var thePoint = textDesc.getObjectValue(stringIDToTypeID("textClickPoint"));
var theHor = thePoint.getUnitDoubleValue(stringIDToTypeID("horizontal"));
var theVer = thePoint.getUnitDoubleValue(stringIDToTypeID("vertical"));
alert (Number(theHor)*Number(theWidth)/100+"___"+Number(theVer)*Number(theHeight)/100);
// reset;
app.preferences.rulerUnits = originalRulerUnits;
}
};
edited
Copy link to clipboard
Copied
Sorry I forgot to upload it it, here's the file:
https://file.io/am9uOHhRwGo3
Copy link to clipboard
Copied
Sorry I forgot to upload it it, here's the file:
https://file.io/am9uOHhRwGo3
By @Stefan Cargoski
Copy link to clipboard
Copied
I tried the script, and get different values. One is with the live text, and the other with text converted to path.
Copy link to clipboard
Copied
I tried the script, and get different values. One is with the live text, and the other with text converted to path.
And is the value you get for live text roughly (rounding effects may apply) the one you expected?
Copy link to clipboard
Copied
Hi C.
The live text value, is always off by 3-4 pixels when I run Stephens script. Converting the text to shape is more accurate by 1 pixel. I believe its got to do with the registration point of the text, which is not sitting exaclty on a pixel. its a bit off, as you'll see in the example I zoomed in.
When converting the text to shape, the registration of the text, does shift from its location.
Is there a way to improve the script, so the location of the X value is more accurate for the live text?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Here's the test file:
https://file.io/Qcv3UB7q14Tf
yes, I saw the link, but its not making any difference. My text does not contain and FX, only colour.
Copy link to clipboard
Copied
The file is still not available.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The Script I posted seems to give the value of the transformation-center for a Type Layer with centered text pretty exactly.