V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
FatGhosta
V2EX  ›  Apple

如何用 shell 开启一个新的 Terminal 或者 iTerm,并在其中执行指定命令??

  •  
  •   FatGhosta · 2015-08-28 18:15:22 +08:00 · 8210 次点击
    这是一个创建于 3156 天前的主题,其中的信息可能已经有所发展或是发生改变。

    问题的后半句是关键!!

    我想在 Terminal 里,用 shell 命令再调用出一个 Terminal ,再在这个刚启动的执行刚才 shell 命令中带的命令???
    我现在用exec /Applications/iTerm.app/Contents/MacOS/iTerm,在 iTerm 中启动新的 iTerm 。我想执行类似 exec /Applications/iTerm.app/Contents/MacOS/iTerm | xargs ls, 在新打开的 iTerm 中执行ls命令。这个命令显然不行。但是有没有这种命令??

    或者通过 swift 可以?

    12 条回复    2018-03-21 11:16:53 +08:00
    fatestigma
        1
    fatestigma  
       2015-08-28 18:44:20 +08:00   ❤️ 1
    你说的方法我不太会。
    但是 AppleScript 可以实现。对应的 shell 是 osascript -e;
    比如 iTerm 的:
    ```AppleScript
    osascript -e 'tell application "iTerm"
    set _term to (make new terminal )
    tell _term
    launch session "Default"
    set _session to current session
    end tell
    tell _session
    write text "ls"
    end tell
    end tell'
    ```
    fatestigma
        2
    fatestigma  
       2015-08-28 18:46:12 +08:00   ❤️ 1
    缩进怎么不见了。。另外倒数第三行那个双引号里的可以直接修改成你要执行的。
    ```
    osascript -e 'tell application "iTerm"
    set _term to (make new terminal )
    tell _term
    launch session "Default"
    set _session to current session
    end tell
    tell _session
    write text "ls"
    end tell
    end tell'
    ```
    FatGhosta
        3
    FatGhosta  
    OP
       2015-08-28 18:55:10 +08:00
    @fatestigma Mac 小白,不知道还有 AppleScript 这么个神器。。。
    pityonline
        4
    pityonline  
       2015-08-28 19:17:18 +08:00 via iPhone
    其实你需要的是 tmux ,只在一个终端里全搞定!
    ooonme
        5
    ooonme  
       2015-08-28 19:28:09 +08:00
    open -a Terminal
    ooonme
        6
    ooonme  
       2015-08-28 19:28:41 +08:00
    osx 里的
    ooonme
        7
    ooonme  
       2015-08-28 19:31:29 +08:00
    如果本身在 Termianl 执行 open -n -a Terminal
    FatGhosta
        8
    FatGhosta  
    OP
       2015-08-29 13:05:15 +08:00
    @ooonme exec 也可以,最重要的是能不能这个命令把在新 Terminal 中的要输入的命令带上。
    Creolophus
        9
    Creolophus  
       2017-07-28 11:33:15 +08:00
    @fatestigma set myterm to (make new terminal)
    Creolophus
        10
    Creolophus  
       2017-07-28 11:34:10 +08:00
    @fatestigma
    tell application "iTerm"
    activate
    set myterm to (make new terminal)
    end tell

    error "The variable terminal is not defined." number -2753 from "terminal"
    terminal 未定义是怎么回事啊
    fatestigma
        11
    fatestigma  
       2017-07-28 16:29:27 +08:00
    @Creolophus /这是一个创建于 699 天前的主题,其中的信息可能已经有所发展或是发生改变。/
    简单来说,就是 iTerm2 3.0 的 AppleScript 字典变了,terminal 关键字没有了。

    tell application "iTerm"
    create window with default profile
    tell current session of current window to write text "ls"
    end tell
    noogler67
        12
    noogler67  
       2018-03-21 11:16:53 +08:00
    open -a Terminal bash.sh
    open -a iTerm bash.sh
    如果要新窗口 -n
    注意 bash.sh 需要有执行权限
    提前执行 sudo chmod +x bash.sh
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5255 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 08:39 · PVG 16:39 · LAX 01:39 · JFK 04:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.