Recursing subfolders assistance Please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OWeb
    New Member
    • Feb 2008
    • 6

    Recursing subfolders assistance Please

    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:/outputprojectfo lder/gallery/albumone/large
    C:/outputprojectfo lder/gallery/albumone/thumb
    C:/outputprojectfo lder/gallery/albumtwo/large
    C:/outputprojectfo lder/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:

    Code:
    //create a new slideshow package 
    function slideShow() 
    { 
       alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder."); 
       var sourceFolder = Folder.selectDialog("Select your folder of images"); 
       if(sourceFolder == null) 
       { 
             isCancelled = true; 
       } 
    
       var totalPics = 0; 
       if(!isCancelled) 
       { 
          var items = sourceFolder.getFiles(/\.(jpe|jpg|jpeg|gif|png|tif|tiff|bmp)/i); 
          totalPics = items.length; 
       } 
        
       //If no images are open 
       if(totalPics == 0) 
       { 
          alert("No images were selected!");    
       } 
       //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:
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Maybe this thread will help.

    Comment

    • OWeb
      New Member
      • Feb 2008
      • 6

      #3
      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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Show the script code.

        Comment

        • OWeb
          New Member
          • Feb 2008
          • 6

          #5
          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:

          Code:
          var lgPath = ("gallery/" + subfolderAlbum + "/large/");
          Right?

          I appreciate your time friend!

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

          Code:
          // JavaScript Document
          
          /***********************************************************************
          
          SlideShowPro-Export-Folder for Photoshop CS2 / CS3
          
          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.
          
          For installation and usage instructions, refer to the "SSP_Export_PS_Guide.pdf" document included alongside this script.
          
          Copyright 2007 (c) Dominey Design Inc. All Rights Reserved.
          
          http://slideshowpro.net/
          
          ************************************************************************/
          
          //set unit preferences
          var strtRulerUnits = app.preferences.rulerUnits;
          var strtTypeUnits = app.preferences.typeUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          app.preferences.typeUnits = TypeUnits.PIXELS;
          
          //Set the cancelled flag
          var isCancelled = false;
          
          /////////////////////////////////////////////////////////////////////////////////////////////
          
          //clean up input text with safe html characters
          function cleanTxt(txt)
          {
          	if(txt != null)
          	{
          		//replace unsafe characters
          		var clean = txt;
          		clean = clean.replace(/\"/gi,""");
          		clean = clean.replace(/\'/gi,""");
          		clean = clean.replace(/\</gi,"&lt;");
          		clean = clean.replace(/\>/gi,"&gt;");
          		return clean;
          	}
          }
          
          /////////////////////////////////////////////////////////////////////////////////////////////
          
          //add hyphens to file names
          function webSafe(txt)
          {
          	if(txt != null)
          	{
          		//add hyphens and change extension
          		var web = txt;
          		web = web.replace(/\s/gi,"-");
          		var separator = web.lastIndexOf (".");
          		web = web.substr(0, separator);
          		web = web + ".jpg";
          		return web;
          	}
          }
          
          /////////////////////////////////////////////////////////////////////////////////////////////
          
          //check for numbers in input
          function isNumber(num)
          {
          	var success;
          	
          	if(num == null)
          	{
          		isCancelled = true;
          		success = true;
          	}
          	else if(isFinite(parseInt(num)))
          	{
          		success = true;
          	}
          	else
          	{
          		success = false;
          	}
          	
          	return success;
          }
          
          /////////////////////////////////////////////////////////////////////////////////////////////
          
          //create a new slideshow package
          function slideShow()
          {
          	alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder.");
          	var sourceFolder = Folder.selectDialog("Select your folder of images");
          	if(sourceFolder == null)
          	{
          			isCancelled = true;
          	}
          
          	var totalPics = 0;
          	if(!isCancelled)
          	{
          		var items = sourceFolder.getFiles(/\.(jpe|jpg|jpeg|gif|png|tif|tiff|bmp)/i);
          		totalPics = items.length;
          	}
          	
          	//If no images are open
          	if(totalPics == 0)
          	{
          		alert("No images were selected!");	
          	}
          	//Otherwise get on with it
          	else
          	{
          		//create a new project folder
          		alert("Please create a new output folder.");
          		var projectFolder = Folder.selectDialog("Create a new output folder");
          		if(projectFolder == null)
          		{
          			isCancelled = true;
          		}
          	
          		if(!isCancelled)
          		{
          			//set the paths for each sub folder
          			var glPath = "gallery/";
          			var alPath = "gallery/album/";
          			var lgPath = "gallery/album/large/";
          			var tnPath = "gallery/album/thumb/";;
          		
          			//Create new xml file
          			var myXML = new File(projectFolder + "/images.xml");
          		
          			//create a new gallery folder
          			var gallery = new Folder(projectFolder.absoluteURI + "/" + glPath);
          			gallery.create();
          		
          			//create a new gallery folder
          			var album = new Folder(projectFolder.absoluteURI + "/" + alPath);
          			album.create();
          		
          			//create a new folder to save the images in
          			var large = new Folder(projectFolder.absoluteURI + "/" + lgPath);
          			large.create();
          		
          			//create a new folder to save the images in
          			var thumbs = new Folder(projectFolder.absoluteURI + "/" + tnPath);
          			thumbs.create();
          		
          			//set the export options for the images
          			var options = new ExportOptionsSaveForWeb();
          			options.quality = 60;
          			options.format = SaveDocumentType.JPEG;
          			options.optimized = true;
          		}
          		
          		//get the title
          		if(!isCancelled)
          		{
          			var title = cleanTxt(prompt("Enter an album title","ex: Vacation photos"));
          			if(title == null)
          			{
          				isCancelled = true;
          			}
          		}
          	
          		//get the description
          		if(!isCancelled)
          		{
          			var desc = cleanTxt(prompt("Enter an album description","ex: Photos from my summer vacation"));
          			if(desc == null)
          			{
          				isCancelled = true;
          			}
          		}
          		
          		//get the max image width from the user
          		if(!isCancelled)
          		{
          			var maxWidth = prompt("Enter the image max width","ex: 500");
          			while(!isNumber(maxWidth))
          			{
          				alert("Please enter a number");
          				maxWidth = prompt("Enter the image max width","ex: 500");
          			}
          			maxWidth = parseInt(maxWidth);
          		}
          		
          		//get the max image height from the user
          		if(!isCancelled)
          		{
          			var maxHeight = prompt("Enter the image max height","ex: 250");
          			while(!isNumber(maxHeight))
          			{
          				alert("Please enter a number");
          				maxHeight = prompt("Enter the image max height","ex: 250");
          			}
          			maxHeight = parseInt(maxHeight);
          		}
          		
          		//get the max thumbnail width from the user
          		if(!isCancelled)
          		{
          			var maxTnWidth = prompt("Enter the thumbnail max width","ex: 50");
          			while(!isNumber(maxTnWidth))
          			{
          				alert("Please enter a number");
          				maxTnWidth = prompt("Enter the thumbnail max width","ex: 50");
          			}
          			maxTnWidth = parseInt(maxTnWidth);
          		}
          		
          		//get the max thumbnail height from the user
          		if(!isCancelled)
          		{
          			var maxTnHeight = prompt("Enter the thumbnail max height","ex: 50");
          			while(!isNumber(maxTnHeight))
          			{
          				alert("Please enter a number");
          				maxTnHeight = prompt("Enter the thumbnail max height","ex: 50");
          			}
          			maxTnHeight = parseInt(maxTnHeight);
          		}
          		
          		if(!isCancelled)
          		{
          			//Open the new file with the mode set to (w) for write
          			myXML.open("w");
          			
          			//Write the opening XML tags with empty options
          			myXML.writeln("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
          			myXML.writeln("<gallery>");
          			myXML.writeln("<album title=\"" + title + "\" description=\"" + desc + "\" lgPath=\"" + lgPath + "\" tnPath=\"" + tnPath  + "\" tn=\"\">");
          			
          			//Loop through the open images and get to work
          			for (var i = 0; i < totalPics; i++) 
          			{ 
          				//open each file and give edit access
          				var fileRef = new File(items[i]);
          				var docRef = app.open(fileRef);
          				
          				//gather info on the current item
          				app.activeDocument = docRef;
          				var currWidth = app.activeDocument.width.value;
          				var currHeight = app.activeDocument.height.value;
          				var caption = cleanTxt(app.activeDocument.info.caption);
          				var filename = webSafe(app.activeDocument.name);
          				
          				//set the resample type
          				var res = app.activeDocument.resolution;
          				var resample = ResampleMethod.BICUBICSHARPER;
          				
          				//write a new line to the xml file
          				myXML.writeln("<img src=\"" + filename + "\" title=\"\" caption=\"" + caption + "\" link=\"\"/>"); 
          		
          				//perform a fit image action on the current image
          				//check the image size against the width constraint
          				if(currWidth > maxWidth)
          				{
          					var wPercent = (maxWidth / currWidth);
          					var newWidth = maxWidth + " px";
          					var newHeight = Math.ceil(currHeight * wPercent) + " px";
          					app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          			
          					currWidth = app.activeDocument.width.value;
          					currHeight = app.activeDocument.height.value;
          			
          					if(currHeight > maxHeight)
          					{
          						var hPercent = (maxHeight / currHeight);
          						newHeight = maxHeight + " px";
          						newWidth = Math.ceil(currWidth * hPercent) + " px";
          						app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          					}
          				}
          				//now check the image size against the height restraint
          				else if(currHeight > maxHeight)
          				{
          					var hPercent = (maxHeight / currHeight);
          					var newHeight = maxHeight + " px";
          					var newWidth = Math.ceil(currWidth * hPercent) + " px";
          					app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          			
          					currWidth = app.activeDocument.width.value;
          					currHeight = app.activeDocument.height.value;
          			
          					if(currWidth > maxWidth)
          					{
          						var wPercent = (maxWidth / currWidth);
          						newWidth = maxWidth = " px";
          						newHeight = Math.ceil(currHeight * wPercent) + " px";
          						app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          					}
          				}
          		
          				//export the current image, this will strip away all metadata to decrease the filesize
          				app.activeDocument.exportDocument(new File(large + "/" + filename), ExportType.SAVEFORWEB, options);
          				
          				//perform a fit image action on the current image
          				//check the image size against the width constraint
          				if(currWidth > maxTnWidth)
          				{
          					var wPercent = (maxTnWidth / currWidth);
          					var newWidth = maxTnWidth + " px";
          					var newHeight = Math.ceil(currHeight * wPercent) + " px";
          					app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          			
          					currWidth = app.activeDocument.width.value;
          					currHeight = app.activeDocument.height.value;
          			
          					if(currHeight > maxTnHeight)
          					{
          						var hPercent = (maxTnHeight / currHeight);
          						newHeight = maxTnHeight + " px";
          						newWidth = Math.ceil(currWidth * hPercent) + " px";
          						app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          					}
          				}
          				//now check the image size against the height restraint
          				else if(currHeight > maxTnHeight)
          				{
          					var hPercent = (maxTnHeight / currHeight);
          					var newHeight = maxTnHeight + " px";
          					var newWidth = Math.ceil(currWidth * hPercent) + " px";
          					app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          			
          					currWidth = app.activeDocument.width.value;
          					currHeight = app.activeDocument.height.value;
          			
          					if(currWidth > maxTnWidth)
          					{
          						var wPercent = (maxTnWidth / currWidth);
          						newWidth = maxTnWidth = " px";
          						newHeight = Math.ceil(currHeight * wPercent) + " px";
          						app.activeDocument.resizeImage(newWidth,newHeight,res,resample);
          					}
          				}
          		
          				//export the current image, this will strip away all metadata to decrease the filesize
          				app.activeDocument.exportDocument(new File(thumbs + "/" + filename), ExportType.SAVEFORWEB, options);
          			
          				//close the current file and don't save changes
          				app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          			}
          			
          			//write the closing xml tags
          			myXML.writeln("</album>\n</gallery>");
          			
          			//close the output file
          			myXML.close();
          			
          			//let the user know that the operation completed
          			alert("Your album has been created!")
          			
          			//reset photoshops preferences
          			app.preferences.rulerUnits = strtRulerUnits;
          			app.preferences.typeUnits = strtTypeUnits;
          		}
          		else
          		{
          			alert("Script cancelled. Please try again.");
          		}
          	}
          }
          //create the slideshow source files
          slideShow();
          Thanks again for your assistance.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Where is the Folder object defined? That's not a native JavaScript object.

            Comment

            • OWeb
              New Member
              • Feb 2008
              • 6

              #7
              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.


              Code:
              alert("Welcome to SlideShowPro Export for Photoshop\nPlease select a source folder.");
                  var sourceFolder = Folder.selectDialog("Select your folder of images");

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                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?

                Comment

                • OWeb
                  New Member
                  • Feb 2008
                  • 6

                  #9
                  Code:
                         
                   var projectFolder = Folder.selectDialog("Create a new output folder");
                          if(projectFolder == null)
                  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.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    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?

                    Comment

                    Working...