Created
October 8, 2009 21:10
-
-
Save amrox/205427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# cribbed from the iTunes 9 installer, with some modifications | |
# TODO: localize (somehow) | |
################################################################################################### | |
my $SYSTEM_VERS = "/System/Library/CoreServices/SystemVersion.plist"; | |
my $EXIT_VALUE = 0; | |
################################################################################################### | |
DO_CHECKS: { | |
# quit with error for system earlier than 10.5 | |
if(CheckVersion("$SYSTEM_VERS", "10.5.0", "ProductVersion", "<")) { | |
$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 16); | |
last; | |
} | |
# block while the application is running | |
while (CheckRunning("com.apple.Finder")) | |
{ | |
if (DisplayAlert("Please unplug your Neat scanner before continuing.") == -1) { | |
# the user cancelled | |
$EXIT_VALUE = ((1 << 6) | ( 1 << 5 ) | 17); | |
last; | |
} | |
} | |
} | |
exit($EXIT_VALUE); | |
################## | |
sub CheckRunning | |
{ | |
my $bundleIdentifier = $_[0]; | |
my $count = `osascript -e 'tell app "System Events" to count processes whose bundle identifier is "$bundleIdentifier"' 2> /dev/null`; | |
if ($count > 0) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
sub DisplayAlert | |
{ | |
my $message = $_[0]; | |
my $script = <<"END"; | |
tell application "Installer" | |
-- activate | |
try | |
set dd to display dialog "$message" buttons {"Cancel", "Continue"} default button 2 with icon caution | |
set response to button returned of dd | |
if response = "Continue" | |
return 0 | |
end if | |
end try | |
return -1 | |
end tell | |
END | |
return `osascript -e '$script'`; | |
} | |
# verbatim from iTunes 9 installer | |
sub CheckVersion | |
{ | |
my $path = $_[0]; | |
my $version = $_[1]; | |
my $keyName = $_[2]; | |
my $operator = $_[3]; | |
if (! -e $path) { | |
return 0; | |
} | |
if (!$operator) { | |
$operator = "=="; | |
} | |
my $oldSeperator = $/; | |
$/ = \0; | |
open( PLIST, "$path") || do { | |
return 0; | |
}; | |
$plistData = <PLIST>; | |
$plistData =~ /<dict>(.*?)<\/dict>/gis; | |
@items = split(/<key>/, $plistData); | |
shift @items; | |
foreach $item (@items) { | |
$item =~ /(.*?)<\/key>.*?<string>(.*?)<\/string>/gis; | |
$versiondata{ $1 } = $2; | |
} | |
close(PLIST); | |
$/ = $oldSeperator; | |
@theVersionArray = split(/\./, $versiondata{$keyName}); | |
for ($i = 0; $i < 3; $i++) { | |
if(!$theVersionArray[$i]) { | |
$theVersionArray[$i] = '0'; | |
} | |
} | |
@versionArray = split(/\./, $version); | |
my $actualVersion; | |
for ($i = 0; $i < 3; $i++) { | |
if (($theVersionArray[$i] != $versionArray[$i]) or ($i == 2)) { | |
$actualVersion = $theVersionArray[$i]; | |
$version = $versionArray[$i]; | |
last; | |
} | |
} | |
my $expression = '$actualVersion ' . $operator . ' $version'; | |
if( eval ($expression) ) | |
{ | |
return 1; | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment