2 Replies Latest reply: Aug 3, 2012 5:55 AM by Ghoulfool RSS

    Create an action that opens file with same name as current file

    rizla1991

      Apologies, I am a complete beginner at scripting, I am currently working with images that need to be colour corrected against a corresponding thumbnail image.

       

      My current file structure is two folders:

       

      Mannequin

      Image0001

      Image0002

      Image0003

      etc.

       

      Thumbnails

      Image0001

      Image0002

      Image0003

      etc.

       

      I need to set up an action which runs on the files in the Mannequin folder, that opens up the corresponding file with the same filename in the Thumbnails folder, so that it can use that files colour information with the Colour Match tool.

       

      I can record an action to open the Image0001 Thumbnail but then when I run the action for other files it will always open Image0001 thumbnail, which is not what I want.

       

      Is it possible to use a variable filename? Or can I use the current filename to determine the file I open from it?

       

      Hope this makes sense, thanks in advance.

        • 1. Re: Create an action that opens file with same name as current file
          Ghoulfool Community Member

          I think i follow you. It won't be possible as an action, but try this script:

          var dir1 = "Mannequin";
          var dir2 = "Thumbnails";
          
          // call the current document
          var tempDoc = app.activeDocument;
          
          // get the info out of the source doc and then close it
          var fileName = tempDoc.name;
          var filePath = tempDoc.path.toString();
          var fileNameAndPath = filePath + '/' + fileName;
          
          //alert(filePath);
          var docName = fileName.substring(0,fileName.length -4);
          
          //open other image
          var otherImagePath = filePath.substring(0, filePath.lastIndexOf(dir1));
          
          var tempFile  = decodeURI(otherImagePath + "/" + dir2 + "/" + fileName);
          var fileRef = new File(tempFile)
          
          app.open(fileRef);
          

           

          If you have image_001.jpg open from the mannequins directory, once run it will open image_001.jpg from the thumbnails directory. It's not very robust at the momen, but i'll work. If i have time I'll tidy it up.

          • 2. Re: Create an action that opens file with same name as current file
            Ghoulfool Community Member

            This is a bit more robust (it won't fall over if you have no images open or can't find the corresponding image) I've made it so if you have the thumbnail image open it will find the mannequin image and visa versa. Save the script to your PS scripts directory. In photoshop record an action that uses the script. Stop recording! Then you can run the script as an action.

             

            app.preferences.rulerUnits = Units.PIXELS;
            
            
            if (documents.length == 0)
            {
                alert ("No documents open, dude!")
            }
            else
            {
                var dir1 = "Mannequin";
                var dir2 = "Thumbnails";
            
                // call the current document
                var tempDoc = app.activeDocument;
            
                // get the info out of the source doc
                var fileName = tempDoc.name;
                var filePath = tempDoc.path.toString();
                var fileNameAndPath = filePath + '/' + fileName;
                var tempDir = filePath.substring(filePath.lastIndexOf("/") +1, filePath.length);
                var goCode = false;
                var docName = fileName.substring(0,fileName.length -4);
            
                if (tempDir == dir1)
                {
                    var otherImagePath = filePath.substring(0, filePath.lastIndexOf(dir1));
                    var tempFile  = decodeURI(otherImagePath + "/" + dir2 + "/" + fileName);
                    goCode = true;
                }
            
                if (tempDir == dir2)
                {
                    var otherImagePath = filePath.substring(0, filePath.lastIndexOf(dir2));
                    var tempFile  = decodeURI(otherImagePath + "/" + dir1 + "/" + fileName);
                    goCode = true;
                }
            
                if (tempDir != dir1 && tempDir != dir2)
                {
                    alert("Cannot find matching image.")
                    goCode = false;
                }
            
                if (goCode)
                {
                    //open the other image
                    var fileRef = new File(tempFile)
                    if (fileRef.exists)
                    {
                        app.open(fileRef);
                    }
                    else
                    {
                        alert("Matching file does not exist.")
                    }
                }
            
            }// end doc check
            

             

            Hope this helps.