How to make a file reference to a non-existing file from a folder ref

I seem to be struggling with the simplest things in AS.

According to the AS docs from Apple, a “file” type can point to a non-existing file. Yet, I can’t get it working the way I want (I am using 10.10.5 in case that matters):

This works:

set f to POSIX file "/Users/xxx/Desktop/nonexisting"
get f --> file "DQ1:Users:xxx:Desktop:nonexisting"

But this does not:

set f to file "DQ1:Users:xxx:Desktop:nonexisting"

Why would the result that I get from the first example not work when I repeat it exactly?

Supposing that the old file specifier is somehow broken on 10.10 and only “POSIX file” works, then how do I do this:

I start with a folder reference such as this:

tell application "Finder"
	set dir to item "Desktop" of home
end tell

Now I like to create a file (or POSIX file) reference for a file “nonexisting” in that desktop folder. How? I’ve tried to coerce the folder to text and then append “/nonexisting” to it, and conver that back into a posix file, but I only get errors no matter what I try. Why is this so friggin difficult!

Also, if someone gives a good answer, how about I ask the same on StackOverflow, which is much better suited for Q&A like this, would you be willing to reply there, too?

set f to POSIX file "/Users/xxx/Desktop/nonexisting"
get f --> file "DQ1:Users:xxx:Desktop:nonexisting"
class of f --> «class furl»

So, use the same class when you start from an HFS path.

set f to "DQ1:Users:xxx:Desktop:nonexisting" as «class furl»
--> file "SSD 500:DQ1:Users:xxx:Desktop:nonexisting"

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 22 avril 2016 14:58:31

Thanks, “as «class furl»” works, but that’s really sick.

Why isn’t this type even known with a proper type name? AppleScript is in a really sorry state, isn’t it?

BTW, I found a better way to refer to the desktop folder:

set f to ((path to desktop folder) & "nonexisting") as string as «class furl»

«class furl» is more an AppleEvent type (typeFileURL to be excactly) rather than AppleScript type. For better supportibility «class furl» is resolved into an file specifier on return. It’s what I (ab)use when returning so called file specifiers from the AppleScript Toolbox command which are in fact «class furl» objects. Not all AppleEvent types are included in AppleScript (like «class utf8» for utf8 encoded strings).

I apologize but I can’t answer to the question : “Why . ?”

I gave the shorter syntax but, if you dislike «class furl» - which is available for several years, you may use this alternate syntax :

set f to POSIX file (POSIX path of "DQ1:Users:xxx:Desktop:nonexisting")
--> file "SSD 500:DQ1:Users:xxx:Desktop:nonexisting"

Don’t worry, I’m aware of path to desktop folder syntax which I use for years, but here, as the goal was to give a syntax for a non existant item I felt useless to build an existing pathname :wink:

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 22 avril 2016 15:38:37

Thanks, Yvan, for showing me “POSIX path” - I had not realized that was available as well. Now I found it in the Apple docs, too. I had overlooked that because it was only mentioned under “alias”, which I was not interested in for this specific issue.

Fair enough.

But I blame Apple for breaking the “file” specifier. I am right that it used to work and now doesn’t any more, right? The docs at https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW28 suggest that file “path:to:file” should just work.

I am still upset that such a simple and surely often-needed thing is so hard to figure out.

Not exactly. It works in some contexts, such as:

read file "path:to:file"

and within some app tell blocks.

But it has never worked as a standalone "set x to file “path:to…”.

That’s a question of the individual workflow.

Personally I’m using file only for the read/write command of Standard Additions.

In all other cases I’m using alias for existing and HFS string paths for non-existing files

Log bugs. One of the problems, I think, is that most of the people who find problems are scripters who don’t use bugreporter.

Right. I did report it (see https://openradar.appspot.com/25876138) but now I think my points were not even valid. I am so confused.

But what if the command you’re using the non-existing path with expects a file (as does my scriptable app, incidentally, that’s how I ended up with this problem - I wanted to pass a file to a save command that expects a “file” specifier), when I can’t even construct such a file easily first?

I now have learned some ways, but they were all not obvious to me. I mean, if the save command wants a file, why shouldn’t I be able to first save that file ref in a variable before I pass it to the save command?

Like this works:

save ... in file "Disk:xx"

But this does not:

set ff to file "DQ2:raba"
save ... in ff

I find that very confusing.

In fact,

file "path:to:file"

and

read file "path:to:file"

behave exactly the same way.

Both compile and both fail if the given path doesn’t point to an existing item.

What is annoying is that in: https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW28
we may read :
Use a file object to refer to a file that does not yet exist.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 22 avril 2016 16:22:56

Please forget the client side of your scriptable app.

The file type in the sdef file is not the same as the file specifier in the terminology.

The sdef file type is bridged to NSURL and usually can take a HFS string path or an alias and “ if the developer added a few lines “ even a POSIX path on the AppleScript side.

I forgot to comment that.
Look at what we get if we split your instruction in its components.

set f to ((path to desktop folder) & "nonexisting")
--> {alias "SSD 500:Users:userName:Desktop:", "nonexisting"} # Yes, it returns an awful list of two items of different class
set f to f as string
--> "SSD 500:Users:userName:Desktop:nonexisting"
set f to f as «class furl»
--> file "SSD 500:Users:userName:Desktop:nonexisting"

We may easily get rid of the awful list.

set f to (path to desktop folder as string) & "nonexisting"
--> "SSD 500:Users:userName:Desktop:nonexisting" # Yes, this time, no awful list but a clean string
set f to f as «class furl»
--> file "SSD 500:Users:userName:Desktop:nonexisting"
# So, if we concatenate the components in a single instruction :
set f to (path to desktop folder as string) & "nonexisting" as «class furl»
--> file "SSD 500:Users:userName:Desktop:nonexisting"

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) vendredi 22 avril 2016 20:41:23