Benches macOS

8

choose file name in Snow Leopard

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

In Leopard, when I run the StandardAdditions command *choose file name* and I click on an existing file listed in the resulting *Choose File Name* dialog, the +entire file name including file extension+ is placed into the *Save As:* text box, and that is what is returned when I click the Save button.

In Snow Leopard, when I do the same, only the file’s +base name+ is placed into the text box, and only the +base name+ is returned.

I need the Leopard behaviour.

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I need the Leopard behaviour.

    You're not likely to be able to change that.

    However,
    choose file name doesn't create the file. it just passes back to your script the value that was entered. It should be easy enough to append the required filename extension to that result, no?

  2. 0

    PeterSchoenrank

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It should be easy enough to append the required filename extension to that result, no?

    Having the script append the file extension is awkward for a number of reasons. Firstly, it is inconsistent with the behaviour users are used to. In 10.5 and earlier, the *choose file name* dialog mimics the behaviour of the standard *Save As* sheet that a user sees in any well-behaved OS X app; in 10.6 in no longer does. One of the reasons I prefer Mac OS X is for its UI consistency. If I give this final script to other users (which I might), I have to explain that the script doesn’t behave the way they are used to.

    Secondly, if I want to deploy the final script on a variety of 10.4/10.5/10.6 Macs (which is likely), I will have to either write different versions or write a significantly more complicated script. A single simpler script is easier to create correctly in the first place and easier to maintain afterward.

    Thirdly, in 10.5 and earlier the *choose file name* dialog checks for the existence of the chosen file. For example, the user navigates away from the default folder, clicks on a file in the list and then clicks
    Save; a warning sheet is displayed asking the user to confirm overwriting the existing file. As comparison, in 10.6, the user navigates away from the default folder, clicks on “foo.pdf”, and clicks
    Save; the *choose file name* dialog does not warn about overwriting because file “foo” does not exist. Now the script has to tack on “.pdf” and check for the existence of file “foo.pdf”, and if it exists, then display an alert separate from the choose file name dialog, and then do *choose file name* again. The warning is no longer directly associated with dialog which is confusing for the user and annoying to the user when she has to +do the navigation all over again+.

    Fourthly, now in 10.6, the script must second-guess the user. Did the choose file name dialog return file “foo” because the user clicked “foo.pdf” in the list, or because the user clicked “foo.txt” in the list, or because the user typed “foo”? It doesn’t apply in the particular script that I am currently working on, but there are situations where it might be reasonable for the user to be choosing between say “foo.doc” and “foo.docx” and in 10.6 the script can’t tell the difference. Even if I am sure that the extension that the user wants the file to have is “.blah”, did the choose file name dialog return file “foo.blah” because the user typed “foo.blah” or because the user clicked on “foo.blah.blah”? In the general case, it is extremely difficult to cover all the possibilities (maybe even impossible; I haven’t yet considered all the possibilities).

  3. 0

    hungryjoe

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Appending a filename to the returned result is rather trivial but the points that you make are quite valid as well. Perhaps you should file a bug report/feature request, however you see it at [http://bugreport.apple.com>.

  4. 0

    PeterSchoenrank

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    hungryjoe wrote:

    Appending a filename to the returned result is rather trivial

    Trivial? Consider, in Leopard and before:

    |
    set f *to choose file name*

    | +(* At this point, f is a file specifier and exactly one of the following is true:+

    | +• the file specified by f does not exist; or+

    | +• the user wants to overwrite the file.+

    | +And the file specified by f is exactly what the user chose. *)+

    That’s
    one line. Compare that to Snow Leopard:

    |
    property FileNameExtension : ".pdf"

    |

    |
    repeat

    |
    set f
    to +POSIX file+ ((POSIX path
    of ( *choose file name* )) & FileNameExtension)

    |
    try

    |
    alias f

    | *on error* number -1728 +-- can’t get reference; i.e. file does not exist+

    | *exit repeat*

    | *end try*

    |
    try

    |
    tell
    application "System Events" *to get the* name *of the* result

    | *display alert* "“" &
    the result & "” already exists. Do you want to replace it?" ¬

    | message "A file or folder with the same name already exists. " & ¬

    | "Replacing it will overwrite its current contents." ¬

    | as warning ¬

    | buttons {"Cancel", "Replace"} ¬

    | default button "Cancel" ¬

    | cancel button "Cancel"

    |
    if button returned *of the* result
    is "Replace" *then exit repeat*

    | *on error* number -128 +-- User cancelled+

    | +-- ask again+

    | *end try*

    | *end repeat*

    |

    | f

    | +(* At this point, f is a file specifier and exactly one of the following is true:+

    | +• the file specified by f does not exist; or+

    | +• the user wants to overwrite the file.+

    | +But the script has had to force a file name extension on the user,+

    | +so the file specified by f is not actually what the user chose. *)+

    You might do it differently as a matter of style, but you can’t do it simpler and you can’t do it shorter.

    (Rant: do you have any idea how much fiddling it took me to come up with

    |
    set f
    to +POSIX file+ ((POSIX path
    of ( …

    to work around the fact that AppleScript doesn’t do what the AppleScript Language Guide says on pages 43 and 88.)

    Perhaps you should file a bug report/feature request, however you see it at [
    http://bugreport.apple.com
    >.

    I’ll do that tomorrow.

  5. 0

    hungryjoe

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    hungryjoe wrote:

    Appending a filename to the returned result is rather trivial

    Oops! I guess it was too late in the night when I wrote that. I actually meant that adding a filename extension to the filename shouldn't be difficult. I wasn't talking about verifying that the file is actually a new one, but I should've mentioned that. Anyway, there is a minor flaw with your code.

    set f to POSIX file ((POSIX path of ( choose file name )) & FileNameExtension)

    If the user does enter the extension, you are adding it again without checking. You can do this instead:

    set f to POSIX path of (choose file name) as string
    if f does not end with FileNameExtension then set f to f & FileNameExtension
    set f to f as POSIX file

    Sorry that this is even more code.

  6. 0

    PeterSchoenrank

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    hungryjoe wrote:

    Anyway, there is a minor flaw with your code.

    Yes, this behaviour might not be what you or others want.

    But that is one of the points that I was trying to make above. *choose file name* does not respect what the user chose. There is no way, when *choose file name* returns “foo.blah”, to know whether the user typed “foo.blah” and really means “foo.blah” or the user clicked on “foo.blah.blah” and *choose file name* returned “foo.blah” instead.

    So if *choose file name* returns “foo.blah”, the script writer has to make a choice: add the extension or not. For certain scripts, adding the extension will be the
    right thing to do. In other circumstances, not adding the extension will be the
    right thing. In the general case, it is
    impossible for the script to
    always do the
    right thing.

    Sorry that this is even more code.

    Yes, even more code as compared to *one line* in previous versions of AppleScript.

  7. 0

    PeterSchoenrank

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    hungryjoe wrote:


    Perhaps you should file a bug report/feature request, however you see it at [
    http://bugreport.apple.com
    >.

    Done. Bug ID# 7306789.

  8. 0

    hungryjoe

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    it is impossible for the script to always do the right thing

    Very true. I suppose that this statement is true in more situations than not. Anyway, I hope that your bug report has the desirable outcome.