Wednesday, November 20, 2019

#1 2019-06-20 12:18:02 pm

chrisndeca
Member
From:: Oakland, CA
Registered: 2005-06-28
Posts: 24

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:
https://macscripter.net/viewtopic.php?id=40224

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:

Applescript:

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:

Applescript:

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

Applescript:

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:

Applescript:

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


Filed under: droplet, File reference

Offline

 

#2 2019-06-24 09:09:52 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 101

Re: How to specify a new file that ASObjC will accept.

There are a few options.  You could change:

Applescript:

jpegData's writeToURL:theOutput atomically:true

to:

Applescript:

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:

Applescript:

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:

Applescript:

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

where I declare NSURL and NSString beforehand:

Applescript:

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.

Offline

 

#3 2019-06-24 10:21:53 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6037

Re: How to specify a new file that ASObjC will accept.

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

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2019-06-24 10:25:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6037

Re: How to specify a new file that ASObjC will accept.

CK wrote:

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

Applescript:

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



You can simplify that a bit:

Applescript:

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.

Last edited by Shane Stanley (2019-06-24 10:25:33 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-06-25 12:44:57 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 101

Re: How to specify a new file that ASObjC will accept.

Shane Stanley wrote:

You can simplify that a bit:

Applescript:

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)