Wrap cocoa in basic script?

Is it possible to wrap this cocoa snippet into a script? I don’t have 10.6 to develop on yet.
Not an application just a stand alone script if possible.


NSURL *src = [NSURL URLWithString:@"file:///Users/bjh/Desktop/temp.m"];
NSURL *dest = [NSURL URLWithString:@"file:///Users/bjh/Desktop/myalias"];

NSData *bookmarkData = [src bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile
                  includingResourceValuesForKeys:nil
                                   relativeToURL:nil
                                           error:NULL];
[NSURL writeBookmarkData:bookmarkData
                toURL:dest
              options:0
                error:NULL];

Create a foundation tool from your code and call that from AppleScript.

You could make it more useful by allowing src and dest to be provided by arguments

do shell script "/path/to/new_name " & "file:///Users/bjh/Desktop/temp.m" & space & "file:///Users/bjh/Desktop/myalias"

Keep in mind that the method bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:
is 10.6-only.

Shane, thanks! Wow that’s awesome. I must give you major props, you’re so knowledgeable on Scritpting and various other aspects. My coworkers think I’m smart, but compared to you, I feel sorta dumb sometimes. I truly thank you for your assistance and continued participation on this forum.

Running this in Terminal, how can I use the path with spaces? I tried it with single quotes and double quotes, with and without “file://”. I know in my Applescript I’m gonna have a path that has spaces in it. Every time I try with a path that has a space it never works (in Terminal).

–Edit: OK I think I see what’s up. I need to escape out spaces before it hits NSURL. Will try in 2 seconds.

Strings with spaces in the terminal have to be surrounded by quotes (or a backslash '' has to precede every space).

do shell script quoted form of "/path/to/new_name " & quoted form of "file:///Users/bjh/Desktop/temp.m" & space & quoted form of "file:///Users/bjh/Desktop/myalias"

Hope it helps,
ief2

EDIT:
Since POSIXes are more common in AppleScript, I suggest converting the POSIXes to URLs in the CLU:

Call it like this

set srcFile to POSIX path of (choose file with prompt "Source")
set destFile to POSIX path of (choose file with prompt "Destination")

do shell script (quoted form of "/path/to/new_name ") & quoted form of srcFile & space & quoted form of destFile

Hope it works,
ief2

I’m not dealing with any relative locations, it’s all full paths. I used:

NSURL *src = [NSURL URLWithString:[argvOne stringByAddingPercentEscapesUsingEncoding:4]];

I was using quoted form in Terminal and that wasn’t working, but the above modification does work.
Thanks!

Don’t thank me, thank Craig – he posted it…

hm oh silly me! Thanks Craig!!! You are totally awesome too!:smiley: