Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the POSIX path of the Finder's front window

How can I get the POSIX path of the Finder window that is currently at the top of its window list? Preferably with some kind of Cocoa framework but I am open for anything.

Background: I would need this because I want to make a selection of files matching a regex pattern, starting recursively from this path. The idea is to use

[[NSWorkspace sharedWorkspace] subpathsAtPath:thePath]

method to get all the descendants of this path, use "grep" in an NSTask (to avoid packaging a regex support framework) and use

[[NSWorkspace sharedWorkspace] selectFile:aFile inFileViewerRootedAtPath:thePath]

in a loop looping through an NSArray made from the entries returned by grep.

So far, I have looked at NSWorkspace, NSFileManager and NSApplication plus other keyword searches within the Xcode Documentation.

Thanks for checking out my question!

Andre

PS: I am not sure about the grep part, maybe I'll just slap RegexKit Lite in there...

like image 944
andreb Avatar asked Dec 23 '22 10:12

andreb


1 Answers

You can probably ask the Finder this via an AppleScript.

This* one-liner works for me:

osascript -e 'tell application "Finder" to set myname to POSIX path of (target of window 1 as alias)'

*a modified version of this.

like image 118
Rhythmic Fistman Avatar answered Dec 24 '22 23:12

Rhythmic Fistman