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


当前回答

我主要使用这个函数:

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,他们就会重用该选项卡。

其他回答

这样的:

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

这是一个小的应用程序,你可以拖到Finder工具栏中,图标非常合适。它使用Terminal, xterm(在X11下),term。

如果你安装了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

类似的脚本还可以通过右键单击获得文件的完整路径,我发现这更有用。

查看这里的开放终端。它可能与“在这里打开命令窗口”最相似。我使用>cdto,这是非常相似的,但这似乎是更好地处理空格…但并不完美。

它具有的非常好的功能是“在应用程序开始时检测键下事件并使用它们修改脚本的行为”,允许脚本在按住⌘键调用时在最前面的终端窗口中打开一个新选项卡。整洁的技巧。

还要注意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 $@`"; };

这是macosxhints.com的终端提示。

我主要使用这个函数:

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,他们就会重用该选项卡。