20 Replies Latest reply: May 20, 2013 4:52 PM by Trevorׅ RSS

    File extensions on Mac

    [Ariel] Community Member

      Hi there,

       

      Using a line like this on a Windows machine:

       

           myFile= File.saveDialog("Save Settings", "Super script extension:

      *.sse");

       

      this makes sure that whatever name the user chooses for their file, the

      correct .ccpp extension will be added.

       

      From the documentation, it looks like the Mac will ignore the second

      string in the brackets.

       

      If this is so, how would you open save dialog on a Mac and make sure the

      file gets saved with the correct extension?

       

      Many thanks,

      Ariel

       

        • 1. Re: File extensions on Mac
          [Ariel] Community Member

          Ok, a combination of emailing the post and bad editing made it a challenge to understand the question, so here goes again:

           

          I'm trying to open a "Save As" dialog so the user can choose a folder and type a file name, and the script will create a file with a bunch of information.

           

          The extension for this script-created file should be, say, .sse (an extension I've made up).

           

          Now, on Windows, you can use the following line in your script:

           

          myFile = File.saveDialog("Save Settings", "Super script extension:*.sse")

           

          That makes sure that whatever name the user types, the Save As dialog will automatically append the correct .sse extension.

           

          However, from the Adobe Scripting Guide documentation, it looks like the Mac ignores the "Supoer script extension:*.sse" part.

           

          So the question is: (1) On a Mac, how do you make sure that the file name the user creates in the saveDialog will have the correct extension?

           

          Thanks for any help or pointers in the right direction,

          Ariel

          • 2. Re: File extensions on Mac
            Harbs. MVP

            On Mac, you can type in any extension you want, so there's not much in terms of foolproofing the extension...

             

            Harbs

            • 3. Re: File extensions on Mac
              [Jongware] MVP

              What about examining the returned name and adjusting it if necessary?

              • 4. Re: File extensions on Mac
                [Ariel] Community Member

                Thanks Harbs.

                 

                Jongware, I tried doing that, but it seems to me that once the user clicks ok in the save dialog, the file is created, so then changing the name means deleting the file and creating a new one -- obviously fairly easily doable, but it seems a bit roundabout compared to the Windows mechanism.

                 

                Unless there's some Javascript way of renaming a file without deleting it? I couldn't find that.

                 

                Ariel

                 

                Message was edited by: Arïel (came in blank via email)

                • 5. Re: File extensions on Mac
                  Muppet Mark Community Member

                  The file object has a built-in rename function…

                  • 6. Re: File extensions on Mac
                    [Ariel] Community Member

                    Hi MuppetMark,

                     

                    Thanks. I saw that. But as far as I could see, it only works with

                    aliases (known as shortcuts on Windows). I couldn't get it to rename an

                    actual file, only an alias (or shortcut) to a file. This is what the OMV

                    says:

                     

                    File.rename (newName: string ): Boolean

                    Core JavaScript Classes

                    Renames the associated file.

                    Does not resolve aliases, but renames the referenced alias or shortcut

                    file itself. Returns true if the file was successfully renamed.

                    newName: Data Type: string

                    The new file name, with no path information.

                    Example:

                    aFile.rename(newName)

                     

                    Have I misunderstood? I did play around with it yesterday and couldn't

                    get it to work, but maybe I did something wrong...

                     

                    Thanks,

                    Ariel

                    • 7. Re: File extensions on Mac
                      absqua Community Member

                      I had no problem with File.rename() on the Mac. Did you see that the parameter should be

                      The new file name, with no path information.

                       

                      To help someone save a file with the right extension, I usually start with a File object in order to pre-polulate the save name and location.

                       

                      var xmlFile = File("~/Desktop/new file.xml").saveDlg("Save the xml file.");
                      

                       

                      This will pop up a save dialog with only the name, and not the extension, highlighted. I think that's as foolproof as you're going to get. I did try it with your "sse" extension, and it acted weird. If the system doesn't recognize the extension as belonging to a UTI with an application that owns it, it won't act right. (The UTI stuff is, to me, pretty complicated and mysterious, and I've given up trying to figure it out on more than one occasion.)

                       

                      Jeff

                      • 8. Re: File extensions on Mac
                        [Ariel] Community Member

                        Thanks Jeff, this is very helpful. I was wondering how you choose where

                        the saveDialog open up at -- and now I know!

                         

                        Another question: If the user chooses to overwrite an existing file,

                        it's customary to alert them to confirm that they want to overwrite the

                        file.

                         

                        The way I've done that is that once they click Save on the save dialog,

                        I check if (theFile.exists), in which case I alert them (alert("Do you

                        want to overwrite").

                         

                        The problem with doing it like that is that the Save dialog has to close

                        first, and then the alert message pops up.

                         

                        This is not so standard. Usually the overwrite alert pops up with the

                        save dialog in the background, and if the user clicks no, they get

                        straight back to the filename field in the save dialog.

                         

                        Is there any way of doing that with ExtendScript?

                         

                        Thanks,

                        Ariel

                        • 9. Re: File extensions on Mac
                          absqua Community Member

                          On the Mac the system will handle the overwrite confirmation for you as soon as you click "Save", and the dialog doesn't return until you confirm. Adding your own File.exists check would be redundant. Does Windows not do this?

                          • 10. Re: File extensions on Mac
                            [Ariel] Community Member

                            Hi Jeff,

                             

                            I would have expected Windows to do the same, but it doesn't seem to ...

                            unless there's some setting I'm missing?

                             

                            So does that mean that the script has to check whether it's on a mac

                            first? Otherwise the user might get an "Overwrite file" alert twice...

                             

                            Incidentally, I'm using File.saveDialog. I get an error if I try to use

                            File.saveDlg (which is mentioned in the documentation). Mayeb the

                            overwrite alert behaves differently with saveDlg.

                             

                            Thanks,

                            Ariel

                            • 11. Re: File extensions on Mac
                              John Hawkinson Community Member

                              Incidentally, I'm using File.saveDialog. I get an error if I try to use

                              File.saveDlg (which is mentioned in the documentation). Mayeb the

                              overwrite alert behaves differently with saveDlg.

                              I was just bit by this so it's fresh. File.saveDialog() is a class method of the File class. But saveDlg() is a method of any instance of the File class -- you call it on an existing file.

                              So for instance:

                               

                              var file1 = File.saveDialog();
                              var file2 = file1.saveDlg();
                              var file3 = (new Folder("~/Desktop")).saveDlg();
                              

                               

                              Helpful?

                              • 13. Re: File extensions on Mac
                                John Hawkinson Community Member

                                Empty post again, Ariel. The email interface apparently does not agree with you. I recommend plain text emails with only ASCII characters...

                                • 14. Re: File extensions on Mac
                                  [Ariel] Community Member

                                  Sorry, this is tedious for you and for me.

                                   

                                  I'm pretty sure the problem of blank email happens because I'm using

                                  Thunderbird. Hitting reply includes the previous message as a quote at

                                  the bottom. The message is included as a table. If I delete the quoted

                                  message (which 99% I do) the table still remains. And that causes a

                                  blank email. And I'm forgetting to delete the blank table (possibly

                                  because it's not visible).

                                   

                                  Anyway, what I wanted to say was:

                                   

                                  Yes, very helpful. Thank you. (And I think Jeff used it correctly

                                  earlier in the thread, but I ignored it since it wasn't working.)

                                   

                                  And it solves the "File Exists Overwrite?" problem. SaveDlg checks if a

                                  file exists and asks the user. SaveDialog doesn't.

                                   

                                  Many thanks,

                                  Ariel

                                  • 15. Re: File extensions on Mac
                                    absqua Community Member

                                    I get (on the Mac) an overwrite confirmation from the system whether I'm using saveDlg() from a File instance or File.saveDialog(). It doesn't make sense to me that it would differ; it's the same system dialog in either case, just starting from a different place.

                                    • 16. Re: File extensions on Mac
                                      Trevorׅ Community Member

                                      Hi Ariel

                                       

                                      I wanted to send you an email so I did a search for you on the forum and this is what came up

                                       

                                      A bit old and I haven't read the posts that have been replied but If you want to save a file type on a Mac you can use this function I made up for a script I wrote for someone who has a Mac.

                                       

                                      Message was edited by: ~ Trevor ~ I removed the code because it was buggy If any got it they shouldn't run it.

                                      • 17. Re: File extensions on Mac
                                        Trevorׅ Community Member

                                        See above edit

                                         

                                        I'll post the code whe (if) I debug it.

                                        • 18. Re: File extensions on Mac
                                          Trevorׅ Community Member

                                          Ok this is the fixed script

                                           

                                          Haven't tested it on a Mac (Sponsers welcome ) but to me it looks like it will work with a file filter in the same way the save dialog works on windows

                                           

                                          Please give either positive or otherwise feedback on whether or not it works on a Mac.

                                           

                                          Trevor

                                           

                                          myNewFile =  SaveDialog ("Name of New (Filtered) Swatch .ase File to Save" // The file save prompt --- Change as desired
                                                                                        ,"Adobe swatch exchange files:*.ase" // The filter --- Change as desired
                                                                                        , true) // OPTIONAL if true give warn on overwrite, defalt is FALSE 
                                           if (myNewFile)
                                              {
                                              myNewFile.open('w');
                                              myNewFile.close();
                                              }
                                          
                                          function SaveDialog (myFilePromt, /* The file promt" */ Preset /* The desired suffix */, warning /* OPTIONAL if true give warn on overwrite, defalt is FALSE */)
                                                     /* function by Trevor to apply a file filter for a Mac http://forums.adobe.com/message/4036767#4036767  */                  
                                              {
                                                  var myFile = File.openDialog (myFilePromt, [Preset]);
                                                  if (myFile)
                                                      {
                                                          var myFileName = myFile.name;
                                                          var suffix = Preset.match(/[^.]*$/);
                                                          var regSuffix = eval("/\." + suffix + "$/i");
                                                          var regBaseName = /^[^.]*/;
                                                          if (! regSuffix.test(myFileName)) myFile = File (myFile.path + "/" + myFileName += "." + suffix);        
                                                          if (warning && myFile.exists) if (!confirm ("The file " + myFile.name + " exists, do want to overwrite it? ", false, "Comfirm Save")) SaveDialog (myFilePromt, [Preset], warning);
                                                          return myFile;
                                                      }
                                                }
                                          
                                          • 19. Re: File extensions on Mac
                                            Trevorׅ Community Member

                                            I noticed that someone placed the above script on their site - Great fame at last!

                                            I also noticed that it was not well written to say the least

                                             

                                            But I think this is well written and should work nicely.

                                             

                                            But I didn't get many donations for my "buy me a Mac" campaign so I still haven't been able to test it.

                                             

                                            So if there's any nice kind blokes out there, && you the reader are one of them && you have a Mac please test the script and let me know if it works.

                                             

                                            Thanks in advance,

                                             

                                            Trevor

                                             

                                            P.s. I actually think this script could well be useful.

                                             

                                             

                                            // Function by Trevor to apply a file filter to a "SaveDialog" for a Mac http://forums.adobe.com/message/4036767#4036767   
                                            // On Windows the regular saveDialog is shown however if 'warning' is set to true then a confirm prompt is given that the file exists
                                            
                                            function SaveDialog (myFilePromt /* The file promt" */ 
                                                                            ,Preset /* The desired suffix */
                                                                            , warning /* OPTIONAL if true warn on overwrite, defalt is FALSE */)
                                                {
                                            
                                                    var   suffix = Preset.match(/[^.]*$/),
                                                            regSuffix = new RegExp("\\." + suffix + "$", "i"),
                                                            myFile = ($.os.match(/Mac/i))  ?  File.openDialog (myFilePromt, function (f) { return (f instanceof Folder) || f.name.match(regSuffix);}) // if Mac
                                                                          : File.saveDialog (myFilePromt, [Preset]); // else
                                                    if (myFile)
                                                        {
                                                            var myFileName = myFile.name;
                                                            if (! regSuffix.test(myFileName)) myFile = File (myFile.path + "/" + myFileName += "." + suffix);
                                                            if (myFile.exists)
                                                                {
                                                                    if ($.os.match(/Mac/i) && myFile.readonly) {alert (myFile.name + "\rThis file is set to read-only.\rTry again with a different file name."); SaveDialog (myFilePromt, Preset, warning);};
                                                                    else if (warning) if (!confirm ("The file " + myFile.name + " exists, do want to overwrite it? ", false, "Comfirm Save")) SaveDialog (myFilePromt, Preset, warning);
                                                                 }
                                                            return myFile;
                                                        }
                                                  }
                                            
                                            ///////////////////////////////////////////////////////////////////
                                            ////                      Sample usage                        ////
                                            /////////////////////////////////////////////////////////////////
                                            
                                            
                                            myNewFile =  SaveDialog ("Name of JavaScript .jsx File to Save" // The file save prompt --- Change as desired
                                                                                   ,"JavaScript Files:*.jsx" // The filter --- Change as desired
                                                                                   , true) // OPTIONAL if true give warn on overwrite, default is FALSE 
                                            
                                            if (myNewFile) // save file if cancel was not pressed
                                                {
                                                myNewFile.open('w');
                                                myNewFile.write('alert("Thanks Trevor!")'); // change as desired if you insist :-(
                                                myNewFile.close();
                                                }  
                                            

                                             

                                            Made an important fix up

                                            • 20. Re: File extensions on Mac
                                              Trevorׅ Community Member

                                              Some how one of the lines was majorly messed up, don't know how that happened.

                                              Either way I fixed it up so before you test the script on the email make sure this line is correct and not as originally posted or received by mail

                                              if (! regSuffix.test(myFileName)) myFile = File (myFile.path + "/" + myFileName += "." + suffix);