-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfocus.scpt
executable file
·223 lines (200 loc) · 7.16 KB
/
focus.scpt
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/osascript
# vim: set filetype=applescript :
-- Copyright (c) 2010 Gregory A Frascadore
-- Licensed under the Open Software License version 3.0
# from
# http://forums.omnigroup.com/archive/index.php/t-7998.html
(*
Run from Launchbar to refocus current window on any projects
whose names match a sub-string entered in Launchbar.
If the script is indexed in and summoned from Launchbar,
tapping spacebar will allow you enter enough of any substring
of a project name to uniquely identify it.
If the string entered turns out not to be unique,
then a list of candidate projects will get the focus in the current window.
(Useful to me as I often have a small set of projects with related names)
*)
property pstrTitle : "Focus on project"
property pmaxProjCount : 20
on run (sProjName)
-- if sProjName is missing we will focus on Inbox,
-- but if the sProjName is Foo (including "Inbox") we will
-- focus on projects containing Foo, not Inbox
tell application id "com.omnigroup.OmniFocus"
set oDoc to default document
set oWin to my firstVisibleWindow(oDoc)
if oWin is missing value then
set oWin to make new document window with properties { ¬
bounds: {0,0,800,1200}, ¬
selected view mode identifier:"project" ¬
}
end
set lstprojects to {}
if length of sProjName > 0 then
-- set lstprojects to lstprojects & my ProjectsByName(oDoc, sProjName)
-- set lstprojects to my FoldersByName(oDoc, sProjName)
-- set lstprojects to my TasksByName(oDoc, sProjName)
-- set lstprojects to lstprojects & my SectionsByTaskName(oDoc, sProjName)
set lstprojects to lstprojects & my projectsHavingname(sections of oDoc, sProjName)
end
tell oWin
if lstprojects is {} and length of sProjName > 0 then
return "nothing found"
end if
if lstprojects is {} then
set focus to {}
tell sidebar to select inbox
set selected sorting identifier of content to "modified"
set selected task state filter identifier of content to "incomplete"
set search term to ""
set visible of oWin to true
tell oWin to activate
return {}
else
set search term to missing value
set focus to lstprojects
tell sidebar to select library
set selected sorting identifier of content to "modified"
set selected task state filter identifier of content to "incomplete"
set search term to sProjName
set visible of oWin to true
tell oWin to activate
return properties of oWin
end
end
end
tell application "System Events"
tell process "Dashboard"
delay 0.3
key code 111
return lstprojects
end
end
end run
on firstVisibleWindow(oDoc)
using terms from application "OmniFocus"
set oWin to missing value
set aWindows to document windows of oDoc
repeat with oWin in aWindows
if oWin is visible then
exit repeat
end if
end repeat
if oWin is missing value or oWin is not visible then
tell oDoc to ¬
set oWin to make new document window with properties { ¬
bounds: {0,0,800,1200}, ¬
visible: true, ¬
selected view mode identifier:"project" ¬
}
end
return oWin
end using terms from
end firstVisibleWindow
on ProjectsByName(oDoc, strName)
using terms from application "OmniFocus"
tell oDoc
set lstMatches to (complete strName as project ¬
maximum matches pmaxProjCount)
set lstprojects to {}
repeat with recMatch in lstMatches
if name of recMatch contains strName then
set end of lstprojects to project id (id of recMatch)
end
end repeat
return lstprojects
end tell
end using terms from
end ProjectsByName
on FoldersByName(oParent, strName)
using terms from application "OmniFocus"
set lstMatches to folders of oParent where name contains strName
if length of lstMatches > 0 then
return lstMatches
else
set lstFolders to {}
repeat with oFolder in folders of oParent
set lstFolders to lstFolders & my FoldersByName(oFolder, strName)
end repeat
end if
return lstFolders
end using terms from
end FolderByName
on TasksByName(oParent, strName)
using terms from application "OmniFocus"
set lstMatches to tasks of contents of oParent where name contains strName
if length of lstMatches > 0 then
return first item of lstMatches
else
set lstFolders to folders of oParent
repeat with oFolder in lstFolders
set varResult to TasksByName(oFolder, strName)
if varResult is not missing value then return varResult
end repeat
end if
return missing value
end using terms from
end TasksByName
on SectionsByTaskName(oParent, sName)
using terms from application "OmniFocus"
try
-- set aMatches to get every project of oParent ¬
set aMatches to get every section of oParent ¬
where some task's name contains sName
return aMatches
on error
set aMatches to {}
end try
set aFolders to folders of oParent
repeat with oFolder in aFolders
set aMatches to aMatches & my SectionsByTaskName(oFolder, sName)
end repeat
return aMatches
end using terms from
end SectionsByTaskName
on projectsHavingName(aSections, sName)
using terms from application "OmniFocus"
local aResults
set aResults to {}
repeat with oSection in aSections
try
set aTasks to tasks of oSection
on error
set aTasks to {}
end try
if oSection's name contains sName then
set beginning of aResults to oSection
else if my someTaskHasName(aTasks, sName) then
set beginning of aResults to oSection
else
try
set aResults to my projectsHavingName(sections of oSection, sName) & aResults
on error
-- ignore
end try
end if
end repeat
return aResults
end using terms from
end projectsHavingName
on someTaskHasName(aTasks, sName)
local aSubTasks
using terms from application "OmniFocus"
repeat with oTask in aTasks
set aSubTasks to tasks of oTask
if name of oTask contains sName then
return true
else if someTaskHasName(aSubTasks, sName)
return true
end
end repeat
return false
end using terms from
end someTaskHasName
on classList(aSections)
set aResults to {}
repeat with oSection in aSections
set aResults to class of oSection & aResults
end repeat
return aResults
end classList