如何打开已 cd 到的特定路径的终端?

发布于 2024-09-12 16:52:17 字数 185 浏览 6 评论 0 原文

如何使用终端打开另一个终端窗口,但使用我指定的路径?

当我开始工作时,我正在使用自动化程序加载我的工作内容,但我需要知道如何执行此操作:

打开终端并输入:
• cd 工作/公司/项目/
• script/server

然后在该终端窗口中创建新选项卡并 cd 到同一文件夹。

How do I use the terminal to open another terminal window but with a path I specify?

I am using automator to load my work stuff when I get to work, but I need to know how to do this:

Open Terminal and Type:
• cd Work/Company/Project/
• script/server

And then new tab in that terminal window and cd to the same folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痕至 2024-09-19 16:52:18

这会从 Mac OSX 上的命令提示符打开一个新的终端窗口,执行“cd /”,然后将窗口保持在顶部:

osascript -e 'tell application "terminal"' -e 'do script "cd /"' -e 'end tell'

您可以将其放入这样的脚本中:

#!/bin/sh
osascript -e 'tell application "terminal"' -e "do script \"cd $1\"" -e 'end tell'

希望这会有所帮助。

This opens a new terminal window from a command prompt on Mac OSX , executes "cd /" and then keeps the window on top:

osascript -e 'tell application "terminal"' -e 'do script "cd /"' -e 'end tell'

You can put this into a script like this:

#!/bin/sh
osascript -e 'tell application "terminal"' -e "do script \"cd $1\"" -e 'end tell'

Hope this helps.

放我走吧 2024-09-19 16:52:18

使用 applescript 来执行此操作。

例如 在此处打开终端

Use an applescript to do this.

e.g. Open Terminal Here

浪菊怪哟 2024-09-19 16:52:18

您可以编写一个 shell 脚本来 cd 到该目录

因此编写一个执行 cd /user/music 或类似内容的脚本,将其另存为 myscript.sh 并使用 chmod +x myscript.sh 运行它。

这个 资源非常有帮助

You can write a shell script to cd to that directory

So write a script that executes something like cd /user/music or something like that, save it as myscript.sh and run it using chmod +x myscript.sh.

This resource from the OS X developer network is pretty helpful

智商已欠费 2024-09-19 16:52:18

下面的两个脚本一起处理常见场景:

1) 如果终端已在运行,请打开一个新的终端窗口并在其中运行“cd mydir”

2) 如果终端尚未运行,请使用终端生成的初始窗口(窗口 0 ),而不是烦人地启动第二个窗口

注意:不太完美的是,如果终端打开了多个窗口,所有这些窗口都会被带到前面,与任何其他应用程序重叠。仅将最后一个终端窗口提升到前面的解决方案似乎需要 AppleScriptObjC 的黑魔法 - 参考如下:

https://apple.stackexchange.com/questions/39204/script-to-raise-a-single-window-to-the-front
http://tom.scogland.com /blog/2013/06/08/mac-raise-window-by-title/

脚本 1 - 打开文本编辑器并另存为:

/usr/local/bin/terminal-here.sh

#!/bin/sh
osascript `dirname $0`/terminal-here.scpt $1 > /dev/null 2> /dev/null

脚本 2 - 打开“AppleScript 编辑器”,粘贴下面的内容并另存为:

/usr/local/bin/terminal-here.scpt

# AppleScript to cd (change directory) to a path passed as an argument

# If Terminal.app is running, the script will open a new window and cd to the path
# If Terminal.app is NOT running, we'll use the window that Terminal opens automatically on launch

# Run script with passed arguments (if any)
on run argv
    if (count of argv) > 0 then
        # There was an argument passed so consider it to be the path
        set mypath to item 1 of argv
    else
        # Since no argument was passed, default to the home directory
        set mypath to "~"
    end if
    tell application "System Events"
        if (count (processes whose bundle identifier is "com.apple.Terminal")) is 0 then
            # Terminal isn't running so we'll make sure to run the 'cd' in Terminal's first window (0)
            tell application "/Applications/Utilities/Terminal.app"
                # Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
                do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo" in window 0
                activate last window
            end tell
        else
            # Terminal is already running so we'll let it open a new window for our 'cd' command
            tell application "/Applications/Utilities/Terminal.app"
                # Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
                do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo"
                activate last window
            end tell
        end if
    end tell
end run

The two scripts below together handle the common scenarios:

1) If Terminal is already running, open a new terminal window and run the 'cd mydir' there

2) If terminal is not already running, use the initial window that Terminal spawns (window 0), rather than annoyingly launching a second window

NOTE: what's not quite perfect is if Terminal has several windows open, all of them will be brought to the front, overlapping any other apps. A solution to raising only the last terminal window to the front appears to require the black magic of AppleScriptObjC - references below:

https://apple.stackexchange.com/questions/39204/script-to-raise-a-single-window-to-the-front
http://tom.scogland.com/blog/2013/06/08/mac-raise-window-by-title/

Script 1 - open a text editor and save as:

/usr/local/bin/terminal-here.sh

#!/bin/sh
osascript `dirname $0`/terminal-here.scpt $1 > /dev/null 2> /dev/null

Script 2 - open 'AppleScript Editor', paste contents below and save as:

/usr/local/bin/terminal-here.scpt

# AppleScript to cd (change directory) to a path passed as an argument

# If Terminal.app is running, the script will open a new window and cd to the path
# If Terminal.app is NOT running, we'll use the window that Terminal opens automatically on launch

# Run script with passed arguments (if any)
on run argv
    if (count of argv) > 0 then
        # There was an argument passed so consider it to be the path
        set mypath to item 1 of argv
    else
        # Since no argument was passed, default to the home directory
        set mypath to "~"
    end if
    tell application "System Events"
        if (count (processes whose bundle identifier is "com.apple.Terminal")) is 0 then
            # Terminal isn't running so we'll make sure to run the 'cd' in Terminal's first window (0)
            tell application "/Applications/Utilities/Terminal.app"
                # Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
                do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo" in window 0
                activate last window
            end tell
        else
            # Terminal is already running so we'll let it open a new window for our 'cd' command
            tell application "/Applications/Utilities/Terminal.app"
                # Turn off echo, run the 'cd', clear screen, empty the scrollback, re-enable echo
                do script "stty -echo; cd " & (mypath as text) & ";clear; printf \"\\e[3J\"; stty echo"
                activate last window
            end tell
        end if
    end tell
end run
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文