Skeeve •---->


Anmeldedatum: 20.04.2006 Beiträge: 1067
|
Verfasst am: 03.02.2012 - 23:18 Titel: Just for the fun of it: StandardAdditions in perl nutzen |
|
|
iScript fragte mich gestern im chat, wie man von perl aus einen "display dialog" aufrufen könne.
Mir kam dann die Idee, den AppleScript StandardAdditions eine Perl Schnittstelle zu verpassen. Sozusagen AppleScript in Perl programmieren
Ich habe hier mal den Anfang gemacht:
Code: | package StandardAdditions;
use Exporter;
our @ISA =qw( Exporter );
our @EXPORT=qw( display_dialog );
use Carp;
my $OSASCRIPT= '/usr/bin/osascript';
my %display_dialog=(
default_answer => \&text, # the default editable text
hidden_answer => \&boolean, # Should editable text be displayed as bullets? (default is false)
buttons => \&list_of_text, # a list of up to three button names
default_button => \&text_or_integer, # the name or number of the default button
cancel_button => \&text_or_integer, # the name or number of the cancel button
with_title => \&text, # the dialog window title
with_icon => \&with_icon, # the resource name or ID of the icon to display…
# stop/note/caution] : …or one of these system icons…
giving_up_after => \&integer, # number of seconds to wait before automatically dismissing the dialog
);
sub display_dialog {
my ($text, $param)= @_;
my $params='';
while (my($p, $v)= each %$param) {
my $fkt= $display_dialog{$p};
croak "Unknown parameter: $p\n" unless $fkt;
$p=~tr/_/ /;
$params.= ' '.$p.' '.$fkt->($v);
}
my @commands= (
'tell application "Finder"',
'activate',
'display dialog ' . double_quoted_form_of($text).$params,
'end'
);
open my $osascript, '-|', $OSASCRIPT, map { ('-e', $_) } @commands;
my @result= <$osascript>;
close $osascript;
return wantarray ? @result : join '', @result;
}
sub with_icon {
return $_[0] if $_[0]=~ /^(?:\d+|stop|note|cution)$/;
return text($_[0]);
}
sub text {
return double_quoted_form_of($_[0]);
}
sub integer {
return $_[0];
}
sub text_or_integer {
return $_[0]=~ /^\d+$/ ? $_[0] : text($_[0]);
}
sub unquoted_text {
return $_[0];
}
sub boolean {
return $_[0] ? 'true' : 'false';
}
sub list_of_text {
return '{' . join (', ', map { double_quoted_form_of($_) } @{$_[0]}).'}';
}
sub double_quoted_form_of {
local($_)= shift;
s/"/\\"/g;
return "\x22$_\x22";
}
1; |
Aufgerufen werden kann das z.B. so:
Code: | #!/usr/bin/perl
use strict;
use warnings;
use lib qw( . );
use StandardAdditions;
print display_dialog("BlaBla", {
default_answer => 'BlaBlubb',
buttons => ['Laber', 'Schwall'],
default_button => 'Laber',
with_title => 'Laber Rhababer',
with_icon => 'stop',
giving_up_after => 10,
});
|
_________________ "All problems are solved in slightly less than half an hour" (Chumbawamba, "Hey Hey We're The Junkies") |
|
Skeeve •---->


Anmeldedatum: 20.04.2006 Beiträge: 1067
|
Verfasst am: 12.02.2012 - 23:27 Titel: |
|
|
Wolle-77 hat Folgendes geschrieben: | Ketzerisch könnte ich jetzt bemerken, daß mich das ein wenig hieran erinnert:
|
Aber nur ganz entfernt. Zudem kannte ich das gar nicht.
Wolle-77 hat Folgendes geschrieben: | Aber sicherlich gibt es einen plausiblen Grund, sowas umzusetzen.
|
Klar: Damit können perl scripte, auch ohne pashua (oder wie das auch immer heißt) Dialogboxen nutzen. Nicht sehr performant, das gebe ich gerne zu, aber immerhin geht es.
Wolle-77 hat Folgendes geschrieben: | In was für Chats bist Du und iScript denn manchmal so? |
Keine Ahnung. Ich nutze Adium und ich glaube, das war ICQ. _________________ "All problems are solved in slightly less than half an hour" (Chumbawamba, "Hey Hey We're The Junkies") |
|