#!/usr/bin/perl

# show_app_creator_code
# This script displays the 4 character "creator code"
# for the application whose name is supplied as a command-line argument.
# Note that the name comparison is case-sensitive - i.e. you need to get
# the capitalization correct in the application name.
# Note also that you need to quote names that have spaces in them.
# It will also work if you supply only part of the name
# - if there is more than one app whose name matches what you supply,
#   then info on all matching apps will be shown.
# This script uses the Spotlight metadata to find the application.
#
# Cameron Hayne (macdev@hayne.net)  July 2006

use strict;
use warnings;
use File::Basename;

my $scriptName = basename($0);
if (scalar(@ARGV) < 1)
{
    print("usage: $scriptName appName\n");
    exit 1;
}

my $target = $ARGV[0];

# get a list of all applications (via Spotlight)
my @apps = `mdfind 'kMDItemContentTypeTree == "com.apple.application"c'`;
foreach my $appPath (@apps)
{
    chomp($appPath);
    next unless $appPath =~ /$target/;

    my $infoFile = "$appPath/Contents/Info.plist";
    my $appRsrc = "$appPath/..namedfork/rsrc";
    my $sigInfo = undef;

    if (-e $infoFile)
    {
        # Bundled apps will have an "Info.plist" file
        $sigInfo = `grep -A1 CFBundleSignature "$infoFile"`;
    }
    elsif (-e $appRsrc)
    {
        # Carbon apps will usually have the info in the app's resource fork
        $sigInfo = `strings "$appRsrc" | grep -A1 CFBundleSignature`;
    }

    next unless defined($sigInfo);
    #print "$sigInfo\n";

    my ($creatorCode) = $sigInfo =~ m{<string>(....)</string>};
    next unless defined($creatorCode);

    print "$appPath\n";
    print "CreatorCode: \"$creatorCode\"\n";
}

