I needed a cross-platform message box for Windows and Mac. I tried wxperl because its native on the mac but their is a bug with their MessageBox code on the mac.
This is pretty simple code, but uses each O/S’s native message box API to display a dialog. If you have Yes/No buttons and click yes it will perform the item in action. Which for us opens a webpage. You only need a standard perl installation for this code.
$message = “My Popup Message”;
#icons Types (stop, note, caution)
$icon = ‘note’;
#button types (ok, yesno)
$buttons = ‘yesno’;
#action
$action = ‘http://www.google.com’;
if ($^O eq “darwin”)
{
#mac
&macPopup;
}
elsif($^O eq “MSWin32”)
{
#windows
&winPopup;
}
else
{
print “Unknown OS”;
exit;
}
sub winPopup
{
if($icon eq ‘stop’)
{
$ic = ’16’;
}
elsif($icon eq ‘note’)
{
$ic = ’32’;
}
elsif($icon eq ‘caution’)
{
$ic = ’48’;
}
if($buttons eq ‘ok’)
{
$bu = ‘0’;
}
elsif($buttons eq ‘yesno’)
{
$bu = ‘4’;
}
#work around because win32 is not on a mac, but the mac will throw an error
eval{require Win32; };
$result = Win32::MsgBox($message, $ic + $bu);
if(($result == 6) && ($buttons eq ‘yesno’))
{ #do action
system(‘start ‘ . $action);
}
}
sub macPopup
{
if($icon eq ‘stop’)
{
$ic = ‘0’;
}
elsif($icon eq ‘note’)
{
$ic = ‘1’;
}
elsif($icon eq ‘caution’)
{
$ic = ‘2’;
}
if($buttons eq ‘ok’)
{
$bu = ‘{“OK”}’;
}
elsif($buttons eq ‘yesno’)
{
$bu = ‘{“YES”,”NO”}’;
}
#mac way
$result = `/usr/bin/osascript <<-EOF
tell application "System Events"
activate
display dialog "$message" buttons $bu with icon $ic
end tell
EOF`;
chomp($result);
if (($buttons eq 'yesno') && ($result eq 'button returned:YES'))
{
system('open ' . $action);
}
}