-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvsc
More file actions
executable file
·138 lines (114 loc) · 3.56 KB
/
vsc
File metadata and controls
executable file
·138 lines (114 loc) · 3.56 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env zsh
# fzf opens /dev/tty directly for its interactive UI:
# https://github.com/junegunn/fzf/blob/v0.67.0/src/tui/light.go#L31
# if ! [[ -r /dev/tty ]]; then
# echo "Error: This script requires an interactive terminal (fzf needs /dev/tty)" >&2
# exit 1
# fi
declare -A name_to_bundle_id_map
name_to_bundle_id_map=(
code "com.microsoft.vscode"
codium "com.vscodium"
)
declare -A bundle_id_to_name_map
for name in ${(k)name_to_bundle_id_map}; do
bundle_id_to_name_map[${(L)name_to_bundle_id_map[$name]}]="$name"
done
targetBundleIDs=( ${name_to_bundle_id_map[${0:t}]} )
if [[ ${#targetBundleIDs[@]} -eq 0 ]]; then
targetBundleIDs=( ${(v)name_to_bundle_id_map} )
fi
vscode_app_bundles="$(osascript -e "$(cat <<EOT
on run argv
set targetBundleIDs to argv
tell application "System Events"
set foundAppBundles to {}
set theResult to {}
repeat with bundleID in targetBundleIDs
try
repeat with proc in (every process whose bundle identifier is bundleID)
set appBundlePath to POSIX path of application file of proc
set end of theResult to "PID:" & unix id of proc & tab & appBundlePath
set end of foundAppBundles to appBundlePath
end repeat
on error
-- Nothing to do
end try
end repeat
repeat with bundleID in targetBundleIDs
try
set query to "kMDItemCFBundleIdentifier ==" & quoted form of bundleID & "c"
set mdfindOutput to do shell script "mdfind " & quoted form of query
set mdfindOutputLines to paragraphs of mdfindOutput
repeat with aLine in mdfindOutputLines
if foundAppBundles does not contain aLine then
set end of theResult to "Not Running" & tab & aLine
end if
end repeat
on error
-- Nothing to do
end try
end repeat
set AppleScript's text item delimiters to linefeed
set pathsString to theResult as text
set AppleScript's text item delimiters to ""
return pathsString
end tell
end run
EOT
)" -- "${targetBundleIDs[@]}" )"
vscode_app_bundle_path=""
() {
local lines=( ${(f)vscode_app_bundles} )
[[ ${#lines[@]} -ne 1 ]] && return
local parts=( ${(@ps:\t:)lines[1]} )
[[ ${#parts[@]} -ne 2 ]] && return
vscode_app_bundle_path="${parts[2]}"
}
() {
if [[ -n "$vscode_app_bundle_path" ]]; then
return
fi
local fzf_args=(
--wrap --reverse --exit-0
--select-1
--header-first
--header="Select which one to use:"
--height 40%
--min-height 18+
--delimiter $'\t'
--with-nth 1,2
--preview='
mdls -name kMDItemDisplayName -name kMDItemVersion {2} ;
echo --- ;
codesign --display --verbose=2 --check-notarization {2} ;
'
--preview-window='right,50%'
)
local vscode_app_bundle=""
local fzf_exit_code=1
if [[ -n "$vscode_app_bundles" ]]; then
vscode_app_bundle="$(fzf < <(echo "$vscode_app_bundles") "${fzf_args[@]}")"
fzf_exit_code=$?
fi
local parts=( ${(ps:\t:)vscode_app_bundle} )
if [[ ${#parts[@]} -ne 2 ]]; then
echo "Invalid selection format." 1>&2
exit 127
fi
vscode_app_bundle_path="${parts[2]}"
if [[ "$fzf_exit_code" -eq 130 ]]; then
# Interrupted with Ctrl-C or Esc
exit 130
elif [[ "$fzf_exit_code" -ne 0 ]] || ! [[ -d "$vscode_app_bundle_path" ]]; then
echo "Failed to find an app bundle." 1>&2
exit 127
fi
}
if [[ -z "$vscode_app_bundle_path" ]] || ! [[ -d "$vscode_app_bundle_path" ]]; then
echo "No valid app bundle path found." 1>&2
exit 127
fi
vscode_bundle_id=$(defaults read "$vscode_app_bundle_path/Contents/Info.plist" CFBundleIdentifier 2>/dev/null)
vscode_exec_name="${bundle_id_to_name_map[${(L)vscode_bundle_id}]:-code}"
exec "$vscode_app_bundle_path/Contents/Resources/app/bin/$vscode_exec_name" "$@"