Gary King Posted May 29, 2017 Share Posted May 29, 2017 If I have a Finder window open and in focus, how can I get that folder's location in a workflow that I run in Alfred? Is it possible at all? Either within Alfred or via any programming language? Link to comment
vitor Posted May 30, 2017 Share Posted May 30, 2017 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now