Converting alias to file

How can I convert an alias object to a file object?
When I use a dialog box to specify a file, AS returns an alias object, but when trying to get the file name or doing a number of other things (such as attaching it to an email) AS demands a file object. (and then there is that weird POSIX file object as well, which everyone writes about, and I have no idea what it would be used for and why?)

So, I am trying:

set importFile to choose file with prompt "Please choose the import file:"
set fileName to name of importFile

–can’t get name of importFile (because it’s an alias)
So I try

set importFile to choose file with prompt "Please choose the import file:"
set theFile to file (path of importFile)
set fileName to name of theFile

no luck (same with “set theFile to importFile as file”)

eventually, since everyone uses POSIX file, when I look at the forum:

set importFile to choose file with prompt "Please choose the import file:"
set theFile to POSIX file (POSIX path of importFile)
set myFile to theFile as file
set fileName to name of myFile

No. POSIX files also don’t seem to have names.

How can I just get the alias as a file object, since all the following code requires a file object. Driving me nuts!

Thanks

Try this. It will be an HFS path

set importFile to choose file with prompt "Please choose the import file:"
set fileName to importFile as text

if you want a POSIX path try this.

set importFile to choose file with prompt "Please choose the import file:"
set thefile to POSIX file (POSIX path of importFile)
set fileName to thefile as text
1 Like

Robert has answered your question, but I thought I would add a few comments.

The simplest method to get a file object from an alias object is as follows:

set aliasObject to (choose file) -- returns an alias
set fileObject to aliasObject as «class furl» -- returns a file object

You can also get a file object from an alias object with POSIX file, but it requires a POSIX path. For example:

set aliasObject to (choose file) -- returns an alias
set posixPath to POSIX path of aliasObject -- returns a POSIX path
set fileObject to POSIX file posixPath -- returns a file object

If your goal is to get the name of a file, it should be noted that name is not a property of either an alias or file object, and that’s why you are getting an error message. There are many ways to get the name of a file from an alias or file object, and the following is just one example:

set aliasObject to (choose file) -- returns an alias
tell application "Finder"
	set fileName to name of aliasObject
end tell

BTW, you can often use an alias object where you can use a file object, so the script will determine what you use where.

1 Like

To get the filename you can also do this…

set importFile to choose file with prompt "Please choose the import file:"
set fileName to importFile as text
set text item delimiters to ":"
set fileName to last text item of fileName
1 Like

Thank you all. I am setting this to the solution, because I really needed a file object, in addition to the file name–for later steps in the script.

I still do not quite understand why this command works

set fileObject to aliasObject as «class furl»

but this does not:

set fileObject to aliasObject as file

I assume «class furl» specifies the class of a file object. Why wouldn’t the simpler term work? (I thought AppleScriupt is all about simplicity and using plain english terms)

I’m not sure why as «class furl» works. I’ve never seen it in the documentation and picked it up somewhere along the way. Perhaps another forum member will know.

An AppleScript file is a type that was defined a long time ago, and is no longer useful. A «class furl» is a not strictly a file object, but a light-weight object with no terminology that can used in place of one.

2 Likes

No, they just tried to deal with changes to the underlying OS without breaking older scriptable apps. The original file object had serious limitations – for one, it could only refer to files that already exist.

That’s “file”, not “file object”. FWIW, when you see that in a Cocoa app, it should always handle a furl.

This is not advisable. If the path were to end with a colon, this will return an empty string. And if the path were to be a posix path (which wouldn’t be the case here, but it’s easy to conceive such situations), the colon might be part of the filename.

Two other approaches.

info for is deprecated since macOS 10.4 but still works pretty well and takes an AppleScript alias as argument

set importFile to choose file with prompt "Please choose the import file:"
set fileName to name of (info for importFile)

or just ask System Events for the name

set importFile to choose file with prompt "Please choose the import file:"
tell application "System Events" to set fileName to name of importFile

Both snippets do actually the same because info for uses System Events under the hood.

info for provides a lot more information for the file, please see

set fileInfo to (info for importFile)
2 Likes

@markusbohu

set importFile to POSIX path of (choose file with prompt "Please choose the import file:")
set fileName to do shell script "basename " & quoted form of importFile
1 Like

You should never put a colon in a filename. Or / or \ for that matter. Nothing but pain

Also just tested. Finder won’t let me put a colon in a filename on Monterey
So not an issue

Colon could never be used in a file name on Mac.

However, what @CJK meant, is a colon in a POSIX path. Which stands for a “/” in a file name.

macOS allows users to use “/” in file names. (It should never be done - but it’s allowed. I assume this was originally permitted on OS X for compatibility with the “classic” Mac OS, where users already created countless files with “/” in their names as “/” wasn’t a path separator. That’s just my guess.)

So if you use a “/” in a file name, it’s actually a colon in the POSIX path.

For example, a display file name in the Finder:

My file name / with slash.pdf

in the POSIX path is actually:

My file name : with slash.pdf

No, what @CJK meant, is a colon in a HFS path, which Apple uses to delineate folders.
Which was why I was politely correcting him as to why you can’t have a colon in said filename for this very reason.
Which is also why it is a good way to get to the filename using text item delimiters.

@CJK was talking about POSIX path (as an unlikely but potentially possible situation), here’s the exact quote:

But my example was using the standard default HFS paths, and someone complained about me using a colon, because that might conflict with a file name. that’s where all this started

I think @robertfern approach is not complete. Here is my approach.

set importFile to choose file with prompt "Please choose the import file:"
if class of importFile is alias then
	set fileName to importFile as text
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set fileName to last text item of fileName
	set AppleScript's text item delimiters to ASTID
end if

set a to text items of "ab:cd"

I agree with you Fredirik71. I was just being lazy on the saving of text item delimiters. I usually do it your way too.

Provided one gets into the habit of ensuring text item delimiters are always set immediately prior to any joining of list items or splitting of strings, then it’s not necessary to reset them and @robertfern’s can be regarded as complete.

The bigger issue remains using them for this purpose at all, which was kindly elucidated by @leo_r on my behalf. As a method in the general case, it’s too fallible. I also highlighted a case-specific example that would result in failure, which @robertfern might have been too distracted with interpreting the rest of what I wrote to fully register it, namely:

This is not the only problematic scenario, it was just the most obvious that I used to indicate that there will be problems. This one also happens to be an extremely common scenario.


@StefanK has signposted the only truly robust means of decomposing a file path, which is to use validated APIs designed for this purpose.

As an academic exercise where we are restricted to making use of only the built-in AppleScript language tools, and string manipulation, then, assuming an absolute path, all of the aforementioned pitfalls highlighted thus far can be avoided by stripping from the beginning of the given path the entirety of its container’s absolute path. What remains will be the file or folder name of the given path, possibly with a terminating sigil (which might be desirable, or, if not, can be removed if done thoughtfully enough to ensure it is a sigil being removed rather than a character appearing at the end of the file name). While this method maximises robustness, there may still be exceptional cases that would need to be determined.

1 Like

I generally agree with CJK but with a reservation. Many people with only a limited understanding of AppleScripts get scripts and, more importantly, portions of scripts from this site. Some of those individuals have no or only a limited understanding of text item delimiters and how they can impact the operation of their scripts. So, resetting text item delimiters to their existing value seems worthwhile in some circumstances, and robertfern’s script suggestion seems one of those circumstances.