#!/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 "ScreenCaptureKit"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appFileManager to refMe's NSFileManager's defaultManager()
set strBinPath to "/usr/sbin/screencapture"
####################
###保存先
set strSaveDirPath to ("~/Pictures/ScreenCapture/ScreenCapture") as string
set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath)
set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
set ocidSaveDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:(true)
##フォルダを作る
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
####################
###
set listWindowApp to {} as list
tell application "System Events"
set listApplication to every application process as list
end tell
repeat with itemApplication in listApplication
tell application "System Events"
tell itemApplication
## set strAppID to (id) as text
## set objApp to (name)as text
set strBundleID to (bundle identifier) as text
set boolBackGround to (background only) as boolean
end tell
end tell
if strBundleID is (missing value) then
log "バンドルIDなし"
else
if boolBackGround is true then
log "バックグラウンドプロセス"
else
tell application id strBundleID
try
###Windowの数を数えて
set numCntWindow to (count of every window) as integer
if numCntWindow ≥ 1 then
##WindowがあるバンドルIDをリストにしていく
copy strBundleID to end of listWindowApp
end if
end try
end tell
end if
end if
end repeat
###################################
log listWindowApp
###ダイアログ
set strName to (name of current application) as text
if strName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
try
set listResponse to (choose from list listWindowApp with title "選んでください" with prompt "キャプチャーを取るアプリを選んでください\r前面のウィンドウをキャプチャーします" default items (item 1 of listWindowApp) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
###選んだバンドルID
set strBundleID to (item 1 of listResponse) as text
####ウィンドウIDを取得
tell application id strBundleID
set listWindowID to (id of every window) as list
end tell
####Windowの数だけ繰り返し
repeat with itemWindowID in listWindowID
####
set strDateNO to doGetDateNo("yyyyMMdd_HHmmss")
set strFileName to ("" & strBundleID & "." & itemWindowID & "-" & strDateNO & ".png") as text
set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName))
set strSaveFilePath to ocidSaveFilePathURL's |path| as text
####Chromeアプリだとエラーになる
try
set strCommandText to ("\"" & strBinPath & "\" -o -l " & itemWindowID & " -rxt png \"" & strSaveFilePath & "\"") as string
do shell script strCommandText
end try
end repeat
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set ocidDirPath to ocidSaveDirPathURL's |path|
set boolDone to appSharedWorkspace's selectFile:(ocidDirPath) inFileViewerRootedAtPath:(ocidDirPath)
return
##############################
### 今の日付日間 テキスト
##############################
to doGetDateNo(argDateFormat)
####日付情報の取得
set ocidDate to current application's NSDate's |date|()
###日付のフォーマットを定義
set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo"
ocidNSDateFormatter's setTimeZone:(ocidTimeZone)
ocidNSDateFormatter's setDateFormat:(argDateFormat)
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo