ChatGPT解决这个技术问题 Extra ChatGPT

在 Mac OS finder 中打开终端 [关闭]

关闭。这个问题是题外话。它目前不接受答案。 10年前关闭。锁定。这个问题及其答案被锁定,因为这个问题离题但具有历史意义。它目前不接受新的答案或交互。

是否有类似于 Mac OS 的“在此处打开命令窗口”Windows Powertoy 的东西?我通过谷歌搜索找到了几个插件,但想看看哪些插件最适合那里的开发人员。


p
pkamb

从 Mac OS X Lion 10.7 开始,终端完全包含此功能即服务。与大多数服务一样,这些默认情况下是禁用的,因此您需要启用它以使其显示在“服务”菜单中。

系统偏好设置 > 键盘 > 快捷方式 > 服务

在文件夹中启用新终端。文件夹中还有一个新的终端选项卡,它将在最前面的终端窗口中创建一个选项卡(如果有的话,它将创建一个新窗口)。这些服务适用于所有应用程序,而不仅仅是 Finder,它们对文件夹以及在文本中选择的绝对路径名进行操作。

您甚至可以为它们分配命令键。

服务出现在每个应用程序菜单的“服务”子菜单中,以及上下文菜单中(按住 Control 键单击或右键单击文件夹或路径名)。

当您在 Finder 中选择文件夹时,文件夹中的新终端服务将激活。您不能简单地打开文件夹并“就地”运行服务。返回父文件夹,选择相关文件夹,然后通过服务菜单或上下文菜单激活服务。

此外,如果您将文件夹(或路径名)拖到终端应用程序图标上,Lion Terminal 将打开一个新的终端窗口,您也可以拖到现有窗口的标签栏以创建新标签。

最后,如果您将文件夹或路径名拖到选项卡上(在选项卡栏中)并且前台进程是外壳程序,它将自动执行“cd”命令。 (拖动到选项卡中的终端视图仅会自行插入路径名,就像在旧版本的终端中一样。)

您也可以从命令行或 shell 脚本执行此操作:

open -a Terminal /path/to/folder

这是将文件夹/路径名拖到终端应用程序图标上的命令行等效项。

