Skip to content

Commit

Permalink
Handle empty selection in Finder
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Nov 12, 2021
1 parent 0f75927 commit 94379bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
60 changes: 19 additions & 41 deletions FinderSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}})
}
3 changes: 2 additions & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,10 @@ variables={allvars}
<integer>0</integer>
<key>relatedApps</key>
<array>
<string>com.apple.finder</string>
<string>com.runningwithcrayons.Alfred-Preferences</string>
<string>com.cocoatech.PathFinder</string>
<string>com.sublimetext.4</string>
<string>com.apple.finder</string>
</array>
<key>relatedAppsMode</key>
<integer>2</integer>
Expand Down

0 comments on commit 94379bc

Please sign in to comment.