开发者

mac auto terminal commands?

Oh my mac, whenever I start terminal I want to run a few commands, open another tab, cd into a certain dir, 开发者_JAVA百科start a server on my local, and ssh into a remote server.

How do I do this automatically?

Thanks


You could write a shell script that does all that. And if you don't want to manually input your password when logging on to the SSH server, then you can put your key on the remote server as an authorized key not needing the password (if configured correctly of course).

This page shows how to use AppleScript to open terminal, give it focus, open 4 tabs etc. You could modify it for your purpose.

The following AppleScript activates the terminal, creates a new tab and executes "your_command" (the script that does what you want) in the second tab:

on menu_click(mList)
    local appName, topMenu, r

    -- Validate our input
    if mList's length < 3 then error "Menu list is not long enough"

    -- Set these variables for clarity and brevity later on
    set {appName, topMenu} to (items 1 through 2 of mList)
    set r to (items 3 through (mList's length) of mList)

    -- This overly-long line calls the menu_recurse function with
    -- two arguments: r, and a reference to the top-level menu
    tell application "System Events" to my menu_click_recurse(r, ((process appName)'s (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
    local f, r

    -- `f` = first item, `r` = rest of items
    set f to item 1 of mList
    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

    -- either actually click the menu item, or recurse again
    tell application "System Events"
        if mList's length is 1 then
            click parentObject's menu item f
        else
            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
        end if
    end tell
end menu_click_recurse

tell application "Terminal"
    activate
    my menu_click({"Terminal", "Shell", "New Tab", "Pro"})
    set window_id to id of first window whose frontmost is true
    do script "your_command" in tab 2 of window id window_id of application "Terminal"
end tell


Have a look at Terminitor. It's focus is on setting up the terminal per-project but config can be saved in a single file and run at will.

If this does not prove helpful, +1 for Applescript as mentioned by @netrom


Add the commands to your ~/.bashrc file and ensure it is runned each time you start the terminal:

Preferences > Shell > Startup > Mark: Run command and type: source ~/bashrc

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