« ターミナルにCTRL+C コントロール+Cを送る | トップページ | [NSString]空のテキストファイルを作る writeToFile:atomically:encoding:error: »

ターミナル ウィンドウを全部閉じる

【スクリプトエディタで開く】


##起動
tell application "Terminal" to launch

##起動確認 最大10秒
repeat 10 times
  tell application "Terminal"
    activate
    set boolFrontMost to frontmost as boolean
  end tell
  if boolFrontMost is false then
    delay 1
  else
    exit repeat
  end if
end repeat

###ウィンドウがあるか?
tell application "Terminal"
  set numCntWindow to (count of every window) as integer
end tell

###windowがある場合は先に閉じる 強制的に全部閉じる
###処理中のプロセスにもCTRL+Cを送って中止させます
if numCntWindow ≠ 0 then
  repeat numCntWindow times
    ####CTRL+Cを送って
    tell application "System Events"
      tell process "Terminal"
        tell front window
          keystroke "c" using {control down}
          keystroke "c" using {control down}
        end tell
      end tell
    end tell
    ####exitしてからCLOSEする
    tell application "Terminal"
      ##root対策で2回
      do script "exit" in front window
      do script "exit" in front window
      tell front window
        close saving no
      end tell
    end tell
  end repeat
end if

###新しいウィンドウを作る
tell application "Terminal"
  set refWindow1 to do script "\n\n"
  log refWindow1
end tell

###新しいウィンドウを作る
tell application "Terminal"
  set refWindow2 to do script "\n\n"
  log refWindow2
end tell

###WINDOWを指定して実行する
tell application "Terminal"
  do script "date" in refWindow1
end tell

tell application "Terminal"
  do script "time" in refWindow2
end tell

|

« ターミナルにCTRL+C コントロール+Cを送る | トップページ | [NSString]空のテキストファイルを作る writeToFile:atomically:encoding:error: »

Terminal」カテゴリの記事