473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Recursing subfolders assistance Please

6
Javascript and recursing subfolders assistance

-------------------------

I have this script that is a free extra download from SlideShowPro. It's a great script but I feel it needs to be tweaked a bit and need some assistance.

The script is not mine, it is actually: Copyright 2007 (c) Dominey Design Inc. All Rights Reserved. (I don't want anybody thinking I'm taking credit for the script.)

Here's what it does and what it should do IMO:

The script points to a folder of photos, and processes them to include thumbnails, It outputs them to an output folder putting the large photos in a folder called "large" and the thumbs in a "thumbs" folder. Then it writes everything to an xml file for the slideshow flash to look at. (All this is already scripted and works fine for one folder)

I would like some help on making the script recurse subdirectories in the source folder. (just one level) (Each subdirectory is an Album of photos)

Example:
The source folder is:
C:/sourcefolder

and it has multiple subfolders or "albums" such as:
C:/sourcefolder/albumone
C:/sourcefolder/albumtwo

It would be nice if the photoshop script would go through all the subfolders (recurse the subdirectories at least one level) to aleviate having to do each one seperatly or one at a time. It would also be nice if the output generated to a more proper folder structure for the multiple albums but it doesn't quite do it.

The output for each should be the same structure as the source you pulled from:
C:/outputprojectfolder/gallery/albumone/large
C:/outputprojectfolder/gallery/albumone/thumb
C:/outputprojectfolder/gallery/albumtwo/large
C:/outputprojectfolder/gallery/albumtwo/thumb

I know a little VBScript, but not enough to make the changes myself, but I do know that javascript can recurse subdirectories, and I know it can pull the source directory structure and reuse it in a var for the output directory structure.

At least I think so...

Let me throw out the part of the script that gets the files first and see what can be done, if anybody is interested in helping me:

Part of the code follows:

Expand|Select|Wrap|Line Numbers
  1. //create a new slideshow package 
  2. function slideShow() 
  3.    alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder."); 
  4.    var sourceFolder = Folder.selectDialog("Select your folder of images"); 
  5.    if(sourceFolder == null) 
  6.    { 
  7.          isCancelled = true; 
  8.    } 
  9.  
  10.    var totalPics = 0; 
  11.    if(!isCancelled) 
  12.    { 
  13.       var items = sourceFolder.getFiles(/\.(jpe|jpg|jpeg|gif|png|tif|tiff|bmp)/i); 
  14.       totalPics = items.length; 
  15.    } 
  16.  
  17.    //If no images are open 
  18.    if(totalPics == 0) 
  19.    { 
  20.       alert("No images were selected!");    
  21.    } 
  22.    //Otherwise get on with it 
First two questions:
1. Is there a way to make that recurse the subdirectories at least one level?
2. Is there a way to grab each subdirectory name, make it a var to re-use later in the script?

I appreciate your time and look forward to working with whoever on this little item! Let me know if more of the script is needed.

Sincerely,
OWeb

More information:
Feb 14 '08 #1
9 2724
acoder
16,027 Expert Mod 8TB
Maybe this thread will help.
Feb 14 '08 #2
OWeb
6
Yeah, you would think that would help but i don't know enough to take what's in that thread and apply it to the script i have ... I appreciate you time.
Feb 14 '08 #3
acoder
16,027 Expert Mod 8TB
Show the script code.
Feb 15 '08 #4
OWeb
6
Here's the whole thing, I've tried getting help from the people that made the script but the guy sold it to ssp and their forum moderators just keep saying it's a feature request and then don't respond back.

Anyway, I understand what the code does, it's a great time-saver. I feel that in line 99 - 100 is where something needs to be added to enable recursion in the sourcefolder.

The other important thing is to grab the name of each subfolder and make a var such as subfolderAlbum or something to use in the output folder structure and the output xml doc it creates. Once I have the recursion and subfolderAlbum var, I think I know enough to tweak the rest since it's just calling the var. Like in line 124 I would make it:

Expand|Select|Wrap|Line Numbers
  1. var lgPath = ("gallery/" + subfolderAlbum + "/large/");
