Terminal: CD into the Active Finder Window's Folder

Hey Guys,

Thread:

Open a new Terminal window for the current Finder folder

My post -- CD into the front Finder window

Latest script:


CD to the frontmost folder in the Finder.


I run this one with O in FastScripts. (Seriously fast.)

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2011/10/16 02:11
# dMod: 2018/04/26 00:28
# Appl: Terminal
# Task: CD to Front Finder Window's Directory – if tab is busy then create a new window.
#     : Normally run from FastScripts - https://www.red-sweater.com/fastscripts/
# Libs: None
# Osax: None 
# Tags: @Applescript, @Terminal, @CD, @Front, @Finder, @Directory, @Insertion_Location
----------------------------------------------------------------

# Normal script has no error-handler due to FastScripts' better error dialog.

# Error-handling added for this posting of the script.

----------------------------------------------------------------

try
   
   tell application "Finder"
      if (front window exists) and (front window's name starts with "Searching “") then
         error "Error:" & linefeed & linefeed & "Front window is a Spotlight Search window!"
      else
         set tScript to "cd " & quoted form of (POSIX path of (insertion location as text))
      end if
   end tell
   
   tell application "Terminal"
      tell selected tab of front window
         if its busy = false then
            do script tScript in it
         else
            do script tScript
         end if
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

----------------------------------------------------------------


CD to the selected folder in the Finder.


I run this one with O in FastScripts.

I should probably support a selected file in addition to a selected folder, but I haven't yet bothered.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2011/10/16 02:11
# dMod: 2018/04/07 23:14
# Appl: Terminal
# Task: CD to selected folder or app in the Finder – if tab is busy then create a new window.
#     : Normally run from FastScripts - https://www.red-sweater.com/fastscripts/
# Libs: None
# Osax: None 
# Tags: @Applescript, @Terminal, @CD, @Front, @Finder, @App, @Directory
----------------------------------------------------------------

# Normal script has no error-handler due to FastScripts' better error dialog.

# Error-handling added for this posting of the script.

----------------------------------------------------------------

try
   
   tell application "Finder"
      
      set finderSelectionList to selection as alias list
      if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
      set theItem to item 1 of finderSelectionList
      
      if kind of (item theItem) is in {"Application", "Folder"} then
         set itemPath to POSIX path of (theItem as text)
      else
         error "Error:" & linefeed & linefeed & "Improper item selected in the Finder!"
      end if
      
   end tell
   
   set shCMD to "cd " & quoted form of itemPath
   
   tell application "Terminal"
      tell selected tab of front window
         if its busy = false then
            do script shCMD in it
         else
            do script shCMD
         end if
      end tell
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

----------------------------------------------------------------


Bash Function to CD into the Front Finder Window:


NOTE – this is a modification for your shell environment and must be placed in:

~/.profile

OR

~/.bash_profile

Personally I use the .profile file.

Once installed you have to re-source any open Terminal windows — OR — restart the Terminal app — to make it see the changes made to the profile.

Then just type ff<return> in any Terminal window to get to the front Finder window.

#====================================================================

# Task: cd the Terminal into the front Finder Window
# dCre: 2014/10/15 10:16
# dMod: 2018/04/26 00:37

function ff {
DIR=$(osascript -e '
tell application "Finder"
   if (count Finder windows) > 0 then
      if name of front window does not start with "Searching" then
         set _path to target of front window as alias
      else
         error "Front window is a Spotlight Search window!"
      end if
   else
      set _path to path to desktop
   end if
end tell
set _path to (POSIX path of _path)
');
cd "$DIR";
}

#====================================================================

-Chris

1 Like