在相关的说明中,Lion Terminal 还具有用于查找手册页的新服务:在终端中打开手册页会在新的终端窗口中显示选定的手册页主题,并且在终端中搜索手册页对选定的文本执行“适当”。前者还理解手册页引用(“open(2)”)、手册页命令行参数(“2 open”)和手册页 URL(“x-man-page://2/open”)。


不适用于优胜美地。
@OlcayErtaş 什么不起作用?我的答案中的所有内容都适用于 Yosemite 10.10.x。
我必须重新启动 Finer 吗?我不知道为什么,但我在文件夹服务中启用了新终端,但它没有用。
@OlcayErtaş 您必须右键单击文件夹图标才能获取菜单项;不要导航到文件夹本身,而是导航到其父级。
我可以通过右键单击/服务使用它,但我不能通过键盘快捷键使用它。
j
jbtule

这个:

https://github.com/jbtule/cdto#cd-to

这是一个小应用程序,您可以将其拖入 Finder 工具栏,图标非常适合。它适用于终端、xterm(在 X11 下)、iterm。


P
Paul Alexander

我发现作为替代方案必不可少的一个应用程序是 DTerm,它实际上在您的应用程序中打开了一个迷你终端。此外,它适用于几乎所有的东西——Finder、XCode、PhotoShop 等。


P
PCheese

澄清(感谢@vgm64):如果您已经在终端中,这可以让您在不离开终端的情况下快速切换到最顶层的 Finder 窗口。这样,您可以避免使用鼠标。

我已将以下内容添加到我的 .bash_profile 中,因此我可以随时在终端中键入 cdff

function ff { osascript -e 'tell application "Finder"'\
 -e "if (${1-1} <= (count Finder windows)) then"\
 -e "get POSIX path of (target of window ${1-1} as alias)"\
 -e 'else' -e 'get POSIX path of (desktop as alias)'\
 -e 'end if' -e 'end tell'; };\

function cdff { cd "`ff $@`"; };

这是来自 this macosxhints.com Terminal hint


"open ." 在当前目录中打开一个新的 Finder 窗口
@jolvi 这与此答案提供的相反。重点不是在 pwd 打开 finder 窗口,而是在 cd 打开 finder 的目录。
啊,对不起,你当然是对的。
p
physicsmichael

查看Open Terminal Here。它可能与“在此处打开命令窗口”最相似。我使用了 >cdto,这非常相似,但这似乎在处理空格方面要好一些......但并不完美。

它非常棒的是能够“在应用程序启动时检测按键事件并使用它们来修改脚本的行为”,从而允许脚本在调用时在最前面的终端窗口中打开一个新选项卡按住 ⌘ 键。巧妙的把戏。

还要注意 PCheese 的回答;它可能对重度终端用户更有用!


p
pklaus

vgm64d0k 发布了一个非常漂亮和纤细的 Open Terminal Here 的更新版本。 james david low 做出了更改。他在 his site 上发布了新版本。只需下载 OpenTerminalHere.zip,解压缩,将包移动到 Library/Scripts 文件夹,然后将其从那里拖到 Finder 工具栏。

它的特别之处在于,如果 Terminal.app 窗口已经打开,它总是会打开一个新选项卡。很有用!我还注意到,应用程序按钮的样式比 redacted 发布的 cdto 更适合 Snow Leopard Finder.app 样式。


这太棒了!请注意,您必须使用 Command+Drag(而不是像上面帖子中所述的那样将其拖动)到 Finder 工具栏。然后你有一个图标快捷方式,单击该快捷方式将从任何文件夹终止。
H
Henrik Hartz

此外,您可以使用 command-C 从查找器中复制项目,跳转到终端(例如使用 Spotlight 或 QuickSilver)输入“cd”并简单地使用 command-v 粘贴


s
stigi

我为查找工具栏创建了一个包含 3 个应用程序的捆绑包。其他两个应用程序:

使用当前选择打开 Textmate

使用当前文件夹打开 GitX

有关详细信息,请参见此处:http://nslog.de/posts/71


i
iayork

如果您安装 Big Cat Scripts (http://www.ranchero.com/bigcat/),您可以添加自己的上下文菜单(右键单击)项目。我不认为它带有 Open Terminal Here applescript,但我使用这个脚本(老实说,我不记得是我自己写的,还是从别人的例子中提取的):

on main(filelist)
    tell application "Finder"
        try
            activate
            set frontWin to folder of front window as string
            set frontWinPath to (get POSIX path of frontWin)
            tell application "Terminal"
                activate
                do script with command "cd \"" & frontWinPath & "\""
            end tell
        on error error_message
            beep
            display dialog error_message buttons ¬
                {"OK"} default button 1
        end try
    end tell
end main

类似的脚本还可以在右键单击时为您提供文件的完整路径,我发现这更加有用。


佚名

它比你要求的要多一点,但我推荐 Cocoatech 的路径查找器给任何希望查找器有更多汁液的人。它包括一个用于打开当前目录的终端窗口的工具栏按钮,或在每个 Finder 窗口底部带有终端命令行的可伸缩窗格。加上我现在不能没有的许多其他功能。非常成熟、稳定的软件。 http://cocoatech.com/


J
Jonatan Lindén

好的,我意识到这有点晚了......也许在写这篇文章的时候这个替代方案不可用?

无论如何,我发现通过 Fink 安装 pos 包(在这种情况下是先决条件,也许对于那些使用 MacPorts 的人来说有类似的东西?)是最简单的解决方案。你得到两个命令:

posd - 给出最前面的 Finder 窗口的当前目录(您可能为其创建别名 cdf=cd posd) fdc - 将最前面的 Finder 窗口的当前目录切换到终端 pwd。这与 'open ' 略有不同。它总是打开一个新的查找器窗口。

是的,您必须在编写 cdf 之前切换到终端窗口,但我想这与单击 Finder 工具栏中的按钮相比相当便宜。它也适用于 iTerm,您不必下载单独的 Finder 工具栏按钮来打开 iTerm 窗口。这与 PCheese 提出的方法相同,但您不必弄乱 .bash_profile。


j
joachim

如果您像我一样关闭 Finder 工具栏,此服务会在每个文件夹的上下文菜单中添加一个项目:http://blog.leenarts.net/2009/09/03/open-service-here/

这还允许您打开在 Finder 树视图中看到的任何文件夹。


L
Lri

我主要使用这个功能:

cf() {
  cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"
}

您还可以为脚本分配一个快捷方式,如下所示。

重用现有选项卡或创建新窗口(终端):

tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "Terminal"
    if (exists window 1) and not busy of window 1 then
        do script "cd " & quoted form of p in window 1
    else
        do script "cd " & quoted form of p
    end if
    activate
end tell

重用现有选项卡或创建新选项卡(终端):

tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "Terminal"
    if not (exists window 1) then reopen
    activate
    if busy of window 1 then
        tell application "System Events" to keystroke "t" using command down
    end if
    do script "cd " & quoted form of p in window 1
end tell

始终创建一个新选项卡(iTerm 2):

tell application "Finder" to set p to POSIX path of (insertion location as alias)
tell application "iTerm"
    if exists current terminal then
        current terminal
    else
        make new terminal
    end if
    tell (launch session "Default") of result to write text "cd " & quoted form of p
    activate
end tell

与 10.7 中添加的服务相比,前两个脚本有两个优点:

他们使用标题栏上的文件夹,而不是要求您先选择一个文件夹。

如果最前面的选项卡不忙,它们会重用它,例如运行命令、显示手册页或运行 emacs。


M
Master Bee

OSX 10.6 上的 AppleScript 中有一个错误。 (2 个终端窗口打开)。我通过在激活后添加关闭命令来解决这个问题。这将关闭第一个终端窗口。

on run
    tell application "Finder"
        try
            activate
            set frontWin to folder of front window as string
            set frontWinPath to (get POSIX path of frontWin)
            tell application "Terminal"
                activate
                close
                do script with command "cd \"" & frontWinPath & "\""
            end tell
        on error error_message
            beep
            display dialog error_message buttons ¬
                {"OK"} default button 1
        end try
    end tell
end run

为什么要关闭终端窗口?这不会破坏你在其中所做的任何工作吗?