Answers:
这里有一个开放终端 AppleScript,您应该可以对其进行修改以调用iTerm。这个MacOSXHints帖子也应该有所帮助。
(我不在Mac上,否则我会对其进行测试。)
这个applescript对我有用:
-- script was opened by click in toolbar
on run
tell application "Finder"
try
set currFolder to (folder of the front window as string)
on error
set currFolder to (path to desktop folder as string)
end try
end tell
CD_to(currFolder, false)
end run
-- script run by draging file/folder to icon
on open (theList)
set newWindow to false
repeat with thePath in theList
set thePath to thePath as string
if not (thePath ends with ":") then
set x to the offset of ":" in (the reverse of every character of thePath) as string
set thePath to (characters 1 thru -(x) of thePath) as string
end if
CD_to(thePath, newWindow)
set newWindow to true -- create window for any other files/folders
end repeat
return
end open
-- cd to the desired directory in iterm
on CD_to(theDir, newWindow)
set theDir to quoted form of POSIX path of theDir as string
tell application "iTerm"
activate
delay 1
-- talk to the first terminal
try
set myterm to the first terminal
on error
set myterm to (make new terminal)
end try
tell myterm
try
-- launch a default shell in a new tab in the same terminal
launch session "Default Session"
on error
display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
end try
tell the last session
try
-- cd to the finder window
write text "cd " & theDir
on error
display dialog "There was an error cding to the finder window." buttons {"OK"}
end try
end tell
end tell
end tell
end CD_to
使用该页面上的其他答案,我创建了一个可以拖到finder任务栏中的应用程序。
您可以从这里下载:https : //github.com/rc1/iTermTo
从3.1.0版本开始,它内置在iTerm2中。
要使用该功能:
在Finder中,右键单击文件夹->服务->新建iTerm2窗口。
注意:Services
子菜单位于右键单击菜单的最底部。
参考
在此链接上,单击“ 显示旧版本”,然后在“ iTerm2 3.1.0”下单击“ 显示更改日志”并查找服务,您将发现:
添加对查找器服务的支持。您可以右键单击Finder以在该位置启动iTerm2。
看一下cdto
托管在https://github.com/jbtule/cdto上的项目
“ Finder Toolbar应用程序”,以在终端机(或iTerm,X11)中打开当前目录。此应用程序(包括其图标)设计为可放入取景器窗口的工具栏。”
出于完整性考虑,在找到此问题之前,对我有用的是:
Applescript Editor-> File-> Export-> File Format = .app
。.app
到Finder的工具栏。这将产生一个Finder工具栏按钮,该按钮将在新iTerm2
选项卡中打开当前目录。XtraFinder提供了这样的按钮,但它会打开新窗口。
在此处可以找到使用服务的类似解决方案,该解决方案链接到更多相关的AppleScript解决方案:
我改编的AppleScript是:
try
tell application "iTerm2"
tell the last terminal
launch session "Default Session"
tell the last session
tell i term application "Finder"
set cur_dir to (the target of the front Finder window) as string
end tell
set cur_dir to POSIX path of cur_dir
write text "cd " & cur_dir
end tell
end tell
end tell
end try
此解决方案在与按钮相关的线程中进行了评论。
感谢上面的iTermTo回答。
我猜这是因为iTerm的内部结构已发生变化,但是没有一种解决方案适合我。下面的代码是做什么的:
tell application "Finder"
set cur_dir to POSIX path of ((the target of the front Finder window) as string)
end tell
tell application "iTerm"
tell (create window with default profile)
write current session text "cd " & quoted form of cur_dir
end tell
end tell
或使用Automator作为查找器服务:
on run {input, parameters}
tell application "Finder"
set cur_dir to POSIX path of (input as string)
end tell
tell application "iTerm"
tell (create window with default profile)
write current session text "cd " & quoted form of cur_dir
end tell
end tell
end run
这是一个简化的脚本,该脚本始终打开一个新选项卡(如Bulljit的脚本):
try
tell application "Finder"
if number of Finder windows is 0 then
set p to POSIX path of (desktop as alias)
else
set p to POSIX path of (target of Finder window 1 as alias)
end if
end tell
tell application "iTerm"
reopen
tell current terminal
tell (launch session "Default Session")
write text "cd " & quoted form of p
end tell
end tell
activate
end tell
end try
如果您希望脚本重用现有的选项卡,请使用以下内容替换该tell current terminal
块:
tell current session of current terminal
write text "cd " & quoted form of p
end tell
但是,例如,如果当前会话很忙或者正在运行一个少或vim的进程,那将无法正常工作。
将脚本包装在try块中会使它静默失败。reopen
如果没有可见的窗口,或者例如仅打开首选项窗口,则打开一个新的终端窗口。Finder还具有一个insertion location
属性,通常target of Finder window 1
是桌面属性。但是在10.7及更高版本中存在一个错误,该错误通常指的是除最前面的窗口以外的其他窗口。
Bulljit脚本的一些潜在问题:
front window
(window 1
)的路径,该路径可以是信息窗口或首选项窗口。Finder window 1
始终是文件浏览器窗口。/
如果最前面的Finder窗口正在显示没有路径的视图(如“网络”视图),它将目录更改为。我更喜欢只使用这样的函数:
cf () {
c "$(osascript -e 'tell application "Finder"
POSIX path of (target of Finder window 1 as alias
end tell)' 2> /dev/null)"
}