Hello all.
i'm real new to any scripting so please be gentle.

I'd like to write a script based on the AddFileNamePlusDate.js for photoshop.

For anyone who doesn't know this script gets the file name and adds a text layer of a photoshop document and places the file name text into a text object on the new layer followed by the date.

What i'd like to achieve is to include one of the parent directories of the file in the text that is passed into the text item - but as i am new to java i don't know how to go about doing this.

Hope that makes sense.

any help is much appreciated.

mike

Hi there gpsmike and welcome to Daniweb,

Your question sounds like a javascript question as opposed to pure Java and there is another forum for JS under Web Development. Try posting there and maybe someone with more knowledge of JS might be able to help you.

Good luck :)
darkagn

Thank you darkagn.
I'll go and ask over there.
I didn't originally go there as it's not for web development as such but i guess the principles will be the same.

mike

Hi there!

With JavaScript, it's possible to do many things. Including some path wouldn't be complex, but... only if we see the code! Please post some code and we'll see what we can do.

Thanks for your interest in helping me sort this out.

I've had a go at sorting this out further over the last few days but am still stuck.

Here's the code as it stands:

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

if ( documents.length > 0 )
{
	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 = "path_layer";
		
		var myTextRef = myLayerRef.textItem;
		var TextBefore = "© 2008 ";
		
		// defining which folder
		// Fill in specific folder name between the quotes
		var myFolder = "ABC Clients/";

		// Find the number of characters in the specified folder name
                var myLength = myFolder.length;
		
		// creating a variable of specified folder
		var myGraphicsFolder = docRef.path.fsName.indexOf(myFolder);
		
		// Find the next forward slash in the folder structure
                var myWhatFolder = "/";
		var myNextFolder = docRef.path.fsName.indexOf(myWhatFolder, myGraphicsFolder+myLength)
                
		// originally i got the full path name
		// now the following puts the number of characters in my specified folder and more
		// this is where i need a lot of help please
		// This populates myTextRef with the information requested
		myTextRef.contents = myLength + " " + myNextFolder + " " + myGraphicsFolder + TextBefore + docRef.path.fsName.substring(myGraphicsFolder, myNextFolder) + " " + docRef.name
		

		// these are methods i thought to investigate as they may help in me trying to achieve what i need
		// "Graphics".indexOf(docRef.path.fsName, 0)
		// docRef.path.fsName.substr(40, 48)
		// "String Literal".indexOf(substring, startindex)
		// stringvar.substr(start [, length ])
		// stringObj.slice(start, [end])
		// stringvar.substr(start [, length ])
		// myTextRef.contents = docRef.path.fsName + "/" + docRef.name
		// var parsename = activeDocument.name.substring(0,2)
		// Set font size in Points
		myTextRef.size = 4;
		
		//Set font - use GetFontName.js to get exact name
		myTextRef.font = "Tahoma";
		
		//Set text colour in RGB values
		var newColor = new SolidColor();
	newColor.rgb.red = 0;
	newColor.rgb.green = 0;
	newColor.rgb.blue = 0;
	myTextRef.color = newColor;


		// off set the text to be in the top left corner
		myTextRef.position = new Array( 50, 50 );

		// Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
		myLayerRef.blendMode = BlendMode.NORMAL;
		
		// select opacity in percentage
		myLayerRef.opacity = 100;
	}
	catch( e )
	{
		// An error occurred. Restore ruler units, then propagate the error back
		// to the user
		preferences.rulerUnits = originalRulerUnits;
		throw e;
	}

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

// end of code

I did, at one point manage to get the information i wanted and place this in a new layer in photoshop by using the number of characters within the path but this seems a clumsy way to do things in case a folder is named differently.

Assume the path to my graphic is as follows:

root/user/username/office/clients/clientName/clientNameGraphics/EventDate/processedImages/finalGraphic

What i would like to return with my script in my final document is the EventDate folder information and file name information so my text layer in photoshop will look as follows:

© 2008 EventDate finalGraphic


I would like to be able to search for the content by finding the / character within the string, this way i could ask for the folder name at a certain position within the path....is this possible?

Any help on this would be extremely grateful.
Mike

You could use something like:

function getFname(yStr){
	var sFileName = "";
	for (nloop=yStr.length-1;nloop>1;nloop--){
		if (yStr.charAt(nloop)=="/"){
			sFileName=yStr.substring(nloop+1,yStr.length);
			break;
		}
		if (yStr.charAt(nloop)=="\\"){  // backslash must be escaped
			sFileName=yStr.substring(nloop+1,yStr.length);
			break;
		}
	}
	return sFileName;
}

I'm looking for the filename string, rather than a directory name string, in that function, but you could alter the code to find whatever you want in the string.

Thanks badbart.

as i'm new to scripting it will take me a while to fully grasp what is going on here and alter it to fit my needs.

i guess if i need to find out two differing sections of the file path i could use this concept too, is that right?

Thanks badbart.

as i'm new to scripting it will take me a while to fully grasp what is going on here and alter it to fit my needs.

i guess if i need to find out two differing sections of the file path i could use this concept too, is that right?

Here's the function with a few comments to help you understand what it's actually doing.

// this function accepts a string (yStr) and returns a string
function getFname(yStr){	
	var sFileName = "";	// this is an empty string to hold the string we parse of yStr
	for (nloop=yStr.length-1;nloop>1;nloop--){		
	// this loop begins with the last character in yStr and progresses to the first character, it passes through all the characters from end to beginning - unless the sought character is found (a forward or backward slash, in this case)
		if (yStr.charAt(nloop)=="/"){			
			// if a forward slash is found, set the empty string container to the substring from yStr containing the character after the backslash to the end of yStr and break out of the loop
			sFileName=yStr.substring(nloop+1,yStr.length);
			break;		
		}		
		if (yStr.charAt(nloop)=="\\"){  // backslash must be escaped			
			// if a backslash is found, set the empty string container to the substring from yStr containing the character after the backslash to the end of yStr and break out of the loop
			sFileName=yStr.substring(nloop+1,yStr.length);
			break;		
		}	
	}	
	// the substring (or an empty string, if no slash found) is returned to the caller
	return sFileName;
}

So if you pass a path that only contains directory names to the function, it will return the last directory name in the path string. If you pass a path containing a filename, you will need to modify the function to keep looking for the second to last slash to get the subdirectory name.

Thank you so much for your input.
I look forward to understanding it all. I'll let you know how i get on.

This article has been dead for over six months. Start a new discussion instead.