Re: Creating alias programatically
Re: Creating alias programatically
- Subject: Re: Creating alias programatically
- From: chaitanya pandit <email@hidden>
- Date: Wed, 24 Sep 2008 03:27:59 +0530
On 24-Sep-08, at 2:22 AM, Nick Zitzmann wrote:
On Sep 23, 2008, at 2:48 PM, chaitanya pandit wrote:
I tried using:
OSErr FSNewAliasFromPath ( const char *fromFilePath, const char
*targetPath, OptionBits flags, AliasHandle *inAlias, Boolean
*isDirectory );
But i don't quite get how should i use the fromFilePath parameter,
say i want to create an alias for "Users/me/Documents/foo" at
"Users/me/Desktop/foo"
should i pass the first path as "fromFilePath"?
Yes. If you have the paths as NSStrings, then use -
fileSystemRepresentation (not -UTF8String and definitely not -
cStringUsingEncoding:) to get a path string.
Here is what i did:
// target path is the destination the alias would be pointing (in my
case it is /Desktop/foobar)
// aliaspath is the path where the alias file would be created (in my
case it is /Desktop/foobarvalias)
+ (void)createAliasforTargetPath:(NSString *)targetPath aliasPath:
(NSString *)aliasPath
{
AliasHandle aliasHandle;
OSErr err = FSNewAliasFromPath([targetPath fileSystemRepresentation],
[targetPath fileSystemRepresentation], 0, &aliasHandle, NULL);
if(err != noErr)
NSLog(@"Error creating alias handler");
else
{
int fd = open([aliasPath fileSystemRepresentation], O_WRONLY |
O_CREAT |
O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO);
if(fd == -1)
perror("store-open");
else
{
HLock((Handle)aliasHandle);
long handleSize = GetHandleSize((Handle)aliasHandle);
int wrote = write(fd, &handleSize, sizeof(long));
wrote = write(fd, *aliasHandle, handleSize);
close(fd);
HUnlock((Handle)aliasHandle);
printf("store-wrote file\n");
}
}
}
It created an executable file with name foobar alias on desktop but
when i try to open it it says
"chaitanya-pandits-macbook:~ chaitanya$ /Users/chaitanya/Desktop/foobar
\ alias ; exit;
-bash: /Users/chaitanya/Desktop/foobar alias: cannot execute binary file
logout
[Process completed]"
Any idea what i'm doing wrong?
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden