Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A Perl script to manage file types and creators' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A Perl script to manage file types and creators
Authored by: dierauer on Dec 05, '03 01:18:04PM
Here's my version of this script, made more readable by using the standard perl module Getopt::Std to process command-line switches.

#!/usr/bin/perl
use strict; #-- strict and warnings are always a good idea!
use warnings;
use Getopt::Std; #-- standard perl module

#-- written by David Dierauer (dierauer at NOSPAMvoyager.net) 05-DEC-2003
#-- based on the 'tc' perl script by 'anonymous' posted to macosxhints

my $usage = "usage: $0 [-t TYPE][-c creator] filenames\n"
   . "\tif type and creator are present then they will be set for the files;\n"
   . "\totherwise the type and creator of the files are displayed\n"; 

our ( $opt_c, $opt_t );
getopts( 'c:t:' );

foreach my $opt ( $opt_t, $opt_c )
{
   if ( $opt and length($opt) != 4 )
   {
      die "'$opt' does not have length 4; try using quotes if you need spaces.\n";
   }
}
my @files = @ARGV;
@files or die $usage;

my $devtools = '/Developer/Tools';
foreach my $file ( @files )
{
   `$devtools/SetFile -t '$opt_t' '$file'` if $opt_t;
   `$devtools/SetFile -c '$opt_c' '$file'` if $opt_c;
   
   my $newtype = `$devtools/GetFileInfo -t '$file'`;
   chomp( $newtype );
   
   my $newcreator = `$devtools/GetFileInfo -c '$file'`;
   chomp( $newcreator );
   
   print "$newtype   $newcreator  $file\n";
}


[ Reply to This | # ]
A Perl script to manage file types and creators
Authored by: clarkcb on Dec 05, '03 02:01:59PM

Unless Getopt::Std handles this behind the scenes, it looks like one thing is missing from these scripts: passing the file arguments to the glob function to get a list of files from arguments like '*.txt':


@files = ();
foreach $arg (@ARGV) {
    push @files, glob($arg);
}


[ Reply to This | # ]
A Perl script to manage file types and creators
Authored by: dierauer on Dec 05, '03 02:14:14PM

I don't understand why you'd need to do this; for something like '*.txt', the shell will pass the list of files to perl.

Please give more details.



[ Reply to This | # ]
A Perl script to manage file types and creators
Authored by: clarkcb on Dec 05, '03 05:55:55PM

Oops, you're right, sorry. I don't recall why now, but I used to have to do this. Anyway, please disregard previous post.



[ Reply to This | # ]