Getting the POSIX Path in the Finder Window

How can I get the POSIX path in the Finder window, which is now at the top of the window list? Preferably with some kind of Cocoa structure, but I'm open to anything.

Background: I will need this because I want to make a selection of files matching the regular expression pattern that will start recursively from this path. The idea is to use

[[NSWorkspace sharedWorkspace] subpathsAtPath:thePath]

to get all the descendants of this path, use "grep" in NSTask (not to pack the regular expression support framework) and use

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

in a loop passing through NSArray from the records returned by grep.

So far I have covered NSWorkspace, NSFileManager, and NSApplication and other keyword searches in the Xcode documentation.

Thanks for finding out my question!

Andre

PS: grep, , RegexKit Lite ...

+3
4

, , , . , , , , .

Apple Script

3 , Finder, . Apple Script, .

Carbon

(- Apple Script ), CGWindowListCopyWindowInfo, :

, . , , . , , " ", .

Apple Script Cocoa

Apple Script, 3, , , , .

  • Apple Event Manager Apple. , , .

  • NSAppleScript, . , Apple Script .

  • Scripting Bridge, , . , . . , , SB, .

Objective C Apple Script, AppScript. - , , , .

, Apple Script, , .

, . , , , , .

+3

, Finder AppleScript.

* :

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

* this.

+5

Getting the POSIX path represented by the front-most window in Finder will involve sending Apple events to Finder anyway.

Your choices include:

  • Use Apple Event Manager (or NSAppleEventDescriptor and friends) directly.
  • Using NSAppleScript or OSAScript objects.
  • Using ScriptingBridge.

If you are building a Cocoa application, OSAScript is probably the most natural choice.

+2
source

Can be used ScriptingBridgewith Swift 4

import Foundation
import ScriptingBridge // imports: ScriptingBridge.SBApplication, .SBElementArray, .SBObject

// SBApplication? to SBApplication to FinderApplication
let finder = (SBApplication(bundleIdentifier: "com.apple.finder")!) as FinderApplication

let windowList: SBElementArray = finder.windows!()
guard windowList.count > 0,
    let window = windowList[0] as? FinderWindow,
    let windowProperties = window.properties,
    let windowTarget = windowProperties["target"] as? FinderFolder,
    let windowUrlOptionalStr = windowTarget.URL,
    let windowUrlSubStr = windowUrlOptionalStr
        .removingPercentEncoding?
        .dropFirst(7) // "file://" 7 characters
else {
    // … handle failure
}

let windowUrl = URL(
    fileURLWithPath: String(windowUrlSubStr),
    isDirectory: true,
    relativeTo: nil)
0
source

Source: https://habr.com/ru/post/1711337/


All Articles