> I'm trying to understand how to use the Mac::Files NewAlias
> function. I have not found any code examples and I'm struggling.
>
> The NewAlias function creates a filehandle. How do I create an
> alias file from the alias filehandle?
I'm not surprised: Chris Nandor provided a nicely detailed run
through of this process some time back (Feb 2003, it seems). It is,
let's say, "pretty grotty", with more emphasis on the grotty than on
the pretty!
Have a look at
http://use.perl.org/~pudge/journal/10437
Just for fun I tried it out locally, and unfortunately it seems to
lack a certain something on my 10.4 machine: that being the "alias
bit", which isn't set on the newly created file. Flicking that on
(using, say, Path Finder) shows that the file does have the right
pointer to the original file, and the creator and type are being set
correctly, but isn't recognised/represented as an alias in the Finder.
Anyway, I hope this helps gets you pointed in a direction not
entirely opposite to where you need to be!
Cheers,
Paul
Chris Nandor - 07 Nov 2006 03:22 GMT
> http://use.perl.org/~pudge/journal/10437
Thanks Paul. I knew I had this code somewhere, but couldn't find it. :-)
Here's a slightly cleaned-up version.
#!/usr/bin/perl
use warnings;
use strict;
use MacPerl qw(GetFileInfo);
use Mac::AppleEvents qw(typeAlias);
use Mac::Errors;
use Mac::Files;
use Mac::Resources;
my $target = '/Users/pudge/Desktop/foo';
my $alias = '/Users/pudge/Desktop/foo alias';
# get target's creator, type, and alias
my($creator, $type) = GetFileInfo($target);
my $alis = NewAlias($target) or die $Mac::Errors::MacError;
# make resource file, open it, add the resource, and close it
FSpCreateResFile($alias, $creator, $type, 0) or die $Mac::Errors::MacError;
my $res = FSpOpenResFile($alias, 0) or die $Mac::Errors::MacError;
AddResource($alis, typeAlias, 0, '') or die $Mac::Errors::MacError;
CloseResFile($res);
# set "alias" attribute
my $finfo = FSpGetFInfo($alias) or die $Mac::Errors::MacError;
$finfo->fdFlags( $finfo->fdFlags | kIsAlias );
FSpSetFInfo($alias, $finfo) or die $Mac::Errors::MacError;

Signature
Chris Nandor pudge@pobox.com http://pudge.net/
Open Source Technology Group pudge@ostg.com http://ostg.com/
Laurence Haynes - 11 Jan 2007 18:20 GMT
Thanks for the code example. This was very helpful.
This seems to work. However, there is a small problem. In Finder the
alias appears as a file alias not a dir alias . The alias functions
correctly but does not look right.
> From: Chris Nandor <pudge@pobox.com>
> Date: November 6, 2006 7:22:39 PM PST
[quoted text clipped - 41 lines]
> FSpSetFInfo($alias, $finfo) or die
> $Mac::Errors::MacError;
Regards,
Laurence Haynes
Chris Nandor - 19 Jan 2007 03:25 GMT
> Thanks for the code example. This was very helpful.
>
> This seems to work. However, there is a small problem. In Finder the
> alias appears as a file alias not a dir alias . The alias functions
> correctly but does not look right.
Yeah, this is a problem with the Finder in general, not specific to this
code: basically, the Finder can get out of sync with what the file should
actually look like.
There's a "Nudge" contextual menu item that you can use (free download), but
from your code, you can also do:
use Mac::Glue;
my $finder = new Mac::Glue 'Finder';
$finder->obj(file => $alias)->update;
Cheers,

Signature
Chris Nandor pudge@pobox.com http://pudge.net/
Open Source Technology Group pudge@ostg.com http://ostg.com/