How to specify a new file that ASObjC will accept.

Hi All,
Shane helpfully rewrote a script that I had asked about yesterday here in this post:

Now I am wanting to take his idea, and turn it into a droplet. The problem I am having is replicating the file reference that is created with the following line:

set theOutput to (choose file name default name "Untitled.jpg")

Since I am working on a droplet, I don’t want to poll the user for a new name for every image, I just want to add "_RGB to the end of the file name. So the the new “theOutput” string would look something like this:

set newOutput to "MyComputer:Users:Chris:Downloads:TestImage_RGB"
set theOutput to (a reference to file newOutput)

Now when I try to reference that as an Alias, of course, I get an error since there is no actual file there yet. So I have tried converting that string into a Posix File and “a reference to file” and neither of them works when I send it gets to the last line of Shanes Script

jpegData's writeToURL:theOutput atomically:true

I get the following error: “-[NSAppleEventDescriptor isFileURL]: unrecognized selector sent to instance 0x614001236f00”

So I know what I am doing wrong. I am not passing the correct “file” specification to that last line of the script. I just don’t know how to create the same file reference that is being created when you prompt the user with:

choose file name default name

Can anyone point out what I am doing incorrectly here?

Also SD sees the two file references differently. When you use “choose file name default name” the info for that variable in SD shows file “Filepath”. But the variable created with the “a reference to file” has a little blank file icon in the value for the file reference and then says file “Filepath”. So that shows me that I have so far not found the right way to specify the file since SD displays the values for those variables differently.

Here is an example of that difference

There are a few options. You could change:

jpegData's writeToURL:theOutput atomically:true


jpegData's writeToPath:theOutput atomically:true

and then theOutput can be a simple posix path in string form.

But, to get the right file reference for an Objective-C method expecting an NSURL file reference, you can use «class furl» to point to a file path that doesn’t yet exist:

set newOutput to "MyComputer:Users:Chris:Downloads:TestImage_RGB"
set theOutput to newOutput as «class furl»

The path you coerce to «class furl» should be fully formed, but can be either HFS-style (colon-delimited) or posix-style (slash-delimited).

Then, of course, you could switch into AppleScriptObjC since you’ll be doing so anyway, and construct the NSURL object that way:

set theOutput to NSURL's fileURLWithPath:((NSString's ¬
		stringWithString:(POSIX path of newOutput))'s ¬

where I declare NSURL and NSString beforehand:

property NSURL : a reference to NSURL of the current application
property NSString : a reference to NSString of the current application

but inserting the current application reference at the point of use is perfectly fine.

CK has pointed you in the right direction with «class furl», but let me add that if you run this:

set theOutput to (choose file name default name "Untitled.jpg")
class of theOutput

you will see that the resulting file is also «class furl». Both file references and furls appear as file when logged, which is more than a tad confusing.

You can simplify that a bit:

set theOutput to (current application's NSURL's fileURLWithPath:(POSIX path of newOutput))'s URLByStandardizingPath()

But if your paths are the result of manipulation file objects, standardizing is probably unnecessary.

Oops. I’m so used to writing a certain thing, my fingers just do it. Thanks for spotting that.