macOS Finder finds the current directory and opens the terminal

insert image description here

insert image description here

In Windows 11, you can right-click any folder in [This Computer], and you may directly open [Terminal] in the current directory. It feels very convenient to use for a long time.
As in macOS, I would also like to get similar functionality. How to achieve it is documented.

Actual combat process

My actual combat environment is:
Operating system: macOS Venture 13.4
Terminal: iTerm2

iTermHere.scpt

Create a script using any text editor you like:
~/Library/Scripts/iTermHere.scpt
the script path can be saved anywhere, just choose a path you like.
The content of the script is as follows:


(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3+
     
    Written by Brian Schlining
	*)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run
	tell application "Finder"
		try
			set this_folder to (the target of the front window) as alias
		on error
			set this_folder to startup disk
		end try
		
		my process_item(this_folder)
		
	end tell
end run


-- This handler processes folders dropped onto the toolbar script icon 
-- 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		my process_item(this_item)
	end repeat
end open


-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
	
	set thePath to quoted form of POSIX path of this_item
	set theCmd to "cd " & thePath & ";clear;"
	
	tell application "iTerm"
		activate
		-- just open a terminal and cd to thePath
		--set newWindow to (create window with default profile)
		
		set aWindow to current window
		
		if aWindow is equal to missing value then
			set aWindow to (create window with default profile)
		else
			tell aWindow
				set t to (create tab with default profile)
			end tell
		end if
		
		
		tell current session of aWindow
			write text "cd " & thePath & ";clear;"
		end tell
		
	end tell
	
	
end process_item

The script compiler opens the script

I can use the script editor to open the script by double-clicking the file directly.
insert image description here

export to program

file ==> export...
insert image description here

File Format: Application
Location: Application
Code Signing: Sign to Run Locally
insert image description here
After clicking Save, the program iTermHere will appear in the application.
insert image description here

define icon

You can select the program, press ⌘+ithe display profile, here we can modify the icon. If you have icon resources, just drag them here.
insert image description here
I want to use the same icon as iTerm here, you can copy his icon as follows:
⌘+c in the upper left corner to copy, ⌘+v to paste.

insert image description here
In this way, we can customize the icon.
If you don't like the name, you can also rename it. I will change it to [Open in Terminal].
insert image description here

Add to Finder toolbar

Select from the right-click menu of the mouse in the Finder 自定义工具栏...
insert image description here
, then restart a Finder and open the application, and drag [Open in Terminal] to the small box at the top with the mouse.
insert image description here
The final effect is as follows:
1. When the label is displayed
insert image description here
2. When only the icon is displayed
insert image description here

Supplement 1: iTerm2 comes with

After completing the above steps, I accidentally discovered that iTerm2 itself also has its own type function.
The operation is as follows:
insert image description here

Indeed, it can be done, but the operation is a little cumbersome.
We only need to click the left mouse button to complete the above method. And this method will

  1. right click
  2. selected service

  3. It takes two extra steps to click on the relevant menu with the left button , but fortunately, this function is available by default, so there is no need to toss.

Supplement 2: macOS comes with

This is my problem, I later found out that macOS itself also has this function. The operation is as follows:
insert image description here
It turns out that it has always been there, but I don't know it.

Summarize

The operation is a little troublesome, but it can be done once and for all. It is still very convenient to use. And through this method, we can do more custom toolbars, such as: use vscode to open this directory and the like.

reference

《Integrate iTerm2 v.3 with Your Mac’s Finder》
https://schlining.medium.com/integrate-iterm2-v-3-with-your-macs-finder-f3825acd3e0b

Guess you like

Origin blog.csdn.net/lxyoucan/article/details/131579437