-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGet Library Handlers.applescript
146 lines (119 loc) · 8.31 KB
/
Get Library Handlers.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
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
139
140
141
142
143
144
145
146
#!/usr/bin/osascript
--------------------------------------------------------------------------------
# pnam: GET LIBRARY HANDLERS
# nmxt: .applescript
# pDSC: Retrieves a list of all handlers defined in the lib files and allows
# the user to select one or more by name, the full code for which will
# be pasted into Script Editor (if focused) or copied to the clipboard.
# plst: +funclist* : A list of handler names to bypass manual selection
# rslt: 0 : User cancelled selection
# 1 : Handlers' source code copied to clipboard (notification displayed)
# 2 : Handlers' source code pasted into Script Editor
--------------------------------------------------------------------------------
# sown: CK
# ascd: 2018-12-05
# asmo: 2018-12-07
--------------------------------------------------------------------------------
property parent : script "load.scpt"
property regex : load script "_regex"
--------------------------------------------------------------------------------
property Finder : application "Finder"
--------------------------------------------------------------------------------
property date : missing value
property contents : missing value
property lf3 : linefeed & linefeed & linefeed
--------------------------------------------------------------------------------
# IMPLEMENTATION:
on run funclist
if funclist's class = script then set funclist to {}
set OK to "Copy"
get name of application (path to frontmost application)
if the result = "Script Editor" then set OK to "Paste"
if [my date, contents] contains missing value ¬
or willUpdateContents() then init()
if {} = funclist or {missing value} = funclist then
regex's map(contents, regexPattern for anything, "$2")
choose from list sort(result) ¬
with title ["Library Handlers"] ¬
with prompt ["Select handler:"] ¬
OK button name OK ¬
cancel button name ["Cancel"] ¬
with multiple selections allowed
set funclist to {} & the result
if funclist = {false} then return 0
end if
set funcs to regex's join(funclist, "|")
set code to regex's match(contents, regexPattern for funcs)
if OK = "Copy" then
set the clipboard to regex's join(code, lf3)
display notification ["Handlers copied to clipboard"] ¬
with title my name
return 1
end if
tell application "Script Editor" to tell its front document to ¬
set its selection's contents to regex's join(code, lf3)
return 2
end run
--------------------------------------------------------------------------------
# HANDLERS & SCRIPT OBJECTS:
on regexPattern for function
set subpattern to function
if function = anything then set subpattern to "(\\|?)\\w+(\\3)"
"(?ism)^(on|to) (" & subpattern & ")\\b.*?^end \\2"
end regexPattern
to init()
log "init"
set libFiles to getLibFiles() as «class alst»
set my contents to readFiles(libFiles)
set my date to the current date
end init
to getLibFiles()
set fp to POSIX file (path to me) as alias
set lib to [fp, "::", "lib"] as text as alias
set fs to a reference to («class orig» of ¬
every «class alia» in Finder's item lib ¬
whose name starts with "_" and ¬
name extension = "applescript")
if not (fs exists) then return a reference to ¬
(every «class docf» in Finder's item lib ¬
whose name starts with "_" and ¬
name extension = "applescript")
fs
end getLibFiles
on willUpdateContents()
script
property list : modification date of getLibFiles()
end script
repeat with d in result's list
if d > my date then return true
end repeat
false
end willUpdateContents
to readFiles(fs)
local fs
script |files|
property list : fs
property contents : {}
end script
repeat with f in the list of |files|
read f as «class ut16»
set end of contents of |files| to the result
end repeat
regex's join(contents of |files|, linefeed)
set my contents to the result
end readFiles
to sort(L)
local L
script
use framework "Foundation"
to sort(L, mode)
local L, mode
((current application's NSArray's ¬
arrayWithArray:L)'s ¬
sortedArrayUsingSelector:mode) ¬
as list
end sort
end script
result's sort(L, "caseInsensitiveCompare:")
end sort
---------------------------------------------------------------------------❮END❯