-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_active_app.applescript
More file actions
54 lines (49 loc) · 1.25 KB
/
Copy pathget_active_app.applescript
File metadata and controls
54 lines (49 loc) · 1.25 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
on run
global activePID, activeName, windowName
set activeName to ""
set windowName to ""
tell application "System Events"
set activeApp to first application process whose frontmost is true
set activePID to unix id of activeApp
set activeName to name of activeApp
tell activeApp
try
tell (1st window whose value of attribute "AXMain" is true)
set windowName to value of attribute "AXTitle"
end tell
end try
end tell
end tell
return ("{\"pid\":" & activePID & ",\"name\":" & quoteJSONString(activeName) & ",\"window\":" & quoteJSONString(windowName) & "}" as text)
end run
on quoteJSONString(val)
set codepoints to id of val
if (class of codepoints) is not list
set codepoints to {codepoints}
end
set escaped to ""
repeat with cp in codepoints
set cp to cp as integer
if cp = 34
set q to "\\\""
else if cp = 92 then
set q to "\\\\"
else if 32 <= cp and cp < 127
set q to character id cp
else
set q to uEncode(cp)
end
set escaped to escaped & q
end
return "\"" & escaped & "\""
end
on uEncode(c)
set digits to "0123456789abcdef"
set hex to ""
repeat until length of hex = 4
set d to (c mod 16)
set c to (c - d)/16 as integer
set hex to (character (1+d) of digits) & hex
end
return "\\u" & hex
end