This repository has been archived by the owner on Jul 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsublime.php
55 lines (46 loc) · 1.45 KB
/
sublime.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
<?php
// Main configuration
$inQuery = $argv[1] ?: '';
$reRowQuery = '/' . implode('.*?', str_split(preg_quote($inQuery))) . '.*\.sublime-project$/i';
$results = array();
$cache = "/tmp/alfred2-sublimeprojects.tmp";
$ttl = 10;
$paths = (file_exists($cache) && time()-filemtime($cache) < $ttl) ? json_decode(file_get_contents($cache)) : array();
if (!count($paths))
{
exec('mdfind "kMDItemFSName=*.sublime-project" | grep -v node_modules', $paths);
file_put_contents($cache, json_encode($paths));
}
foreach ($paths AS $path)
{
$pathInfo = pathinfo($path);
if ($inQuery == '' || preg_match($reRowQuery, $pathInfo['basename']))
$results[] = array(
'uid' => $path,
'arg' => $path,
'title' => basename($path, '.'.$pathInfo['extension']),
'subtitle' => 'Open (new window) or ⌘ (append) ',
'icon' => 'icon.png',
'valid' => true);
}
// No favorites matched
if (!count($results))
$results[] = array(
'uid' => 'none',
'arg' => 'none',
'title' => 'No Favorites Found',
'subtitle' => 'No favorites matching your query were found',
'icon' => 'icon.png',
'valid' => false);
// Preparing the XML output file
$xmlObject = new SimpleXMLElement("<items></items>");
foreach($results AS $rows)
{
$nodeObject = $xmlObject->addChild('item');
$nodeKeys = array_keys($rows);
foreach ($nodeKeys AS $key)
$nodeObject->{ $key == 'uid' || $key == 'arg' ? 'addAttribute' : 'addChild' }($key, $rows[$key]);
}
// Print the XML output
echo $xmlObject->asXML();
?>