This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

tasker web interface

By: a guest on Dec 29th, 2012  |  syntax: Perl  |  size: 1.96 KB  |  views: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env perl
  2.  
  3. use Getopt::Std;
  4.  
  5. sub help {
  6.   my $error = shift;
  7.   print "Error: $error\n\n" if($error);
  8.   print <<HELP;
  9. Usage: $0 <params>
  10. -f           Don't fork and stay in foreground
  11. -h           This help message
  12. -P <port>    TCP port to listen on. Always listens on all available IPv4 interfaces. Defaults to tcp/8080
  13.  
  14. HELP
  15. exit(1) if($error);
  16. };
  17.  
  18. our($opt_f, $opt_h, $opt_P);
  19. $opt_P = 8080;
  20. getopts('fhP:') or (help() and exit(1));
  21.  
  22. if($opt_h) { help(); exit(0); };
  23. help('Port must be numeric') unless($opt_P =~ m/^\d+$/);
  24.  
  25. my $OSASCRIPT = '/usr/bin/osascript';
  26.  
  27. {
  28. package TaskerWebServer;
  29.  
  30. use HTTP::Server::Simple::CGI;
  31. use base qw(HTTP::Server::Simple::CGI);
  32. use strict;
  33. use warnings;
  34.  
  35. my %dispatch = (
  36.     '/' => \&mainPage,
  37.     '/cmd1' => \&cmd1,
  38.     '/cmd2' => \&cmd2,
  39. );
  40.  
  41. sub handle_request {
  42.   my $self = shift;
  43.   my $cgi  = shift;
  44.   my $path = $cgi->path_info();
  45.   my $handler = $dispatch{$path};
  46.  
  47.   if (ref($handler) eq "CODE") {
  48.     print "HTTP/1.0 200 OK\r\n";
  49.     $handler->($cgi);
  50.   } else {
  51.     print "HTTP/1.0 404 Not found\r\n";
  52.     print $cgi->header,
  53.           $cgi->start_html('Not found'),
  54.           $cgi->h1('Not found'),
  55.           $cgi->end_html;
  56.   };
  57. };
  58.  
  59. sub helper {
  60.   my $cgi = shift;
  61.   my $message = shift;
  62.   my $html =  $cgi->header .
  63.               $cgi->start_html('Tasker interface');
  64.   $html .= $cgi->h1($message) if($message);
  65.   $html .= $cgi->end_html;
  66.   return $html;
  67. };
  68.  
  69. sub mainPage {
  70.   my $cgi = shift;
  71.   print helper($cgi, 'Tasker interface');
  72. };
  73.        
  74. sub cmd1 {
  75.   my $cgi = shift;
  76.   system("$OSASCRIPT ~/my.first.applescript");
  77.   print helper($cgi, 'Ran cmd1');
  78. };
  79.  
  80. sub cmd2 {
  81.   my $cgi = shift;
  82.   system("$OSASCRIPT ~/my.second.applescript");
  83.   print helper($cgi, 'Ran cmd1');
  84. };
  85.  
  86. };
  87.  
  88. if($opt_f) {
  89.   TaskerWebServer->new($opt_P)->run();
  90. } else {
  91.   my $pid = TaskerWebServer->new($opt_P)->background();
  92.   print "Started tasker web service with PID $pid\n";
  93. };
clone this paste RAW Paste Data