Jump to content

Get the folder currently in focus in Finder


Recommended Posts

It’s possible with AppleScript and JavaScript for Automation. Here’s code for both languages.


With this method, if any Finder window is open it’ll return the path to the frontmost one, else it’ll return the path to the desktop.


AppleScript:

tell application "Finder" to return (quoted form of POSIX path of (insertion location as alias))


JavaScript:

unescape(Application("Finder").insertionLocation().url()).slice(7).slice(0, -1)


With this method, if a Finder window is the frontmost window app it’ll return the path of the frontmost one, else it’ll return the path to the home directory.


AppleScript:

tell application "System Events"
    if (name of first process whose frontmost is true) is "Finder" then
        tell application "Finder" to return (POSIX path of (folder of the front window as alias))
    else
        return (POSIX path of (path to home folder))
    end if
end tell


JavaScript:

if Application("Finder").frontmost() {
    unescape(Application("Finder").finderWindows[0].target.url()).slice(7).slice(0, -1)
} else {
    unescape(Application("Finder").home.url()).slice(7).slice(0, -1)
}

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...