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
Reply

Join Date: Dec 2007
Posts: 5
Reputation: gpsmike is an unknown quantity at this point 
Solved Threads: 0
gpsmike gpsmike is offline Offline
Newbie Poster

Newbie: get part of filename path

 
0
  #1
Dec 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 798
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Newbie: get part of filename path

 
0
  #2
Dec 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: gpsmike is an unknown quantity at this point 
Solved Threads: 0
gpsmike gpsmike is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #3
Dec 22nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 9
Reputation: WebManiac is an unknown quantity at this point 
Solved Threads: 1
WebManiac WebManiac is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #4
Dec 24th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: gpsmike is an unknown quantity at this point 
Solved Threads: 0
gpsmike gpsmike is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #5
Dec 30th, 2007
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. // this script is a further variation of the script addTimeStamp.js that is installed with PH7
  3.  
  4. if ( documents.length > 0 )
  5. {
  6. var originalRulerUnits = preferences.rulerUnits;
  7. preferences.rulerUnits = Units.PIXELS;
  8.  
  9. try
  10. {
  11. var docRef = activeDocument;
  12.  
  13. // Now create a text layer at the front
  14. var myLayerRef = docRef.artLayers.add();
  15. myLayerRef.kind = LayerKind.TEXT;
  16. myLayerRef.name = "path_layer";
  17.  
  18. var myTextRef = myLayerRef.textItem;
  19. var TextBefore = "© 2008 ";
  20.  
  21. // defining which folder
  22. // Fill in specific folder name between the quotes
  23. var myFolder = "ABC Clients/";
  24.  
  25. // Find the number of characters in the specified folder name
  26. var myLength = myFolder.length;
  27.  
  28. // creating a variable of specified folder
  29. var myGraphicsFolder = docRef.path.fsName.indexOf(myFolder);
  30.  
  31. // Find the next forward slash in the folder structure
  32. var myWhatFolder = "/";
  33. var myNextFolder = docRef.path.fsName.indexOf(myWhatFolder, myGraphicsFolder+myLength)
  34.  
  35. // originally i got the full path name
  36. // now the following puts the number of characters in my specified folder and more
  37. // this is where i need a lot of help please
  38. // This populates myTextRef with the information requested
  39. myTextRef.contents = myLength + " " + myNextFolder + " " + myGraphicsFolder + TextBefore + docRef.path.fsName.substring(myGraphicsFolder, myNextFolder) + " " + docRef.name
  40.  
  41.  
  42. // these are methods i thought to investigate as they may help in me trying to achieve what i need
  43. // "Graphics".indexOf(docRef.path.fsName, 0)
  44. // docRef.path.fsName.substr(40, 48)
  45. // "String Literal".indexOf(substring, startindex)
  46. // stringvar.substr(start [, length ])
  47. // stringObj.slice(start, [end])
  48. // stringvar.substr(start [, length ])
  49. // myTextRef.contents = docRef.path.fsName + "/" + docRef.name
  50. // var parsename = activeDocument.name.substring(0,2)
  51. // Set font size in Points
  52. myTextRef.size = 4;
  53.  
  54. //Set font - use GetFontName.js to get exact name
  55. myTextRef.font = "Tahoma";
  56.  
  57. //Set text colour in RGB values
  58. var newColor = new SolidColor();
  59. newColor.rgb.red = 0;
  60. newColor.rgb.green = 0;
  61. newColor.rgb.blue = 0;
  62. myTextRef.color = newColor;
  63.  
  64.  
  65. // off set the text to be in the top left corner
  66. myTextRef.position = new Array( 50, 50 );
  67.  
  68. // Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
  69. myLayerRef.blendMode = BlendMode.NORMAL;
  70.  
  71. // select opacity in percentage
  72. myLayerRef.opacity = 100;
  73. }
  74. catch( e )
  75. {
  76. // An error occurred. Restore ruler units, then propagate the error back
  77. // to the user
  78. preferences.rulerUnits = originalRulerUnits;
  79. throw e;
  80. }
  81.  
  82. // Everything went Ok. Restore ruler units
  83. preferences.rulerUnits = originalRulerUnits;
  84. }
  85. else
  86. {
  87. alert( "You must have a document open to add the filename!" );
  88. }
  89.  
  90. // 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 12
Reputation: badbart is an unknown quantity at this point 
Solved Threads: 1
badbart badbart is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #6
Dec 31st, 2007
You could use something like:
  1. function getFname(yStr){
  2. var sFileName = "";
  3. for (nloop=yStr.length-1;nloop>1;nloop--){
  4. if (yStr.charAt(nloop)=="/"){
  5. sFileName=yStr.substring(nloop+1,yStr.length);
  6. break;
  7. }
  8. if (yStr.charAt(nloop)=="\\"){ // backslash must be escaped
  9. sFileName=yStr.substring(nloop+1,yStr.length);
  10. break;
  11. }
  12. }
  13. return sFileName;
  14. }
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: gpsmike is an unknown quantity at this point 
Solved Threads: 0
gpsmike gpsmike is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #7
Jan 1st, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 12
Reputation: badbart is an unknown quantity at this point 
Solved Threads: 1
badbart badbart is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
1
  #8
Jan 1st, 2008
Originally Posted by gpsmike View Post
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.
  1. // this function accepts a string (yStr) and returns a string
  2. function getFname(yStr){
  3. var sFileName = ""; // this is an empty string to hold the string we parse of yStr
  4. for (nloop=yStr.length-1;nloop>1;nloop--){
  5. // 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)
  6. if (yStr.charAt(nloop)=="/"){
  7. // 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
  8. sFileName=yStr.substring(nloop+1,yStr.length);
  9. break;
  10. }
  11. if (yStr.charAt(nloop)=="\\"){ // backslash must be escaped
  12. // 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
  13. sFileName=yStr.substring(nloop+1,yStr.length);
  14. break;
  15. }
  16. }
  17. // the substring (or an empty string, if no slash found) is returned to the caller
  18. return sFileName;
  19. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: gpsmike is an unknown quantity at this point 
Solved Threads: 0
gpsmike gpsmike is offline Offline
Newbie Poster

Re: Newbie: get part of filename path

 
0
  #9
Jan 2nd, 2008
Thank you so much for your input.
I look forward to understanding it all. I'll let you know how i get on.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC