add part of filename

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Bardioch
Posts: 4
Joined: Tue Sep 19, 2017 1:00 pm

add part of filename

Post by Bardioch »

Heyho,

i´m new in using java script with photoshop and need some help. :| :| :|

I´ve already modyfied a script that creates a new text layer with the filename but now i neet to delete the first word.

For excample: "Cruise Tom" => "Tom"

I´m shure for you guys it´s quiet easy...

If someone could tell me where to insert which lines of code i would be verry thankful!!!

Bardioch


Code: Select all



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 = "Dateiname";

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 = 40;
myTextRef.font = "Berlin Sans FB Demi";
myTextRef.justification = Justification.CENTER;

var newColor = new SolidColor();
newColor.rgb.red = 80;
newColor.rgb.green = 42;
newColor.rgb.blue = 14;
myTextRef.color = newColor;

}
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!" );
}
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: add part of filename

Post by jaydoubleyou80 »

I think this has a few issues. First, my regular expressions may work, but I think there's something wrong with the pivotal one, and I don't know enough to fix it. Second, this leaves a leading space at the beginning of the text that doesn't need to be there (again, probably my regular expression). I'm sorry, I just don't have time to figure out how to get rid of it. Third, my "finalName" variable has two instances of the truncated file name in it, so I specified to use the first one [0]. That is another thing I think my regular expression may be doing incorrectly.

You also had something that I changed. An if asked about the length of the name, but I changed this to look for if there was a space in the name. If there wasn't, nothing happens, but if there is, the name is changed and the text layer is applied. I think this might be a more reliable way to determine if the first word needs to come off.

Hope it helps.

Code: Select all

#target Photoshop
if ( documents.length > 0 ){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
try {
var docRef = activeDocument;
if (docRef.name.match(/[\ ]/)){
// Now create a text layer at the front
var myLayerRef = docRef.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Dateiname";
var myTextRef = myLayerRef.textItem;
// modify file name
var fileNameNoExtension = docRef.name;
fileNameNoExtension = fileNameNoExtension.split( "." );
fileNameNoExtension = fileNameNoExtension.join(".");
var finalName=fileNameNoExtension.match(/(?!\w)([\w*\ ]*)/)
myTextRef.contents = finalName[0]
// off set the text to be in the middle
myTextRef.position = Array( docRef.width / 2, docRef.height / 2 );
myTextRef.size = 40;
myTextRef.font = "Berlin Sans FB Demi";
myTextRef.justification = Justification.CENTER;
var newColor = new SolidColor();
newColor.rgb.red = 80;
newColor.rgb.green = 42;
newColor.rgb.blue = 14;
myTextRef.color = newColor;
}
}
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!" );
}
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: add part of filename

Post by txuku »

Bonjour

You can try to insert this code line 28 then you delete the alerts :

Code: Select all

	fileNameNoExtension=fileNameNoExtension.split( " " );
alert(fileNameNoExtension[0]);
alert(fileNameNoExtension[1]);
myLayerRef.name = fileNameNoExtension[1];
myTextRef.contents = fileNameNoExtension[1];
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: add part of filename

Post by jaydoubleyou80 »

txuku, this is much better than what I was trying to do.
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: add part of filename

Post by txuku »

:D
Bardioch
Posts: 4
Joined: Tue Sep 19, 2017 1:00 pm

Re: add part of filename

Post by Bardioch »

Hey guys,

thank you so much, i´ve been testing it for a few days now and this is incredible helpful to me!!!

i only found out that it can´t handle our austrian "umlaute" like äÄ/öÖ/üÜ and Áá/Éé/ß and so on... is possible to ad them???

Oh, and again.... THANK YOU SO MUCH!!!! :D :D :D
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: add part of filename

Post by txuku »

Bonjour

Since I program I renounce accents and punctuation ............... :)