V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
FatGhosta
V2EX  ›  Apple

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

  •  
  •   FatGhosta · Aug 28, 2015 · 9160 views
    This topic created in 3906 days ago, the information mentioned may be changed or developed.

    问题的后半句是关键!!

    我想在 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 replies    2018-03-21 11:16:53 +08:00
    fatestigma
        1
    fatestigma  
       Aug 28, 2015   ❤️ 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  
       Aug 28, 2015   ❤️ 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
       Aug 28, 2015
    @fatestigma Mac 小白,不知道还有 AppleScript 这么个神器。。。
    pityonline
        4
    pityonline  
       Aug 28, 2015 via iPhone
    其实你需要的是 tmux ,只在一个终端里全搞定!
    ooonme
        5
    ooonme  
       Aug 28, 2015
    open -a Terminal
    ooonme
        6
    ooonme  
       Aug 28, 2015
    osx 里的
    ooonme
        7
    ooonme  
       Aug 28, 2015
    如果本身在 Termianl 执行 open -n -a Terminal
    FatGhosta
        8
    FatGhosta  
    OP
       Aug 29, 2015
    @ooonme exec 也可以,最重要的是能不能这个命令把在新 Terminal 中的要输入的命令带上。
    Creolophus
        9
    Creolophus  
       Jul 28, 2017
    @fatestigma set myterm to (make new terminal)
    Creolophus
        10
    Creolophus  
       Jul 28, 2017
    @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  
       Jul 28, 2017
    @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  
       Mar 21, 2018
    open -a Terminal bash.sh
    open -a iTerm bash.sh
    如果要新窗口 -n
    注意 bash.sh 需要有执行权限
    提前执行 sudo chmod +x bash.sh
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   990 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 19:33 · PVG 03:33 · LAX 12:33 · JFK 15:33
    ♥ Do have faith in what you're doing.