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

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 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.

If a path ends with a colon, then it wasn’t a file but a folder.
so just test if the last text item is empty.

I’m not saying it isn’t. Any system that’s robust and reliable is a good one. That doesn’t imply that it’s the only good one, nor even the best one. If you’re familiar with AppleScript’s history, then you’ll know how and why this particular practice came about.

Most people in this forum probably eat sugar.

I don’t imagine anyone’s aim is to end up with more errors in their code. That approach isn’t necessary in order to produce error-free code, but if you find it works well for you, that’s good and I’m not suggesting you need to change. Besides, If one has a system that’s consistent and reliable, it’s probably good to stick with that as re-habituating oneself can be a painful process. So, yes, keep doing that if it helps you.

One would hope that this would be part of it, at the very least.

Yes, ideally. That’s usually what I try to advocate and demonstrate, which is all I’m doing here.

It’s not always about something being “good” or “bad”. Every problem has more than one solution, and, for the most part, each solution will have its merits and drawbacks. There’s rarely one perfect, universal solution to anything,

@robertfern’s method for obtaining the file name from its path is a common one, not just within the AppleScript sphere. It’s a naive solution, which sounds like an insult, but it’s an actual term used to refer to a method of achieving something that, by and large, should work fine, and often represents the most intuitive approach that one arrives at first, but it won’t generally contain any optimisations, and may—as is the case here—not cover certain scenarios that may be desirable to have covered in the general case.

As you pointed out, writing code that will be error-free is an important goal, and to do this, it’s crucial to be able to identify situations in which one’s code might not be error-free. This doesn’t mean the code is unusable or bad, and, indeed, a lot of professional software will include a documented list of “known bugs”.

Even if a piece of code appears to work fine, it’s important to be able to consider, find, and identify how and when errors might or will arise (even theoretical ones). It’s not necessary to consider every single conceivable scenario, especially if they’re so obscure they’re unlikely to happen. But, at a minimum, for a piece of code that performs a minor (non-critical) function, one should be thinking about the common scenarios that might lead to failure; and for any code that the rest of a script will depend on in order to function properly, it’s obviously more essential to broaden the scope and include more edge cases that, by themselves, might not be likely, but acknowledging that, over time, even uncommon things will probably happen at some point.

As I said, @robertfern’s method of obtaining the file name from its path is a common one. In bash scripting, for example, "${filepath##*/}" essentially uses the same method, by returning everything after the last occurrence of a slash. In a shell script, it’s a perfectly reasonable method to use, as there’s only one notation that’s used consistently to represent all file paths, where a slash always delimits directory levels and a standardised file path won’t ever end with a slash, even if the path points to a folder. AppleScript is aware of two different kinds of file path. I won’t go into the situation involving colons appearing in posix paths again, as you can read about that above; however, the situation where a colon appears at the end of an HFS file path was one @robertfern hadn’t considered, because he didn’t think about what constitutes a file in AppleScript terms versus what constitutes a folder:

This isn’t true, because MacOS has a notion of something it calls a package file which, similar to folders, act as containers in which other files and folders (and package files) are stored, but are treated and presented by the system (and by AppleScript) as a seemingly-regular file. Thus, the command choose file, which doesn’t allow one to select a folder, does allow one to select package files. A standardised HFS path pointing to a package file will have a terminating colon. @robertfern’s method will therefore incorrectly returns the file name of any of the following types of file: applications, script bundles, rich text bundles, Automator workflows, and basically tons of other file types that we are used to regarding as ordinary files, but aren’t.

1 Like