Hammerspoon get working directory from finder
Here's some AppleScript for you:

tell application "Finder" to get POSIX path of (insertion location as string)


If you don't want to get the Desktop if a Finder window isn't active, use this instead:

tell application "Finder" to get POSIX path of (target of front window as string)


This will get the location of the frontmost Finder window, even if none is selected. If there are none open, it will fail.

I don't know how Hammerspoon works, but if it can run Terminal commands, you can use osascript to run the AppleScript.



Also, you don't need to use Hammerspoon to do this. There's already a Service for this, or you can use Automator to make your own:


Create a new Service.
Make it take No Input in Finder.app.
Add a Run AppleScript action, and put this in the box:


on run {input, parameters}

    tell application "Finder" to set p to POSIX path of (insertion location as string)
    tell application "Terminal" to do script "cd " & quoted form of p

    return input
end run


Save it appropriately, like "New Terminal Here".
Add a keyboard shortcut in System Preferences, if desired.
Enjoy!