Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript to open Terminal in **current** folder when clicking anywhere in Finder?

I already have a New Terminal at Folder service when I right click on a folder in Finder.

Now I would also like to be able to right click anywhere in a Finder window (i.e. on the whitespace surrounding the files and folders), and have the option to New Terminal here.

I have the following Automator script (Run AppleScript with no input in Finder) right now:

tell application "Finder"
    set this_folder to (folder of the front Finder window) as alias
end tell

tell application "Terminal"
    activate
    do script "cd \"" & (POSIX path of (this_folder as string)) & "\""
end tell

This works when I right click on a folder - it opens a new Terminal window in the folder I'm standing in (not the one I right clicked).

Now, is it possible to get this service to appear when right clicking on the whitespace (or at least on files as well as folders)?

EDIT

I just found this app that can be placed in the Finder toolbar and does exactly what I need. However, it would still be interesting to know if it is possible to achieve using a service.

like image 616
Magnus Avatar asked Oct 30 '25 09:10

Magnus


1 Answers

-- script: New Terminal at selected Folder

tell application "Finder"
    set selectedItem to item 1 of (get selection)
    set isFolder to class of selectedItem is folder
end tell

-- open selected item in the Terminal, if it is folder type
if isFolder then
    set posixPath to POSIX path of (selectedItem as alias)
    tell application "Terminal"
        activate
        set a to do script "cd " & quoted form of posixPath
    end tell
else
    display alert "Selected item is not directory"
end if
like image 95
Robert Kniazidis Avatar answered Nov 02 '25 23:11

Robert Kniazidis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!