Search code examples
javascriptdirectoryapplescriptphotoshopfinder

Javascript: Change 'choose folder' to specific location


I have a javascript I found but it starts with having the user choose a folder where it will grab files. I want to create a watch folder so I want to tell the javascript the folder to grab the files from, not let the user choose. I can't for the life of me figure out how to do this. I know applescript but cannot grasp javascript. Thank you!

Here is what I believe the area I need to change:

function main() {
   // user settings
   var prefs = new Object();
   prefs.sourceFolder         = '/Volumes/SERVER_RAID/•Current/MPC';  // default browse location (default: '~')
   prefs.removeFileExtensions = true; // remove filename extensions for imported layers (default: true)
   prefs.savePrompt           = true; // display save prompt after import is complete (default: false)
   prefs.closeAfterSave       = true; // close import document after saving (default: false)

   // prompt for source folder
   var sourceFolder = Folder.selectDialog('Where are the Front and Back files?', Folder(prefs.sourceFolder));

   // ensure the source folder is valid
   if (!sourceFolder) {
      return;
   }
   else if (!sourceFolder.exists) {
      alert('Source folder not found.', 'Script Stopped', true);
      return;
   }

   // add source folder to user settings
   prefs.sourceFolder = sourceFolder;

   // get a list of files
   var fileArray = getFiles(prefs.sourceFolder);

   // if files were found, proceed with import
   if (fileArray.length) {
      importFolderAsLayers(fileArray, prefs);
   }
   // otherwise, diplay message
   else {
      alert("The selected folder doesn't contain any recognized images.", 'No Files Found', false);
   }
}

///////////////////////////////////////////////////////////////////////////////
// getFiles - get all files within the specified source
///////////////////////////////////////////////////////////////////////////////
function getFiles(sourceFolder) {
   // declare local variables
   var fileArray = new Array();
   var extRE = /\.(?:png)$/i;

   // get all files in source folder
   var docs = sourceFolder.getFiles();
   var len = docs.length;
   for (var i = 0; i < len; i++) {
      var doc = docs[i];

      // only match files (not folders)
      if (doc instanceof File) {
         // store all recognized files into an array
         var docName = doc.name;
         if (docName.match(extRE)) {
            fileArray.push(doc);
         }
      }
   }

   // return file array
   return fileArray;
}

///////////////////////////////////////////////////////////////////////////////


Solution

  • To achieve this you'll need to specify the path to your desired folder as String.

    If you take a look at the updated code sample below you'll notice that line 4 of your original code which read:

    prefs.sourceFolder = '/Volumes/SERVER_RAID/•Current/MPC';
    

    has been changed to:

    prefs.sourceFolder = Folder('~/Desktop/targetFolder');
    

    This now assumes the target folder is named targetFolder and it resides in your Desktop folder. You'll need to change the '~/Desktop/targetFolder' part as necessary to point to the folder you actually want. It also assumes you're running this on macOS as the shortcut to the Desktop folder (i.e. the ~/ part) will not be recognized on Windows.


    What are the other ways I can specify the path?

    1. You can specify path name using absolute paths such as:

      prefs.sourceFolder = Folder('/Users/JohnDoe/Desktop/targetFolder');
      

      Note: This example actually points to the same folder as the first example. Assuming that the user is called John Doe of course!

    2. More info about setting file paths can be found here.


    What other changes were made to your code example:

    1. Lines 9 to 22 in your example code which read:

      // prompt for source folder
      var sourceFolder = Folder.selectDialog('Where are the Front and Back files?', Folder(prefs.sourceFolder));
      
      // ensure the source folder is valid
      if (!sourceFolder) {
         return;
      }
      else if (!sourceFolder.exists) {
         alert('Source folder not found.', 'Script Stopped', true);
         return;
      }
      
      // add source folder to user settings
      prefs.sourceFolder = sourceFolder;
      

      are now redundant. Instead, they have been replaced with the following snippet which will alert you if the folder you specified cannot be found:

      // ensure the source folder exists.
      if (!prefs.sourceFolder.exists) {
        alert('Source folder not found.\n' + prefs.sourceFolder, 'Script Stopped', true);
        return;
      }
      
    2. I added main() on line 30 below to invoke the main function. However, if you have that elsewhere in your code you can delete it.


    Updated code sample:

    function main() {   
      // User settings
      var prefs = new Object();
      prefs.sourceFolder         = Folder('~/Desktop/targetFolder');
      prefs.removeFileExtensions = true;
      prefs.savePrompt           = true;
      prefs.closeAfterSave       = true;
    
      // ensure the source folder exists.
      if (!prefs.sourceFolder.exists) {
        alert('Source folder not found.\n' + prefs.sourceFolder, 'Script Stopped', true);
        return;
      }
    
      // Get a list of files
      var fileArray = getFiles(prefs.sourceFolder);
    
      // If files were found, proceed with your tasks.
      if (fileArray.length) {
        alert('I found image(s) in the specified folder\n' +
            'Now you need to write code to perform out a task :)');
       // <-- Continiue your code here
      }
      // otherwise, diplay message
      else {
        alert('The selected folder doesn\'t contain any recognized images.', 'No Files Found', false);
      }
    }
    
    main(); // <-- Invokes the `main` function
    
    /**
     * getFiles - get all files within the specified source
     * @param {String} sourceFolder - the path to the source folder.
     */
    function getFiles(sourceFolder) {
      // declare local variables
      var fileArray = new Array();
      var extRE = /\.(?:png)$/i;
    
      // get all files in source folder
      var docs = sourceFolder.getFiles();
      var len = docs.length;
    
      for (var i = 0; i < len; i++) {
        var doc = docs[i];
    
        // only match files (not folders)
        if (doc instanceof File) {
          // store all recognized files into an array
          var docName = doc.name;
          if (docName.match(extRE)) {
            fileArray.push(doc);
          }
        }
      }
    
      // return file array
      return fileArray;
    }