« [Terminal]ターミナルを終了させる | トップページ | フォルダ判定 »

スリープさせない


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

property refNSNotFound : a reference to 9.22337203685477E+18 + 5807


################################
######ペーストボードテキスト取得
################################
##初期化
set ocidPasteboard to refMe's NSPasteboard's generalPasteboard()
##可変テキストとして受け取る
set ocidPasteboardArray to ocidPasteboard's readObjectsForClasses:({refMe's NSMutableString}) options:(missing value)
##シングルARRAYが空ならペーストボードの中身が画像とか他のものなので初期値を作る
if (count of ocidPasteboardArray) = 0 then
  ###初期値
  set ocidPasteboardStrings to 7200 as text
else
  ###ペーストボードの中身をテキストで確定
  set strPasteboardStrings to (ocidPasteboardArray's objectAtIndex:0) as text
end if
################################
######ダイアログ
################################
set aliasIconPath to POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns" as alias
set strDefaultAnswer to strPasteboardStrings as text
try
  set recordResponse to (display dialog "60秒 7200秒=2時間" with title "入力してください" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
  
on error
  log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
  log "キャンセルしました"
return "キャンセルしました"
end if

################################
######戻り値チェック
################################
###ダイアログの戻り値
set ocidResponse to (refMe's NSString's stringWithString:strResponse)
###数字以外があったらエラーで止める
###数字のキャラクタセット
set ocidDigitsCharSet to refMe's NSCharacterSet's decimalDigitCharacterSet()
###invertedSetで数字以外のキャラクタセット
set ocidNoDigitsCharSet to ocidDigitsCharSet's invertedSet()
###数値以外の文字のレンジを求めて
set recordRenge to ocidResponse's rangeOfCharacterFromSet:ocidNoDigitsCharSet
###見つからない以外=見つかった場合は数字以外の文字がある
if (recordRenge's location) ≠ refNSNotFound then
return "数字以外の文字が含まれています"
else
  log "数字だけです"
end if
############################
####戻り値を半角にする
set ocidResponseHalfwidth to (ocidResponse's stringByApplyingTransform:(refMe's NSStringTransformFullwidthToHalfwidth) |reverse|:false)

set theResponse to ocidResponseHalfwidth as text

##display
set strCommandText to "/usr/bin/caffeinate -d"
##system
set strCommandText to "/usr/bin/caffeinate -i"
##disk
set strCommandText to "/usr/bin/caffeinate -m"
##user
set strCommandText to "/usr/bin/caffeinate -u"

set strCommandText to "/usr/bin/caffeinate -u -t 600"

set strCommandText to "/usr/bin/caffeinate -umd "



tell application "Terminal"
  launch
end tell

tell application "Terminal"
  activate
end tell

tell application "Terminal"
  set numCntWindow to (count of (every window)) as integer
end tell

tell application "Terminal"
  set objWindowID to (do script "\n\n")
  delay 1
  ###コマンド整形
  set strCommandText to ("/usr/bin/caffeinate -u -t " & theResponse & "") as text
  ###コマンド実行
do script strCommandText in objWindowID
end tell

set numDelayTime to theResponse as integer
delay (numDelayTime + 3)

################################
######ターミナルを終了させる
################################
tell application "Terminal"
  set numCntWindow to (count of (every window)) as integer
end tell
tell application "Terminal"
  set objWindow to (every window)
end tell
repeat with itemWindow in objWindow
  tell application "Terminal"
    tell itemWindow
      set frontmost to true
      delay 1
      tell application "System Events"
tell process "Terminal"
keystroke "c" using {control down}
keystroke "c" using {control down}
end tell
      end tell
    end tell
do script "exit" in itemWindow
do script "exit" in itemWindow
  end tell
end repeat
delay numCntWindow

tell application "Terminal"
  quit saving no
end tell


return
################################
######exitを送る
################################
tell application "Terminal"
  set objWindow to (every window)
end tell

repeat with itemWindow in objWindow
  tell application "Terminal"
    tell itemWindow
      set frontmost to true
    end tell
  end tell
  delay 1
  tell application "Terminal"
    tell itemWindow
      set frontmost to true
do script "exit"
do script "exit"
    end tell
  end tell
end repeat

################################
######閉じる
################################

tell application "Terminal"
  set objWindow to (every window)
end tell
repeat with itemWindow in objWindow
  tell application "Terminal"
    tell itemWindow
close saving no
    end tell
  end tell
end repeat



|

« [Terminal]ターミナルを終了させる | トップページ | フォルダ判定 »

Terminal」カテゴリの記事