9 Replies Latest reply on Dec 20, 2008 3:31 PM by Fred Goldman

    [JS] CS3 file.openDialog ()

    Fred Goldman Level 3
      What folder does file.openDialog default to? I can't seem to find any
      consistency in this. And is there any way to control it?
        • 1. Re: [JS] CS3 file.openDialog ()
          Level 1
          Use myFile.openDlg() where myFile is a file object in the folder you want it to start at.

          Dave
          • 2. Re: [JS] CS3 file.openDialog ()
            Fred Goldman Level 3
            Yeah, I just read about that in the JS Tools Guide. The problem is I am
            sending this script to someone else it and the folder he will want it to
            default to will be changing as the project he works on changes.

            Ideally I could tell him do "this and that" to change the default
            without having to teach him how to edit a script. But I can't figure out
            how the script decides where the default folder should be.

            If all else fails I will just send him an updated script every time he
            changes projects.
            • 3. Re: [JS] CS3 file.openDialog ()
              Level 1
              Have him open a folder; construct a file reference from that -- there does actually need to be a file; a file object is all that's needed.

              Dave
              • 4. Re: [JS] CS3 file.openDialog ()
                Fred Goldman Level 3
                Right, but when the project changes to a different folder would I not
                have to send him an updated script with an updated file reference?

                Of course, that wouldn't be the end of the world, but I was hoping to
                avoid that step.
                • 5. Re: [JS] CS3 file.openDialog ()
                  Level 1
                  Write the file object to a file, something like this:

                  var prefsFile = new File( Folder.userData.absoluteURI + "/myScript/baseFile.jsx" );
                  // create the parent folder just to be sure
                  prefsFile.parent.create();

                  if ( prefsFile.open( "r" ) ) { // then the file exists and is openable
                  var file = eval( prefsFile.read() );
                  prefsFile.close();
                  var file = file.openDlg( "Pick a file, Bozo" );
                  } else {
                  var file = File.openDialog( "Pick a file, Bozo" );
                  }
                  if ( file ) {
                  prefsFile.open( "w" );
                  prefsFile.write( file.toSource() );
                  prefsFile.close();
                  }

                  I do this all the time (and I mean ALL the time). I actually use a standard preferences object that stores name-value pairs for persistent data. That object is passed a file on instantiation and can load and save itself and any number of name-value pairs that I want saved. It even stores and recreates objects that are stored as values.

                  Bob
                  • 6. Re: [JS] CS3 file.openDialog ()
                    Kasyan Servetsky Level 5
                    Hi guys,

                    Let me put in my two cents' worth:


                    var myPath = app.extractLabel("RememberedPath");
                    if (myPath != "") {
                       // open the folder that was open last
                       var myLastFile = new File(myPath);
                       // check if the folder exists
                       if (myLastFile.parent.exists) {
                          openFile();
                       }
                       else {
                       // open a default folder
                       var myLastFile = new File("~/Desktop/");
                       openFile();
                       }
                    }
                    else {
                       // open a default folder
                       var myLastFile = new File("~/Desktop/");
                       openFile();
                    }

                    function openFile() {
                       var myFile = myLastFile.openDlg("Open a File");
                       if (myFile != null) {
                          app.insertLabel("RememberedPath", myFile.absoluteURI);
                          // do something
                       }
                    }

                    Kasyan
                    • 7. Re: [JS] CS3 file.openDialog ()
                      dougneale Level 1
                      Forget the technicalities for a moment and remember that we are
                      producing software for non-programmers most of the time.

                      It is important to remember that the 'MyDocuments' folder approach was
                      created in order to give a degree of familiarity to Joe Public when
                      using his new computer.

                      I spend more time helping people to find something they have 'lost'
                      because the new fangled 'preferences' approach often results in files
                      being stored in some very non-obvious places.

                      IMHO our open and save dialogs should always point to the 'MyDocuments'
                      folder as the first place to look or store files that will be needed in
                      the future.

                      Just my 2p worth

                      Doug Neale
                      • 8. Re: [JS] CS3 file.openDialog ()
                        Kasyan Servetsky Level 5
                        Hi Doug,

                        To set as default ‘Desktop’, ‘My Documents’ or some other folder is a matter of personal liking. I personally prefer to work with my files on Desktop and put them away, when finished.
                        But this is not so important.
                        Just replacing
                        var myLastFile = new File("~/Desktop/");
                        with
                        var myLastFile = Folder.myDocuments;
                        will make ‘My Documents’ to open as default.

                        But the problem is different:

                        >The problem is I am sending this script to someone else it and the folder he will want it to
                        default to will be changing as the project he works on changes.

                        As far as I understand, the user wants the script to ‘remember’ the last opened folder.
                        And I posted the snippet to show my approach – using application’s insertLabel and extractLabel methods. I think it has an advantage: no need to create an additional file to store preferences.

                        We used to have the same problem at work, because we keep our files on a network drive and it is very annoying to walk through several subfolders each time you want to open/place a file. We solved this by using ‘Default Folder’.

                        Kasyan
                        • 9. Re: [JS] CS3 file.openDialog ()
                          Fred Goldman Level 3
                          Bob,

                          I see what you mean and this could be useful for a lot of things.

                          But Kasyan's answer was really what I was really looking for. I never
                          realized you could insert a label into the app. That is really a great
                          trick!