- アプリケーションに重い処理をさせたときに使う
waitWhileApplicationIsBusy.scpt
use scripting additions
use framework "Foundation"
my waitWhileApplicationIsBusy("Finder")
on waitWhileApplicationIsBusy(anApplication)
tell application "System Events"
set pid to unix id of my getProcess(anApplication)
end tell
repeat while character 1 of my doShellScript("ps -p " & pid & " -o state=") is in {"D", "R", "U"}
delay 1
end repeat
end waitWhileApplicationIsBusy
on getProcess(args)
tell application "System Events"
if class of args = application then
return application process (name of args)
else if class of args = application process then
return args
else
return application process (args as text)
end if
end tell
end getProcess
on doShellScript(command as text)
--require framework: Foundation
set task to current application's NSTask's alloc()'s init()
set task's launchPath to "/bin/sh"
set task's arguments to {"-c", command}
set outPipe to current application's NSPipe's pipe()
set task's standardOutput to outPipe
set errorPipe to current application's NSPipe's pipe()
set task's standardError to errorPipe
task's |launch|()
set outData to outPipe's fileHandleForReading()'s readDataToEndOfFile()
if outData = missing value or outData's |length|() as integer = 0 then
set errorData to errorPipe's fileHandleForReading()'s readDataToEndOfFile()
if errorData = missing value or errorData's |length|() as integer = 0 then
error "0 以外の状況でコマンドが終了しました。" number 1
else
error (current application's NSString's alloc()'s initWithData:errorData encoding:(current application's NSUTF8StringEncoding)) as text number 1
end if
end if
return (current application's NSString's alloc()'s initWithData:outData encoding:(current application's NSUTF8StringEncoding)) as text
end doShellScript
更新履歴
- 2011-11-27: Evernoteを対象に作成
- 2012-03-04: 任意のアプリケーションに対応
- 2013-10-29: CPUの計測から
ps -o state=
へ変更 - 2016-02-20: シェルコマンドの実行をObjective-Cに変更