Right?

I appreciate your time friend!

Here's the entire code from ssp, it's a free download:

Expand|Select|Wrap|Line Numbers
  1. // JavaScript Document
  2.  
  3. /***********************************************************************
  4.  
  5. SlideShowPro-Export-Folder for Photoshop CS2 / CS3
  6.  
  7. This script publishes imagery (from a folder of imagery) and an XML file for SlideShowPro to load. If you need to publish images already loaded in Photoshop, use the "SlideShowPro-Export" script instead.
  8.  
  9. For installation and usage instructions, refer to the "SSP_Export_PS_Guide.pdf" document included alongside this script.
  10.  
  11. Copyright 2007 (c) Dominey Design Inc. All Rights Reserved.
  12.  
  13. http://slideshowpro.net/
  14.  
  15. ************************************************************************/
  16.  
  17. //set unit preferences
  18. var strtRulerUnits = app.preferences.rulerUnits;
  19. var strtTypeUnits = app.preferences.typeUnits;
  20. app.preferences.rulerUnits = Units.PIXELS;
  21. app.preferences.typeUnits = TypeUnits.PIXELS;
  22.  
  23. //Set the cancelled flag
  24. var isCancelled = false;
  25.  
  26. /////////////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. //clean up input text with safe html characters
  29. function cleanTxt(txt)
  30. {
  31.     if(txt != null)
  32.     {
  33.         //replace unsafe characters
  34.         var clean = txt;
  35.         clean = clean.replace(/\"/gi,""");
  36.         clean = clean.replace(/\'/gi,""");
  37.         clean = clean.replace(/\</gi,"&lt;");
  38.         clean = clean.replace(/\>/gi,"&gt;");
  39.         return clean;
  40.     }
  41. }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////////////////////
  44.  
  45. //add hyphens to file names
  46. function webSafe(txt)
  47. {
  48.     if(txt != null)
  49.     {
  50.         //add hyphens and change extension
  51.         var web = txt;
  52.         web = web.replace(/\s/gi,"-");
  53.         var separator = web.lastIndexOf (".");
  54.         web = web.substr(0, separator);
  55.         web = web + ".jpg";
  56.         return web;
  57.     }
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62. //check for numbers in input
  63. function isNumber(num)
  64. {
  65.     var success;
  66.  
  67.     if(num == null)
  68.     {
  69.         isCancelled = true;
  70.         success = true;
  71.     }
  72.     else if(isFinite(parseInt(num)))
  73.     {
  74.         success = true;
  75.     }
  76.     else
  77.     {
  78.         success = false;
  79.     }
  80.  
  81.     return success;
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////////////////////
  85.  
  86. //create a new slideshow package
  87. function slideShow()
  88. {
  89.     alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder.");
  90.     var sourceFolder = Folder.selectDialog("Select your folder of images");
  91.     if(sourceFolder == null)
  92.     {
  93.             isCancelled = true;
  94.     }
  95.  
  96.     var totalPics = 0;
  97.     if(!isCancelled)
  98.     {
  99.         var items = sourceFolder.getFiles(/\.(jpe|jpg|jpeg|gif|png|tif|tiff|bmp)/i);
  100.         totalPics = items.length;
  101.     }
  102.  
  103.     //If no images are open
  104.     if(totalPics == 0)
  105.     {
  106.         alert("No images were selected!");    
  107.     }
  108.     //Otherwise get on with it
  109.     else
  110.     {
  111.         //create a new project folder
  112.         alert("Please create a new output folder.");
  113.         var projectFolder = Folder.selectDialog("Create a new output folder");
  114.         if(projectFolder == null)
  115.         {
  116.             isCancelled = true;
  117.         }
  118.  
  119.         if(!isCancelled)
  120.         {
  121.             //set the paths for each sub folder
  122.             var glPath = "gallery/";
  123.             var alPath = "gallery/album/";
  124.             var lgPath = "gallery/album/large/";
  125.             var tnPath = "gallery/album/thumb/";;
  126.  
  127.             //Create new xml file
  128.             var myXML = new File(projectFolder + "/images.xml");
  129.  
  130.             //create a new gallery folder
  131.             var gallery = new Folder(projectFolder.absoluteURI + "/" + glPath);
  132.             gallery.create();
  133.  
  134.             //create a new gallery folder
  135.             var album = new Folder(projectFolder.absoluteURI + "/" + alPath);
  136.             album.create();
  137.  
  138.             //create a new folder to save the images in
  139.             var large = new Folder(projectFolder.absoluteURI + "/" + lgPath);
  140.             large.create();
  141.  
  142.             //create a new folder to save the images in
  143.             var thumbs = new Folder(projectFolder.absoluteURI + "/" + tnPath);
  144.             thumbs.create();
  145.  
  146.             //set the export options for the images
  147.             var options = new ExportOptionsSaveForWeb();
  148.             options.quality = 60;
  149.             options.format = SaveDocumentType.JPEG;
  150.             options.optimized = true;
  151.         }
  152.  
  153.         //get the title
  154.         if(!isCancelled)
  155.         {
  156.             var title = cleanTxt(prompt("Enter an album title","ex: Vacation photos"));
  157.             if(title == null)
  158.             {
  159.                 isCancelled = true;
  160.             }
  161.         }
  162.  
  163.         //get the description
  164.         if(!isCancelled)
  165.         {
  166.             var desc = cleanTxt(prompt("Enter an album description","ex: Photos from my summer vacation"));
  167.             if(desc == null)
  168.             {
  169.                 isCancelled = true;
  170.             }
  171.         }
  172.  
  173.         //get the max image width from the user
  174.         if(!isCancelled)
  175.         {
  176.             var maxWidth = prompt("Enter the image max width","ex: 500");
  177.             while(!isNumber(maxWidth))
  178.             {
  179.                 alert("Please enter a number");
  180.                 maxWidth = prompt("Enter the image max width","ex: 500");
  181.             }
  182.             maxWidth = parseInt(maxWidth);
  183.         }
  184.  
  185.         //get the max image height from the user
  186.         if(!isCancelled)
  187.         {
  188.             var maxHeight = prompt("Enter the image max height","ex: 250");
  189.             while(!isNumber(maxHeight))
  190.             {
  191.                 alert("Please enter a number");
  192.                 maxHeight = prompt("Enter the image max height","ex: 250");
  193.             }
  194.             maxHeight = parseInt(maxHeight);
  195.         }
  196.  
  197.         //get the max thumbnail width from the user
  198.         if(!isCancelled)
  199.         {
  200.             var maxTnWidth = prompt("Enter the thumbnail max width","ex: 50");
  201.             while(!isNumber(maxTnWidth))
  202.             {
  203.                 alert("Please enter a number");
  204.                 maxTnWidth = prompt("Enter the thumbnail max width","ex: 50");
  205.             }
  206.             maxTnWidth = parseInt(maxTnWidth);
  207.         }
  208.  
  209.         //get the max thumbnail height from the user
  210.         if(!isCancelled)
  211.         {
  212.             var maxTnHeight = prompt("Enter the thumbnail max height","ex: 50");
  213.             while(!isNumber(maxTnHeight))
  214.             {
  215.                 alert("Please enter a number");
  216.                 maxTnHeight = prompt("Enter the thumbnail max height","ex: 50");
  217.             }
  218.             maxTnHeight = parseInt(maxTnHeight);
  219.         }
  220.  
  221.         if(!isCancelled)
  222.         {
  223.             //Open the new file with the mode set to (w) for write
  224.             myXML.open("w");
  225.  
  226.             //Write the opening XML tags with empty options
  227.             myXML.writeln("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  228.             myXML.writeln("<gallery>");
  229.             myXML.writeln("<album title=\"" + title + "\" description=\"" + desc + "\" lgPath=\"" + lgPath + "\" tnPath=\"" + tnPath  + "\" tn=\"\">");
  230.  
  231.             //Loop through the open images and get to work
  232.             for (var i = 0; i < totalPics; i++) 
  233.             { 
  234.                 //open each file and give edit access
  235.                 var fileRef = new File(items[i]);
  236.                 var docRef = app.open(fileRef);
  237.  
  238.                 //gather info on the current item
  239.                 app.activeDocument = docRef;
  240.                 var currWidth = app.activeDocument.width.value;
  241.                 var currHeight = app.activeDocument.height.value;
  242.                 var caption = cleanTxt(app.activeDocument.info.caption);
  243.                 var filename = webSafe(app.activeDocument.name);
  244.  
  245.                 //set the resample type
  246.                 var res = app.activeDocument.resolution;
  247.                 var resample = ResampleMethod.BICUBICSHARPER;
  248.  
  249.                 //write a new line to the xml file
  250.                 myXML.writeln("<img src=\"" + filename + "\" title=\"\" caption=\"" + caption + "\" link=\"\"/>"); 
  251.  
  252.                 //perform a fit image action on the current image
  253.                 //check the image size against the width constraint
  254.                 if(currWidth > maxWidth)
  255.                 {
  256.                     var wPercent = (maxWidth / currWidth);
  257.                     var newWidth = maxWidth + " px";
  258.                     var newHeight = Math.ceil(currHeight * wPercent) + " px";
  259.                     app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  260.  
  261.                     currWidth = app.activeDocument.width.value;
  262.                     currHeight = app.activeDocument.height.value;
  263.  
  264.                     if(currHeight > maxHeight)
  265.                     {
  266.                         var hPercent = (maxHeight / currHeight);
  267.                         newHeight = maxHeight + " px";
  268.                         newWidth = Math.ceil(currWidth * hPercent) + " px";
  269.                         app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  270.                     }
  271.                 }
  272.                 //now check the image size against the height restraint
  273.                 else if(currHeight > maxHeight)
  274.                 {
  275.                     var hPercent = (maxHeight / currHeight);
  276.                     var newHeight = maxHeight + " px";
  277.                     var newWidth = Math.ceil(currWidth * hPercent) + " px";
  278.                     app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  279.  
  280.                     currWidth = app.activeDocument.width.value;
  281.                     currHeight = app.activeDocument.height.value;
  282.  
  283.                     if(currWidth > maxWidth)
  284.                     {
  285.                         var wPercent = (maxWidth / currWidth);
  286.                         newWidth = maxWidth = " px";
  287.                         newHeight = Math.ceil(currHeight * wPercent) + " px";
  288.                         app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  289.                     }
  290.                 }
  291.  
  292.                 //export the current image, this will strip away all metadata to decrease the filesize
  293.                 app.activeDocument.exportDocument(new File(large + "/" + filename), ExportType.SAVEFORWEB, options);
  294.  
  295.                 //perform a fit image action on the current image
  296.                 //check the image size against the width constraint
  297.                 if(currWidth > maxTnWidth)
  298.                 {
  299.                     var wPercent = (maxTnWidth / currWidth);
  300.                     var newWidth = maxTnWidth + " px";
  301.                     var newHeight = Math.ceil(currHeight * wPercent) + " px";
  302.                     app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  303.  
  304.                     currWidth = app.activeDocument.width.value;
  305.                     currHeight = app.activeDocument.height.value;
  306.  
  307.                     if(currHeight > maxTnHeight)
  308.                     {
  309.                         var hPercent = (maxTnHeight / currHeight);
  310.                         newHeight = maxTnHeight + " px";
  311.                         newWidth = Math.ceil(currWidth * hPercent) + " px";
  312.                         app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  313.                     }
  314.                 }
  315.                 //now check the image size against the height restraint
  316.                 else if(currHeight > maxTnHeight)
  317.                 {
  318.                     var hPercent = (maxTnHeight / currHeight);
  319.                     var newHeight = maxTnHeight + " px";
  320.                     var newWidth = Math.ceil(currWidth * hPercent) + " px";
  321.                     app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  322.  
  323.                     currWidth = app.activeDocument.width.value;
  324.                     currHeight = app.activeDocument.height.value;
  325.  
  326.                     if(currWidth > maxTnWidth)
  327.                     {
  328.                         var wPercent = (maxTnWidth / currWidth);
  329.                         newWidth = maxTnWidth = " px";
  330.                         newHeight = Math.ceil(currHeight * wPercent) + " px";
  331.                         app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
  332.                     }
  333.                 }
  334.  
  335.                 //export the current image, this will strip away all metadata to decrease the filesize
  336.                 app.activeDocument.exportDocument(new File(thumbs + "/" + filename), ExportType.SAVEFORWEB, options);
  337.  
  338.                 //close the current file and don't save changes
  339.                 app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  340.             }
  341.  
  342.             //write the closing xml tags
  343.             myXML.writeln("</album>\n</gallery>");
  344.  
  345.             //close the output file
  346.             myXML.close();
  347.  
  348.             //let the user know that the operation completed
  349.             alert("Your album has been created!")
  350.  
  351.             //reset photoshops preferences
  352.             app.preferences.rulerUnits = strtRulerUnits;
  353.             app.preferences.typeUnits = strtTypeUnits;
  354.         }
  355.         else
  356.         {
  357.             alert("Script cancelled. Please try again.");
  358.         }
  359.     }
  360. }
  361. //create the slideshow source files
  362. slideShow();
Thanks again for your assistance.
Feb 15 '08 #5
acoder
16,027 Expert Mod 8TB
Where is the Folder object defined? That's not a native JavaScript object.
Feb 15 '08 #6
OWeb
6
I think its on line 89 and 90. The script runs in photoshop from the file/script option, you point it to the .jsx file you want to run which is the script above.

A dialog box opens and ask you to select your source. What you select is then your variable sourceFolder I'm guessing.


Expand|Select|Wrap|Line Numbers
  1. alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder.");
  2.     var sourceFolder = Folder.selectDialog("Select your folder of images");
Feb 15 '08 #7
acoder
16,027 Expert Mod 8TB
From lines 131 to 143, you're creating new Folder objects (gallery, thumb, etc.), so where is this Folder object defined? Do you add another script for this to work?
Feb 16 '08 #8
OWeb
6
Expand|Select|Wrap|Line Numbers
  1.  
  2.  var projectFolder = Folder.selectDialog("Create a new output folder");
  3.         if(projectFolder == null)
  4.  
The project folder is the "output" folder. I think it's being defined on line 113 & 114.

The same thing happens for the project folder, a dialog box appears, you drill down to the output folder and then what you select becomes projectFolder variable.

There is no other script, this script is completly self contained in what it does.
Feb 16 '08 #9
acoder
16,027 Expert Mod 8TB
Having looked again, it seems that this script is for Photoshop, not for a browser. Is that correct? You need to find the methods of the Folder object. Have you got some documentation for the functions/properties of the Folder object?
Feb 18 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kris | last post by:
Question: How do you create an Installer program using the Package and Deployment Wizard provided by Visual Studio Pro 6.0 (SP5) to include subfolders and their contents. I understand how to...
9
by: Tom | last post by:
How do you programatically get a list of subfolders in a folder? Thanks! Tom
3
by: TK | last post by:
Excuse me for multiple posting because I've posted this message to aspnet.security NG but have not got any response yet. I'm building an ASP.NET application works in Forms Authentication mode...
9
by: Bill Nguyen | last post by:
I need a VB routine to loop thru a select top folder to find all subfolders and list all subfolders/files under each of these subfolders. Any help is greatly appreciated. Bill
5
by: Robertico | last post by:
Is it possible to upload a complete folder including the subfolders using PHP ? Regards, Robertico
6
by: joey.powell | last post by:
I am working on a windows forms app where I need to do copies. I also need to show a progress bar. In order to be able to set the Maximum property for the progress bar and then update it during the...
3
by: rdemyan via AccessMonster.com | last post by:
How do I get the names of the task subfolders from Outlook? Also, when I right click on the "Tasks" folder in Outlook, I don't have the option to rename it. So, can I assume that this folder name...
2
by: pbrown | last post by:
Hey All, My problem appears to be pretty simple. I need a way of listing all the folders that exist in a certain directory. By all the folders, I mean ALL the folders; any file folder that exists...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.