Skip navigation
Currently Being Moderated

File extensions on Mac

Nov 16, 2011 10:43 AM

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

 

 
Replies
  • Currently Being Moderated
    Nov 17, 2011 3:56 AM   in reply to [Ariel]

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

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 17, 2011 4:31 AM   in reply to Harbs.

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 17, 2011 6:04 AM   in reply to [Ariel]

    The file object has a built-in rename function…

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 17, 2011 7:27 AM   in reply to [Ariel]

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 17, 2011 8:26 AM   in reply to [Ariel]

    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?

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Nov 19, 2011 4:02 PM   in reply to [Ariel]

    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?

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    Nov 20, 2011 6:44 AM   in reply to [Ariel]

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

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 22, 2011 6:54 AM   in reply to [Ariel]

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 6, 2013 10:17 PM   in reply to [Ariel]

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 6, 2013 10:18 PM   in reply to Trevorׅ

    See above edit

     

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

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 7, 2013 8:59 AM   in reply to Trevorׅ

    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;
                }
          }
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 20, 2013 4:48 PM   in reply to Trevorׅ

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 20, 2013 4:52 PM   in reply to Trevorׅ

    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);

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)