-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
83 lines (69 loc) · 2.11 KB
/
search.php
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
<?php
require_once('vendor/autoload.php');
use Alfred\Workflows\Workflow;
use CFPropertyList\CFPropertyList;
$parsedBookmarks = [];
$workflow = new Workflow();
$bookmarkFile = $workflow->env('HOME') . "/Library/Application Support/com.fournova.Tower3/bookmarks-v2.plist";
try {
$bookmarkData = new CFPropertyList($bookmarkFile, CFPropertyList::FORMAT_XML);
} catch (Exception $e) {
$workflow->logger()->log('Couldn’t parse bookmark file: ' . $e->getMessage());
return;
}
$results = [];
$query = trim($workflow->argument() ?? '');
$bookmarks = parseItems($bookmarkData->toArray()['children'], $parsedBookmarks);
foreach ($bookmarks as $bookmark) {
if (str_contains($bookmark['name'], $query)) {
$results[] = $bookmark;
}
}
foreach ($results as $hit) {
$folderPath = str_replace('file://', '', $hit['fileURL']);
$workflow->item()
->title($hit['name'])
->subtitle($folderPath)
->iconForFilePath('/Applications/Tower.app')
->arg($folderPath);
}
$workflow->output();
/**
* Parse a Tower bookmark group (folder), recursively if necessary,
* adding individual bookmarks to $this->parsedBookmarks.
*
* @param array $arr group
* @param $parsedBookmarks
* @return void
*/
function parseGroup(array $arr, &$parsedBookmarks): void
{
foreach ($arr as $item) {
if (isGroup($item)) {
parseGroup($item['children'], $parsedBookmarks);
} else {
$parsedBookmarks[] = $item;
}
}
}
/**
* Parse nested bookmarks into flat array.
*
* @param array $arr multidimensional bookmark array read from Tower plist and converted by CFPropertyList
* @return array flat array of bookmark items
*/
function parseItems(array $arr, $parsedBookmarks): array
{
parseGroup($arr, $parsedBookmarks);
return $parsedBookmarks;
}
/**
* Determine whether the current item (array) is a Tower bookmark group.
*
* @param array $item element from multi-dimensional array of bookmarks
* @return boolean true if item is a folder (has `children` property)
*/
function isGroup(array $item): bool
{
return isset($item['children']);
}