Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / January 2007



Tip: Looking for answers? Try searching our database.

Code Examples for NewAlias

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Laurence Haynes - 03 Nov 2006 20:11 GMT
Greetings,

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?

#!/usr/bin/perl -wl

use File::Basename;
use Mac::Files;

my $dir = "/tmp/directory";
my $alias = NewAlias($dir);
my $alias_path = ResolveAlias($alias);

print basename($alias_path);
print $alias_path;

Do I need to use the function FSpCreate?

Regards,
Laurence Haynes
Paul McCann - 04 Nov 2006 13:17 GMT
> 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/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.