-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppManager.lua
More file actions
1045 lines (902 loc) · 37.1 KB
/
AppManager.lua
File metadata and controls
1045 lines (902 loc) · 37.1 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- TODO - 5f3f9bdb-9112-492a-aed6-24cd63c21452 - add a madness interactive header to the top of the file
-- AppManager.lua - Application management utilities
-- Using singleton pattern to avoid multiple initializations
-- =================================
local HyperLogger = require('HyperLogger')
local log = HyperLogger.new()
-- Check if module is already initialized
if _G.AppManager then
log:d('Returning existing AppManager module')
return _G.AppManager
end
log:i('Initializing application management system')
local FileManager = require('FileManager')
local scripts_dir = os.getenv("HOME") .. "/.hammerspoon/scripts"
local AppManager = {}
-- Configuration
local enableMultiWindowSelector = true
local enableMenuSeparators = false
-- Application Management Functions
function AppManager.madFocus(appName)
if not enableMultiWindowSelector then
hs.application.launchOrFocus(appName)
return
end
local app = hs.application.find(appName)
if not app then
hs.application.launchOrFocus(appName)
return
end
local windows = app:allWindows()
if #windows == 0 then
hs.application.launchOrFocus(appName)
return
end
if #windows == 1 then
windows[1]:focus()
return
end
local choices = {}
local openWindowTitles = {}
-- Add existing windows as choices
for i, win in ipairs(windows) do
local title = win:title()
table.insert(choices, {
text = title,
subText = "Focus this " .. appName .. " window",
window = win,
type = "window",
image = hs.image.imageFromName("NSRightFacingTriangle")
})
openWindowTitles[title] = true
end
if enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
local projects_list = FileManager.getProjectsList()
for _, project in ipairs(projects_list) do
if not openWindowTitles[project.name] then
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "window" then
-- Focus the selected window
choice.window:focus()
elseif choice.type == "project" and choice.text == "New Project" then
-- Handle New Project creation
-- local home = os.getenv("HOME")
-- local timestamp = os.date("%Y%m%d_%H%M%S")
-- local newProjectPath = home .. "/lab/new_project_" .. timestamp
-- -- Create the directory and open it
-- hs.execute("mkdir -p '" .. newProjectPath .. "'")
-- hs.execute("open -a '" .. appName .. "' '" .. newProjectPath .. "'")
-- -- Alert the user
hs.alert.show("TODO: Implement new project creation")
elseif choice.type == "project" then
-- Open the selected project with the application
hs.execute("open -a '" .. appName .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with the application
hs.execute("open -a '" .. appName .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
end
-- Special function for GitHub Desktop that always shows the selection menu
function AppManager.launchGitHubWithProjectSelection(app)
local appName = "GitHub Desktop"
if not app then
app = hs.application.find(appName)
end
if not app then
-- If GitHub Desktop isn't running, launch it with the selection menu
local choices = {}
if enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
local projects_list = FileManager.getProjectsList()
for _, project in ipairs(projects_list) do
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "project" then
-- Open the selected project with GitHub Desktop
hs.execute("open -a '" .. appName .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with GitHub Desktop
hs.execute("open -a '" .. appName .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
else
-- If GitHub Desktop is running, get all its windows
local windows = app:allWindows()
local choices = {}
local openWindowTitles = {}
-- Add existing windows as choices
for i, win in ipairs(windows) do
local title = win:title()
table.insert(choices, {
text = title,
subText = "Focus this " .. appName .. " window",
window = win,
type = "window",
image = hs.image.imageFromName("NSRightFacingTriangle")
})
openWindowTitles[title] = true
end
if enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
local projects_list = FileManager.getProjectsList()
for _, project in ipairs(projects_list) do
if not openWindowTitles[project.name] then
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "window" then
-- Focus the selected window
choice.window:focus()
elseif choice.type == "project" then
-- Open the selected project with GitHub Desktop
hs.execute("open -a '" .. appName .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with GitHub Desktop
hs.execute("open -a '" .. appName .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. appName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
end
end
-- Special function for Cursor that also updates GitHub Desktop
function AppManager.launchCursorWithGitHubDesktop()
local cursorAppName = "cursor"
local githubAppName = "GitHub Desktop"
local cursor = hs.application.find(cursorAppName)
if not cursor then
-- If Cursor isn't running, launch it with the selection menu
local choices = {}
-- Add a separator if enabled
if enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
local projects_list = FileManager.getProjectsList()
for _, project in ipairs(projects_list) do
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "project" then
-- Open the selected project with both GitHub Desktop and Cursor
hs.execute("open -a '" .. githubAppName .. "' " .. choice.path)
hs.execute("open -a '" .. cursorAppName .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with both GitHub Desktop and Cursor
hs.execute("open -a '" .. githubAppName .. "' " .. choice.path)
hs.execute("open -a '" .. cursorAppName .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. cursorAppName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. cursorAppName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
else
-- If Cursor is running, get all its windows
local windows = cursor:allWindows()
local choices = {}
local openWindowTitles = {}
-- Add existing windows as choices
for i, win in ipairs(windows) do
local title = win:title()
-- Try to extract path information from window title or use empty string
local path = ""
-- Check if this window title matches any known project
for _, project in ipairs(FileManager.getProjectsList()) do
if title:match(project.name) then
path = project.path
break
end
end
table.insert(choices, {
text = title,
subText = "Focus this " .. cursorAppName .. " window",
window = win,
type = "window",
path = path,
image = hs.image.imageFromName("NSRightFacingTriangle")
})
openWindowTitles[title] = true
end
-- Add a separator if enabled
if enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
local projects_list = FileManager.getProjectsList()
for _, project in ipairs(projects_list) do
if not openWindowTitles[project.name] then
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "window" then
-- Focus the selected window and update GitHub Desktop if we have a path
if choice.path and choice.path ~= "" then
hs.execute("open -a '" .. githubAppName .. "' " .. choice.path)
hs.execute("open -a '" .. cursorAppName .. "' " .. choice.path)
end
choice.window:focus()
elseif choice.type == "project" then
-- Open the selected project with both GitHub Desktop and Cursor
hs.execute("open -a '" .. githubAppName .. "' " .. choice.path)
hs.execute("open -a '" .. cursorAppName .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with both GitHub Desktop and Cursor
hs.execute("open -a '" .. githubAppName .. "' " .. choice.path)
hs.execute("open -a '" .. cursorAppName .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. cursorAppName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. cursorAppName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
end
end
-- Special function for Antigravity editor with project selection menu
function AppManager.launchAntigravityWithProjectSelection()
local antigravityAppName = "antigravity"
local antigravityPath = "/Users/d.edens/.antigravity/antigravity/bin/antigravity"
-- Try to find antigravity as a registered application first (fast path)
local app = hs.application.find(antigravityAppName)
local windows = {}
if app then
-- Fast path: get windows directly from the app
windows = app:allWindows()
else
-- Fallback: scan all windows (slower, but handles unregistered apps)
local allWindows = hs.window.allWindows()
for _, win in ipairs(allWindows) do
local winApp = win:application()
if winApp and winApp:name() then
local title = win:title()
-- Look for antigravity windows - check if the app name or title contains antigravity
if winApp:name():lower():match("antigravity") or (title and title:lower():match("antigravity")) then
table.insert(windows, win)
end
end
end
end
-- Cache projects list to avoid multiple calls
local projects_list = FileManager.getProjectsList()
local choices = {}
local openWindowTitles = {}
-- Add existing windows as choices if any
if #windows > 0 then
for _, win in ipairs(windows) do
local title = win:title()
local path = ""
-- Check if this window title matches any known project
for _, project in ipairs(projects_list) do
if title:match(project.name) then
path = project.path
break
end
end
table.insert(choices, {
text = title,
subText = "Focus this " .. antigravityAppName .. " window",
window = win,
type = "window",
path = path,
image = hs.image.imageFromName("NSRightFacingTriangle")
})
openWindowTitles[title] = true
end
end
-- Add a separator if enabled and we have windows
if #windows > 0 and enableMenuSeparators then
table.insert(choices, {
text = "──────────────────────────────────",
subText = "Projects",
disabled = true
})
end
-- Add projects list as choices
for _, project in ipairs(projects_list) do
if not openWindowTitles[project.name] then
table.insert(choices, {
text = project.name,
subText = "Open " .. project.path,
path = project.path,
type = "project",
image = hs.image.imageFromName("NSBookmark")
})
end
end
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "window" then
-- Focus the selected window
choice.window:focus()
elseif choice.type == "project" then
-- Open the selected project with antigravity
hs.execute("'" .. antigravityPath .. "' " .. choice.path)
elseif choice.type == "custom" then
-- Open the custom path with antigravity
hs.execute("'" .. antigravityPath .. "' " .. choice.path)
end
end)
-- Handle the query changed callback for custom paths
chooser:queryChangedCallback(function(query)
if query:match("^[~/]") then
-- If query starts with / or ~, show only one option for custom path
local customChoices = {}
for k, v in pairs(choices) do customChoices[k] = v end
table.insert(customChoices, 1, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. antigravityAppName,
path = query,
type = "custom"
})
chooser:choices(customChoices)
else
-- Filter choices based on the query
local filteredChoices = {}
local hasMatches = false
local matchingChoices = {}
local separatorChoice = nil
-- First find the separator and matching items
for _, choice in ipairs(choices) do
if choice.disabled then
separatorChoice = choice
elseif choice.type == "project" or choice.type == "window" then
if choice.text:lower():find(query:lower(), 1, true) then
table.insert(matchingChoices, choice)
hasMatches = true
end
end
end
-- Add windows first if any
for _, choice in ipairs(matchingChoices) do
if choice.type == "window" then
table.insert(filteredChoices, choice)
end
end
-- Add separator if we have any matches and menu separators are enabled
if hasMatches and separatorChoice and enableMenuSeparators then
table.insert(filteredChoices, separatorChoice)
end
-- Add projects
for _, choice in ipairs(matchingChoices) do
if choice.type == "project" then
table.insert(filteredChoices, choice)
end
end
-- If no matches found and query is not empty, add custom path option
if not hasMatches and query ~= "" then
table.insert(filteredChoices, {
text = "Open custom path: " .. query,
subText = "Enter to open this path with " .. antigravityAppName,
path = query,
type = "custom"
})
end
chooser:choices(filteredChoices)
end
end)
chooser:choices(choices)
chooser:show()
end
-- Application Launch Functions
function AppManager.open_github()
local githubAppName = "GitHub Desktop"
local seatOfMadness = "/Users/d.edens/lab/madness_interactive"
hs.execute("open -a '" .. githubAppName .. "' " .. seatOfMadness)
end
function AppManager.open_cursor_with_github()
AppManager.launchCursorWithGitHubDesktop()
end
function AppManager.open_slack()
AppManager.madFocus("Slack")
end
function AppManager.open_arc()
AppManager.madFocus("Arc")
end
function AppManager.open_zen()
AppManager.madFocus("Zen")
end
function AppManager.open_chrome()
AppManager.madFocus("Google Chrome")
end
function AppManager.open_pycharm()
AppManager.madFocus("PyCharm Community Edition")
end
function AppManager.open_antigravity()
AppManager.launchAntigravityWithProjectSelection()
end
function AppManager.open_postman()
AppManager.madFocus("Postman")
end
function AppManager.open_anythingllm()
AppManager.madFocus("AnythingLLM")
end
function AppManager.open_lmstudio()
AppManager.madFocus("LM Studio")
end
function AppManager.open_mongodb()
AppManager.madFocus("MongoDB Compass")
end
function AppManager.open_logi()
AppManager.madFocus("logioptionsplus")
end
function AppManager.open_system()
AppManager.madFocus("System Preferences")
end
function AppManager.open_vscode()
AppManager.madFocus("Visual Studio Code")
end
function AppManager.open_cursor()
AppManager.madFocus("cursor")
end
function AppManager.open_barrier()
AppManager.madFocus("Barrier")
end
function AppManager.open_windsurf()
AppManager.madFocus("Windsurf")
end
-- scrcpy - Special handling for command-line tools
function AppManager.open_scrcpy()
-- scrcpy is a command-line tool, not a traditional app
-- We need to find windows by title rather than application
local scrcpyWindows = {}
-- Get all windows and filter for scrcpy windows
local allWindows = hs.window.allWindows()
for _, win in ipairs(allWindows) do
local title = win:title()
-- Look for scrcpy windows (they typically have device names or "scrcpy" in the title)
if title and (title:lower():match("scrcpy") or title:match("SM%-[A-Z]%d+") or title:match("%d+x%d+")) then
local app = win:application()
if app and app:name() then
table.insert(scrcpyWindows, {
window = win,
title = title,
appName = app:name()
})
end
end
end
-- If no scrcpy windows are found, launch scrcpy
if #scrcpyWindows == 0 then
log:i('No scrcpy windows found, launching scrcpy')
local cmd = string.format("nohup %s/launch_scrcpy.sh samsung &> /dev/null &", scripts_dir)
log:d('Executing command:', cmd)
local success, output, error = os.execute(cmd)
if success then
log:i('Successfully launched scrcpy script')
else
log:e('Error launching scrcpy script:', error)
end
return
end
-- If only one scrcpy window, focus it
if #scrcpyWindows == 1 then
log:i('Focusing single scrcpy window: ' .. scrcpyWindows[1].title)
scrcpyWindows[1].window:focus()
return
end
-- If multiple scrcpy windows, show chooser
local choices = {}
for i, scrcpyWin in ipairs(scrcpyWindows) do
table.insert(choices, {
text = scrcpyWin.title,
subText = "Focus scrcpy window (" .. scrcpyWin.appName .. ")",
window = scrcpyWin.window,
type = "window",
image = hs.image.imageFromName("NSRightFacingTriangle")
})
end
-- Add option to launch new scrcpy instance
table.insert(choices, {
text = "Launch New scrcpy",
subText = "Start a new scrcpy instance",
type = "new",
image = hs.image.imageFromName("NSAddTemplate")
})
local chooser = hs.chooser.new(function(choice)
if not choice then return end
if choice.type == "window" then
log:i('Focusing selected scrcpy window: ' .. choice.text)
choice.window:focus()
elseif choice.type == "new" then
log:i('Launching new scrcpy instance')
hs.execute("/opt/homebrew/bin/scrcpy &")
end
end)
chooser:placeholderText("Select scrcpy window or launch new")