forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_running_app_ids
executable file
·115 lines (92 loc) · 2.18 KB
/
list_running_app_ids
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
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
#
# list_running_app_ids
#
###
### dependencies
###
require "open3"
require "set"
###
### globals
###
$opt_test = nil
###
### methods
###
def check_ruby
if RUBY_VERSION.to_f < 2.0
print "You are currently using Ruby ", RUBY_VERSION, ", but version 2.0 or above is required."
exit 1
end
end
def usage
<<-EOS
list_running_app_ids [ -t <bundle-id> ]
Print a list of currently running Applications and associated
Bundle IDs, which may be useful in a Cask uninstall stanza, eg
uninstall quit: 'bundle.id.goes.here'
Applications attributed to Apple are excluded from the output.
With optional "-t <bundle-id>", silently test if a given app
is running, exiting with an error code if not.
See CONTRIBUTING.md for more information.
EOS
end
def process_args
until ARGV.empty?
if ARGV.first =~ %r{^-+t(?:est)?$} && ARGV.length > 1
ARGV.shift
$opt_test = ARGV.shift
elsif ARGV.first =~ %r{^-+h(?:elp)?$}
puts usage
exit 0
else
puts usage
exit 1
end
end
end
def load_apps
out, err, status = Open3.capture3("/usr/bin/osascript", "-e", 'tell application "System Events" to get (name, bundle identifier, unix id) of every process')
if status.exitstatus > 0
puts err
exit status.exitstatus
end
out = out.split(", ")
one_third = out.length / 3
@app_names = out.shift(one_third)
@bundle_ids = out.shift(one_third)
@unix_ids = out.shift(one_third)
end
def test_app(bundle)
@bundle_ids.include?(bundle) ? 0 : 1
end
def excluded_bundle_id(bundle_id)
%r{^com\.apple\.}.match(bundle_id)
end
def excluded_app_name(app_name)
%r{^osascript$}.match(app_name) # this script itself
end
def report_apps
running = Set.new
@app_names.zip(@bundle_ids, @unix_ids).each do |app_name, bundle_id, _unix_id|
next if excluded_bundle_id bundle_id
next if excluded_app_name app_name
bundle_id.gsub!(%r{^(missing value)$}, '<\1>')
running.add "#{bundle_id}\t#{app_name}"
end
puts "bundle_id\tapp_name\n"
puts "--------------------------------------\n"
puts running.to_a.sort
end
###
### main
###
check_ruby
process_args
load_apps
if $opt_test
exit test_app($opt_test)
else
report_apps
end