9 Replies Latest reply on Aug 7, 2012 8:24 PM by ocphotog

    Replace document name

    ocphotog

      How could I replace a open document name with a new name?

       

      I have a script that creates the new name but I can't figure out how to send the new name back to photoshop replacing the open document name.

       

      Here is the current script:

      *********************

      layerN = activeDocument.activeLayer.name

      var doc = app.activeDocument

      var fname = doc.name.split(".")

      var fname = fname[0]

       

       

      var newDoc = doc.duplicate(fname+layerN,true)

      //var newDoc = (fname+layerN,true)

       

       

      app.activeDocument.name = newDoc

      newDoc.flatten();

      ***************************

      Right now this one makes a duplicate of the original document with the document name like I want, but I would rather just rename the original document instead of making a duplicate.

       

      Any help is appreciated.

        • 1. Re: Replace document name
          c.pfaffenbichler Level 9

          Right now this one makes a duplicate of the original document with the document name like I want, but I would rather just rename the original document instead of making a duplicate.

          Why?

           

          In JavaScript »File« has a method »rename«.

          But if the file is currently open I doubt using that is a great idea.

          • 2. Re: Replace document name
            JJMack Most Valuable Participant

            Most likely if the document has an assoiated file you can save the current document using "save();"  to insure any updates get written out  then you can rename the associated file with the name you want. Close the current document and open the renamed document.  

             

            Some of Photoshop Document Properties objects are read only for example:

            fullName File Read-only. The full path name of the document.

            name string   Read-only. The document's name.

            path File        Read-only. The path to the document.

            • 3. Re: Replace document name
              ocphotog Level 1

              Here is what I'm trying to do just to clarify a bit better.

               

              1     Open Document

               

              2     Duplicate document via script to remove  COPY at end of filename.

               

              3     Close original document

               

              4.      Run action to make the duplicated copy B&W and change the background layer name to BW

               

              5.     Run script to change the name of  document to "Document Name + activeLayerName

               

              I get that far but I can't figure out how to return back to Photoshop with the documents replacement name. The only thing I can do is to make a new document with the new name but that's not what I need. Since the duplicated file hasn't been saved as yet I thought replacing the document name would be simple.

               

              Thanks for any help!

              • 4. Re: Replace document name
                JJMack Most Valuable Participant

                Because the document is a new document a that was created as a dup of some othere document there is no File assocated with this new document.  When you created the document  you could have named it anything you wanted.  Also because the is no file associated with this document I beleive that if you save the document with the name you want that Photoshop will basicly rename the document to match the file it is now associated with. I think this is something that Photoshop does when a document with no file assocation gets assocated with a file. For example if I open one of my Camera RAW file through ACR into Photoshop the name in the image window title will be RAW_FILENAME.CR2  while it looks like there is a file assocation the document does not have a file assocation for Photoshop can not write CR2 raw file.  The document just needed a name so Photoshop gave it the raw file fame. Now if I save that Document as xxx.tif Photoshop will save the xxx.tif file and change the document  name from RAW_FILENAME.CR2 to xxx.tif and you will see the in the image window title.  While the Document name object is read only to scripts its not read only to Photoshop when there is no file assocation. Once there is a file assocation the document name is set and is read only to Photoshop now too.  Even if the document had a lot of changes and I go to the history Palette and see the RAW_FILENAME.CR2 snap shot followed by the open and history. If I step back to the open the name remains xxx.tif.

                 

                So if there is no assocated file all you need do is to save the document to the filename you want the document to have and Photoshop will rename the document for you if the document truly had never been saved and has no file assocated with it.

                • 5. Re: Replace document name
                  ocphotog Level 1

                  Thanks for the info. I just can't find the correct coding to replace the document name that the currently open document has  (not the original document) with the new name that I create in the script? I would think it would some kind of REPLACE command.

                  • 6. Re: Replace document name
                    JJMack Most Valuable Participant

                    Your using Javascript just look in the Photoshop Javascript guide method saveas

                    saveAs (saveIn [, options] [, asCopy] [, extensionType])

                     

                    savein is the file your saving the document to the document should then be associated with that file and Photoshop should rename the document to match if the document had no file associated before.

                    • 7. Re: Replace document name
                      ocphotog Level 1

                      Thanks, but I do not want to save it right away in case I need to do more adjustments, I just want to replace the name . How can I do that in the script?

                      • 8. Re: Replace document name
                        JJMack Most Valuable Participant

                        As I wrote "When you created the document  you could have named it anything you wanted" Photoshop will generate a default name of "Original Doc Name Copy"  however you can override that name during the dupe operation if not with DOM code you can use scriptlistener code for the action manager. DOM code would be "duplicate ([name ] [, mergeLayersOnly] );"

                        • 9. Re: Replace document name
                          ocphotog Level 1

                          Thanks, I got it to do what I needed using your suggestions.