From 94379bcad508bbb6dd47222069a0727d1eb3e500 Mon Sep 17 00:00:00 2001 From: Dean Jackson Date: Fri, 12 Nov 2021 17:12:26 +0100 Subject: [PATCH] Handle empty selection in Finder --- FinderSelection.js | 60 +++++++++++++++------------------------------- info.plist | 3 ++- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/FinderSelection.js b/FinderSelection.js index 58ba010..684c331 100755 --- a/FinderSelection.js +++ b/FinderSelection.js @@ -2,11 +2,11 @@ ObjC.import('stdlib') +// Application bundle IDs const finderId = 'com.apple.Finder', pathFinderId = 'com.cocoatech.PathFinder' -const file2Path = fi => Path(decodeURI(fi.url()).slice(7)).toString() - +// Get environment variable function getEnv(key) { try { return $.getenv(key) @@ -15,56 +15,34 @@ function getEnv(key) { } } +// Return Path Finder selection or target as POSIX paths function pathFinderPaths() { const pf = Application(pathFinderId) - let selection = pf.selection(), - paths = [] - - if (selection) { - selection.forEach(pfi => { - let p = pfi.posixPath() - console.log(`[Path Finder] selection=${p}`) - paths.push(p) - }) - } else { - let p = pf.finderWindows[0].target.posixPath() - console.log(`[Path Finder] target=${p}`) - paths.push(p) - } - - return paths + 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 paths = [], - selection = finder.selection() - - if (selection) { - selection.forEach(fi => { - let p = file2Path(fi) - console.log(`[Finder] selection=${p}`) - paths.push(p) - }) - } else { - let p = file2Path(finder.finderWindows[0].target) - console.log(`[Finder] target=${p}`) - paths.push(p) - } - - return paths + 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() { - console.log('🍻') const activeApp = getEnv('focusedapp') - console.log(`activeApp=${activeApp}`) let paths = [] - if (activeApp === pathFinderId) { - paths = pathFinderPaths() - } else { - paths = finderPaths() - } + console.log(`🍻\nactiveApp=${activeApp}`) + + if (activeApp === pathFinderId) paths = pathFinderPaths() + else paths = finderPaths() return JSON.stringify({alfredworkflow: {arg: paths}}) } \ No newline at end of file diff --git a/info.plist b/info.plist index e924cda..25033d5 100644 --- a/info.plist +++ b/info.plist @@ -802,9 +802,10 @@ variables={allvars} 0 relatedApps - com.apple.finder + com.runningwithcrayons.Alfred-Preferences com.cocoatech.PathFinder com.sublimetext.4 + com.apple.finder relatedAppsMode 2