forked from deanishe/alfred-sublime-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinderSelection.js
executable file
·48 lines (40 loc) · 1.23 KB
/
FinderSelection.js
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
#!/usr/bin/osascript -l JavaScript
ObjC.import('stdlib')
// Application bundle IDs
const finderId = 'com.apple.Finder',
pathFinderId = 'com.cocoatech.PathFinder'
// Get environment variable
function getEnv(key) {
try {
return $.getenv(key)
} catch(e) {
return null
}
}
// Return Path Finder selection or target as POSIX paths
function pathFinderPaths() {
const pf = Application(pathFinderId)
let selection = pf.selection()
// selected files
if (selection) return selection.map(pfi => pfi.posixPath())
// target of frontmost window
return [pf.finderWindows[0].target.posixPath()]
}
// Return Finder selection or target as POSIX paths
function finderPaths() {
const file2Path = fi => Path(decodeURI(fi.url()).slice(7)).toString()
const finder = Application(finderId)
let selection = finder.selection()
// selected files
if (selection && selection.length) return selection.map(file2Path)
// target of frontmost window
return [file2Path(finder.finderWindows[0].target)]
}
function run() {
const activeApp = getEnv('focusedapp')
let paths = []
console.log(`🍻\nactiveApp=${activeApp}`)
if (activeApp === pathFinderId) paths = pathFinderPaths()
else paths = finderPaths()
return JSON.stringify({alfredworkflow: {arg: paths}})
}