Get filename from "choose file name"

I’ve been working on a script lately, which involves saving a file, so I want to use choose file name. However, I need the name I entered in the dialog. How would I do this?

so if the output of the dialog was Mac HD:Users:User:Desktop:File.doc, how could I just get File.doc?

choose file name returns a file URL.
As the file doesn’t exist (yet), name of (info for .) or the Finder can’t help.

You can do it with text item delimiters


set fileURL to choose file name
set {TID, text item delimiters} to {text item delimiters, ":"}
set fileName to last text item of (fileURL as text)
set text item delimiters to TID
fileName

that don’t work for me

It does for me :frowning:

Hello.

I’t wouldn’t work if you used it on a directory or an app, Stefan made it under the assumptions of what the OP did want.

If you need a “catch all” you would need something like:


on baseName(hfsFileOrFolderPathAsText)
    -- whether it is a file, a folder,application or bundle.
    local tids, theFile, lastItem
    set hfsFileOrFolderPathAsText to "" & hfsFileOrFolderPathAsText -- doesn't harm.
    set lastItem to -1
    if hfsFileOrFolderPathAsText ends with ":" then set lastItem to -2
    set {TID, text item delimiters} to {text item delimiters, ":"}
    set fileName to text item lastItem of (hfsFileOrFolderPathAsText as text)
    set text item delimiters to TID
    return fileName
end baseName