Help us understand the problem. What is going on with this article?

アプリケーションが落ち着くまで待つAppleScriptハンドラ

More than 3 years have passed since last update.
  • アプリケーションに重い処理をさせたときに使う
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に変更
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away