Created
March 22, 2014 16:28
-
-
Save skaji/9709862 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/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use feature qw{say}; | |
use subs qw{ heredoc }; | |
use Mac::iTunes; | |
use App::Daemon qw{daemonize}; | |
use Getopt::Long ( | |
':config', # configuration start | |
'no_auto_abbrev', # don't allow option names to be abbreviated | |
'no_ignore_case', # don't ignore case | |
); | |
die "only MacOS and die!\n" if $^O ne 'darwin'; | |
GetOptions("help|h" => sub { help() }) or help(); | |
local $App::Daemon::logfile = "/tmp/itunes_timer.log"; | |
local $App::Daemon::pidfile = "/tmp/itunes_timer.pid"; | |
my $sleep; | |
my $arg = $ARGV[0] || 60; | |
if ($arg =~ /\A [0-9.]+ \z/xsm) { | |
$sleep = $arg + 0; | |
say "iTunes will stop after $sleep minites."; | |
} | |
elsif ($arg !~ /\A (?: status | stop ) \z/xsm) { | |
warn "Unknown argument: $arg\n"; | |
help(); | |
} | |
daemonize(); | |
sleep int($sleep * 60); | |
Mac::iTunes->controller->pause; | |
sub help { | |
my ($script) = $0 =~ m{ ([^/]+) \z}xsm; | |
print STDERR heredoc qq{ | |
usage: | |
$script 30 # stop after 30 minites | |
$script stop # stop timer daemon | |
$script status # confirm status | |
}; | |
exit 1; | |
} | |
sub heredoc { | |
my $string = shift; | |
$string =~ s/\A [ \t]* \n | [ \t]* \z//gxsm; | |
if ($string =~ m/\A ([ \t]+) /xsm) { | |
my $padding = length $1; | |
$string =~ s/^ (.{$padding}) //gxsm; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment