-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet Selected Text.applescript
53 lines (44 loc) · 3.9 KB
/
Get Selected Text.applescript
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
#!/usr/bin/osascript
--------------------------------------------------------------------------------
# pnam: GET SELECTED TEXT
# nmxt: .applescript
# pDSC: Returns the contents of the current text selection. Does not work for
# all applications, most notably web browsers.
# plst: -
# rslt: «ctxt» : The text selection content
# «null» : No selected text found
--------------------------------------------------------------------------------
# sown: CK
# ascd: 2018-03-29
# asmo: 2018-11-03
--------------------------------------------------------------------------------
use sys : application "System Events"
--------------------------------------------------------------------------------
property process : a reference to (first process whose frontmost = true)
property window : a reference to window 1 of my process
property UI element : a reference to UI elements of my process
--------------------------------------------------------------------------------
# IMPLEMENTATION:
set AX to filteredAttributes for my UI element
if AX contains text then return the text in AX
recurse thru a reference to the UI elements of my window
--------------------------------------------------------------------------------
# HANDLERS:
to recurse thru UIElements
local UIElements
if not (UIElements exists) then return null
tell the (filteredAttributes for the UIElements) ¬
to if it contains text then return its text
recurse thru a reference to the UI elements of the UIElements
end recurse
on filteredAttributes for UIElements
local UIElements
tell (a reference to (UIElements whose ¬
name of attributes contains "AXSelectedText" and ¬
value of attribute "AXSelectedText" ≠ "" and ¬
class of value of attribute "AXSelectedText" ≠ class)) ¬
to if exists then return {the value of the attribute ¬
"AXSelectedText" of item 1 of its contents, text}
null
end filteredAttributes
---------------------------------------------------------------------------❮END❯