App-SlowQuitApps
view release on metacpan or search on metacpan
lib/App/SlowQuitApps.pm view on Meta::CPAN
# Export API...
sub import {
for my $subname (qw< delay slowquit fastquit >) {
no strict 'refs';
*{caller().'::'.$subname} = \&{$subname};
}
}
# Set delay...
sub delay {
my ($delay) = @_;
# Convert fractional seconds to integral milliseconds...
$DELAY = max(1, int($delay * 1000));
}
# Add an application to the blacklist (i.e. will be slowquitted)...
sub slowquit {
my ($app) = @_;
my $app_id = _get_id($app);
if (!$app_id || $app_id !~ /\S/) {
my (undef, undef, $line) = caller;
warn "Can't slowquit application '$app' at line $line (application not found)\n";
}
else {
$SLOWQUIT{$app} = $app_id;
}
}
# Add an application to the whitelist (i.e. will NOT be slowquitted)...
sub fastquit {
my ($app) = @_;
my $app_id = _get_id($app);
if (!$app_id || $app_id !~ /\S/) {
my (undef, undef, $line) = caller;
warn "Can't fastquit application '$app' at line $line (application not found)\n";
}
else {
$FASTQUIT{$app} = $app_id;
}
}
# This takes the information gathered, configures the system _defaults, and restarts the app...
sub _configure {
# Does the config make sense???
if (keys(%SLOWQUIT) && keys(%FASTQUIT)) {
warn "Can't configure both slowquit and fastquit on apps.\nConfiguration unchanged.\n";
}
else {
# Clear everything...
_defaults("delete $APP_ID");
# Set up delay...
if ($DELAY) {
_defaults("write $APP_ID delay -int $DELAY");
say "Slowquit after $DELAY msec";
}
# Set up whitelist or blacklist...
if (keys %SLOWQUIT) {
_defaults("write $APP_ID invertList -bool YES");
for my $app (keys %SLOWQUIT) {
_defaults("write $APP_ID whitelist -array-add $SLOWQUIT{$app}");
say "Slowquit: $app";
}
}
elsif (keys %FASTQUIT) {
_defaults("write $APP_ID invertList -bool NO");
for my $app (keys %FASTQUIT) {
_defaults("write $APP_ID whitelist -array-add $FASTQUIT{$app}");
say "Fastquit: $app";
}
}
# Restart to effect changes...
system("killall $APP_NAME");
system("open -a $APP_NAME");
}
}
# Given an app name, get the corresponding identifier...
sub _get_id {
my ($app) = @_;
my $id = `osascript -so -e 'try' -e 'id of app "$app"' -e 'end try'`;
chomp $id;
return $id;
}
# Interface to system defaults utility (with minor cleanup on failure)...
sub _defaults {
my ($command) = @_;
my $result = `defaults $command >& /dev/null`;
chomp $result;
return if $result =~ m{does not exist\s*\Z};
return $result;
}
1; # Magic true value required at end of module
__END__
=head1 NAME
App::SlowQuitApps - Simplify configuration of SlowQuitApps app on MacOS
=head1 VERSION
This document describes App::SlowQuitApps version 0.000003
=head1 SYNOPSIS
use App::SlowQuitApps;
# Set slowness of quit (in seconds)...
delay 1.25;
# Make only these apps slow-quit...
slowquit 'Microsoft PowerPoint';
slowquit 'Terminal';
slowquit 'Firefox';
# Or else...make all apps slow-quit EXCEPT these...
fastquit 'Calendar';
fastquit 'Safari';
fastquit 'Notes';
( run in 0.533 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )