Skip to content

Instantly share code, notes, and snippets.

@frodwith
Created March 30, 2012 01:38
Show Gist options
  • Save frodwith/2245615 to your computer and use it in GitHub Desktop.
Save frodwith/2245615 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
my %keys = ( esc => 53, tab => 48, space => 49);
my %mt = (m => 'command', 'o' => 'option', c => 'control', s => 'shift');
my @mods = @ARGV;
my $key = pop @mods;
@mods = grep {defined} map { $mt{$_} } @mods;
my $mawd = '';
if (@mods) {
$mawd = join ', ', map { "$_ down" } @mods;
$mawd = "using {$mawd}";
}
my $special = $keys{$key};
my $stroke = $special ? "key code $special" : qq'keystroke "$key"';
my $script = <<"SCRIPT";
tell application "System Events"
$stroke $mawd
end tell
SCRIPT
print $script;
open my $pipe, '|-', 'osascript';
print {$pipe} $script;
close $pipe;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment