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

OS X 上开一个新 terminal 窗口拿 exit code 的问题

  •  
  •   ooops · 2016-05-03 11:35:01 +08:00 · 2835 次点击
    这是一个创建于 2886 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想在 git 的 pre-commit hook 里面执行一段命令,目前想新建一个窗口去执行和输出,同时还想在执行完毕后拿到 exit code 做为 pre-commit 的 exit code ,现在的问题是我貌似拿不到这个新窗口执行的 exit code 。有什么办法么?

    新建窗口使用的是在 pre-commit 的 shell 环境调用 Apple Scirpt: to do script with command "shell commmand" 但是没找到这个 shell commnad 的 exit code 如何获取回到 pre-commit 的环境中再 exit 这个 code 。 google 了好多,仍然没有找到答案 T.T 有别的思路和方法也可以提 一下,感谢

    主要代码如下

    r=$(osascript<<EOF
    tell application "Terminal"
    	activate
    
    	set t to do script with command "cd $pwd; set -o pipefail && xcodebuild -workspace $workspace -scheme $scheme -sdk $sdk -destination $destination test | xcpretty"
    
    	tell front window
    		set position to {150, 150}
    		set size to {1100, 700}
    	end tell
    
    	repeat
    		delay 1
    		if not busy of t then exit repeat
    	end repeat
    
    	display notification "switching to terminal..." with title "Unit Test Finished" sound name "Glass"
    
    	activate
    end tell
    EOF)
    
    5 条回复    2016-05-07 02:41:14 +08:00
    ooops
        1
    ooops  
    OP
       2016-05-03 13:28:21 +08:00
    貌似发错节点了,怎么移动到程序员那里?
    nevermlnd
        2
    nevermlnd  
       2016-05-03 14:24:16 +08:00
    command + T ?
    ooops
        3
    ooops  
    OP
       2016-05-03 23:37:25 +08:00
    @nevermlnd 感谢回复,但是能详细说一下么。
    用 osascript 新开 tab 也是可以的,但是拿不到 handler 也没法让 shell 代码运行在新 tab 里吧,最主要的还是拿到运行完的 exit code 。
    ooops
        4
    ooops  
    OP
       2016-05-06 18:34:14 +08:00
    有人么,有人帮移到相对应的节点下么
    ooops
        5
    ooops  
    OP
       2016-05-07 02:41:14 +08:00
    想到一种不是很优雅的方法是用 mac 的剪切板。

    执行命令时使用
    ```
    set t to do script test_script in selected tab of the front window
    ```
    重点在于 in selected tab of the front window ,使后面的$?的命令可以在同一个窗口执行
    但是使用 do script 依旧拿不到 exit code ,所以使用`pbcopy`那个$?,然后再执行一个`pbpaste`到 apple script 的变量,返回该变量

    勉强实现。

    有人 有更好的方法希望不吝赐教。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   942 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:55 · PVG 04:55 · LAX 13:55 · JFK 16:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.