[Terminal]スクリプト実行後終了したらTerminalも終了する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # com.cocolog-nifty.quicktimer.icefloe |
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use scripting additions |
009 | |
010 | property refMe : a reference to current application |
011 | |
012 | |
013 | #実行するコマンド |
014 | set strCommandText to ("~/bin/topgrade/topgrade") as text |
015 | #プロセス名の判定用 |
016 | set strProcesseName to ("topgrade") as text |
017 | |
018 | tell application id "com.apple.Terminal" |
019 | #起動 を先にすることで新規Windowを作らない |
020 | launch |
021 | #前面に |
022 | activate |
023 | end tell |
024 | |
025 | #ターミナル |
026 | tell application "Terminal" |
027 | set numCntWindow to (count of every window) as integer |
028 | if numCntWindow = 0 then |
029 | #ウィンドウが無いなら |
030 | #新規Windowでシェルを実行 |
031 | set objWindowID to (do script "/bin/bash\n\n") |
032 | set boolBusy to false as boolean |
033 | delay 1 |
034 | else |
035 | #ウィンドウがある場合 |
036 | tell front window |
037 | tell front tab |
038 | #前面のタブが処理中か?確認して |
039 | set boolBusy to busy as boolean |
040 | end tell |
041 | end tell |
042 | #処理中なら新規windowを作る |
043 | if boolBusy is true then |
044 | set objWindowID to (do script "/bin/bash\n\n") |
045 | else |
046 | #処理中では無いならそのWindowを使う |
047 | tell front window |
048 | set objWindowID to front tab |
049 | end tell |
050 | end if |
051 | end if |
052 | |
053 | #WidnowのIDを取得 |
054 | do script strCommandText in objWindowID |
055 | #確実にウィンドウが出来るのをまつ |
056 | delay 1 |
057 | tell front window |
058 | tell front tab |
059 | #プロセスが終わるまで繰り返し |
060 | repeat |
061 | #フロントタブで実行中のプロセス |
062 | set listProcesses to processes as list |
063 | set ocidProcessArray to refMe's NSArray's arrayWithArray:(listProcesses) |
064 | #終了判定 |
065 | set boolContain to (ocidProcessArray's containsObject:(strProcesseName)) as boolean |
066 | #終了判定が YESなら |
067 | if boolContain is false then |
068 | #リピートを抜けて終了処理に |
069 | exit repeat |
070 | else if boolContain is true then |
071 | #終了判定がNO |
072 | set strTitle to custom title as text |
073 | #ここはカスタマイズが必要 |
074 | if strTitle contains "Awaiting user" then |
075 | #ユーザー応答が必要になった場合は終了 |
076 | exit repeat |
077 | end if |
078 | delay 1 |
079 | end if |
080 | end repeat |
081 | #コマンドが終了したら |
082 | do script "\n\n" in objWindowID |
083 | #シェルをEXITして BASHを抜ける |
084 | do script "exit" in objWindowID |
085 | #コマンドが終了したら |
086 | do script "\n\n" in objWindowID |
087 | #シェルをEXITして ZSHを抜ける |
088 | do script "exit" in objWindowID |
089 | delay 1 |
090 | end tell |
091 | end tell |
092 | close front window saving no |
093 | set numCntWindow to (count of every window) as integer |
094 | if boolBusy is false then |
095 | repeat with itemWindowID from 1 to (numCntWindow) by 1 |
096 | close window itemWindowID saving no |
097 | end repeat |
098 | try |
099 | quit |
100 | end try |
101 | end if |
102 | end tell |
103 | |
AppleScriptで生成しました |
| 固定リンク
「Terminal」カテゴリの記事
- [Terminal]新規Windowでコマンドを実行するまでの一連の流れ(2025.01.13)
- ターミナル終了(複数Window 複数tab時)(2024.12.30)
- ターミナル終了(単Window時)(2024.12.30)
- ターミナル 実行中のプロセスの終了とウィンドウを閉じる(2024.11.24)
- [Terminal]スクリプト実行後終了したらTerminalも終了する(2024.11.14)