forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappkey
executable file
·106 lines (88 loc) · 2.08 KB
/
appkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl
#
# send keys to an application to remotely control it (macOS-only currently)
# -samy kamkar | 2021/01/18 | https://samy.pl
# Example:
#
# appkey Logic2 ctrl r # starts/stops recording in Saleae Logic
# appkey -g cmd c # system copy
use strict;
use Getopt::Long qw/GetOptions/;
my $OSASCRIPT = "osascript"; # path to osascript
my %opts = (
"cmd" => "command",
"command" => "command",
"ctrl" => "control",
"control" => "control",
"opt" => "option",
"option" => "option",
"shift" => "shift",
);
# get options
my %conf = opts();
# send keys
tx();
#################################
sub opts
{
my %conf;
my $global = 0;
GetOptions(\%conf, "dontreturn", "global") || usage();
$conf{return} = !$conf{dontreturn};
$conf{app} = shift(@ARGV) if !$conf{global};
# we still need the keys to send
usage() if !@ARGV;
# get our keys
my $join = ", ";
for (@ARGV)
{
# special cmd, eg 'opt'
if ($opts{lc()})
{
$conf{ops} .= "$opts{lc()} down" . $join;
}
else
{
$conf{keys} .= $_;
}
}
$conf{ops} =~ s/$join$//;
return %conf;
}
sub usage
{
die "usage: $0 <-d (don't return to prev app)> <-g (global) | app name> [cmd] [shift] [opt] [ctrl] <key> [keys...]\n";
}
sub tx
{
# get our osa (ignore app line unless we have app to send)
my $osa;
while (<DATA>)
{
# ignore comments
next if m~^\s*(#|//)~;
# variable cmds
s~
\{\{ # begin condition {{
\$(\w+) \s* \? \s* # ternary: $variable ?
(.*) # then
(?:\s*:\s*(.*))? # : optional else
\}\}
~ $conf{$1} ? $2 : $3 ~xge;
# replace variables
s/\$(\w+)/$conf{$1}/g;
$osa .= $_;
}
system($OSASCRIPT, "-e", $osa);
}
__DATA__
tell application "System Events"
# store current window
set currentProc to name of first process where it is frontmost
# open app
{{$app ? tell application "$app" to activate}}
# send keystrokes
keystroke "$keys" {{$ops ? using {$ops}}}
end tell
# return to old window
{{$return ? tell application currentProc to activate}}