| | |
Newbie: get part of filename path
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2007
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2007
Posts: 5
Reputation:
Solved Threads: 0
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:
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
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
// 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
•
•
Join Date: Sep 2007
Posts: 12
Reputation:
Solved Threads: 1
You could use something like:
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.
javascript Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Sep 2007
Posts: 12
Reputation:
Solved Threads: 1
•
•
•
•
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?
javascript Syntax (Toggle Plain Text)
// 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.
Last edited by badbart; Jan 1st, 2008 at 7:38 pm.
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Count the selected list box items using javascript
- Next Thread: Send an image to an SWF
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column cookies createrange() css cursor dependent disablefirebug dom download dropdown editor element engine error events explorer ext file form forms google gwt gxt hiddenvalue highlightedword html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump libcurl math matrixcaptcha media microsoft mimic object onmouseoutdivproblem onreadystatechange parent pdf php player post problem progressbar rated rating regex runtime scroll search security select session shopping size software sql star stars synchronous text textarea unicode validation w3c web website window windowofwords windowsxp wysiwyg xml \n






