开发者

How do you open a terminal with a specific path already cd'ed to?

开发者 https://www.devze.com 2023-01-10 02:48 出处:网络
How do I use开发者_运维问答 the terminal to open another terminal window but with a path I specify?

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.


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.


Use an applescript to do this.

e.g. Open Terminal Here


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消