| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | プロセスクリーニング |
| 006 |
|
| 007 | Automator Workflow Runner(999999) を終了させます |
| 008 | バンドルIDは共通で |
| 009 | com.apple.automator.xpc.runner |
| 010 | ユーザー権限で動作します |
| 011 | Quickアクションを実行したりすると |
| 012 | 実行されるプロセスですが |
| 013 | 自動で終了しないことが多いので溜まっていきます |
| 014 | できるだけ安全に終了させます |
| 015 |
|
| 016 | com.cocolog-nifty.quicktimer.icefloe *) |
| 017 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 018 | use AppleScript version "2.8" |
| 019 | use framework "Foundation" |
| 020 | use framework "AppKit" |
| 021 | use scripting additions |
| 022 | property refMe : a reference to current application |
| 023 |
|
| 024 | ################################ |
| 025 | #これでは、ほぼ終了出来ない |
| 026 | tell application "System Events" |
| 027 | set listProcessName to name of every process as list |
| 028 | if listProcessName contains "com.apple.automator.runner" then |
| 029 | tell application process "com.apple.automator.runner" to quit |
| 030 | end if |
| 031 | end tell |
| 032 |
|
| 033 | ################################ |
| 034 | #OBJCで終了 ほとんど出来ないが数個はこれで終了する |
| 035 | set boolDone to doQuitAutomatorRunner("com.apple.automator.xpc.runner") |
| 036 |
|
| 037 | ################################ |
| 038 | #SIGTERM(ソフトな終了) |
| 039 | set listProcessID to doGetProcessID("com.apple.automator.runner") |
| 040 | repeat with itemProcessID in listProcessID |
| 041 | set strCmd to ("/bin/kill -SIGTERM " & itemProcessID & "") as text |
| 042 | do shell script strCmd |
| 043 | end repeat |
| 044 | ################################ |
| 045 | #SIGHUP(ハードな終了=ハングアップ回避) |
| 046 | set listProcessID to doGetProcessID("com.apple.automator.runner") |
| 047 | repeat with itemProcessID in listProcessID |
| 048 | set strCmd to ("/bin/kill -SIGHUP " & itemProcessID & "") as text |
| 049 | do shell script strCmd |
| 050 | end repeat |
| 051 | ################################ |
| 052 | #SIGKILL(強制終了) |
| 053 | set listProcessID to doGetProcessID("com.apple.automator.runner") |
| 054 | repeat with itemProcessID in listProcessID |
| 055 | set strCmd to ("/bin/kill -SIGKILL " & itemProcessID & "") as text |
| 056 | #実行するか?=強制終了は別途考慮が必要 |
| 057 | # do shell script strCmd |
| 058 | end repeat |
| 059 | ################################ |
| 060 | #QUIT(コア付き終了) |
| 061 | set listProcessID to doGetProcessID("com.apple.automator.runner") |
| 062 | repeat with itemProcessID in listProcessID |
| 063 | set strCmd to ("/bin/kill -QUIT " & itemProcessID & "") as text |
| 064 | #実行するか?=コアダンプは別途考慮が必要 |
| 065 | # do shell script strCmd |
| 066 | end repeat |
| 067 | return |
| 068 |
|
| 069 | ################################ |
| 070 | #doGetProcessID |
| 071 | to doGetProcessID(argProcessName) |
| 072 | set strProcessName to argProcessName as text |
| 073 | #システムインフォを取得して |
| 074 | set recordSystemInfo to (system info) as record |
| 075 | #ユーザーIDを取得 |
| 076 | set strUID to (short user name of recordSystemInfo) as text |
| 077 | #ユーザーIDでコマンドを整形して |
| 078 | set strCmd to ("/usr/bin/pgrep -u " & strUID & " " & strProcessName & " || true") as text |
| 079 | #実行 |
| 080 | set strStdOut to (do shell script strCmd) as text |
| 081 | if strStdOut is "" then |
| 082 | set listProcessID to {} as list |
| 083 | return listProcessID |
| 084 | end if |
| 085 | #リストにする |
| 086 | set strDelim to AppleScript's text item delimiters |
| 087 | set AppleScript's text item delimiters to return |
| 088 | set listProcessID to every text item of strStdOut |
| 089 | set AppleScript's text item delimiters to strDelim |
| 090 | return listProcessID |
| 091 | end doGetProcessID |
| 092 | ################################## |
| 093 | #NSRunningApplication |
| 094 | to doQuitAutomatorRunner(argBundleID) |
| 095 | set strBundleID to argBundleID as text |
| 096 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 097 | repeat with itemApp in ocidAppArray |
| 098 | set boolDone to itemApp's terminate() |
| 099 | if boolDone is false then |
| 100 | set boolDone to itemApp's forceTerminate() |
| 101 | end if |
| 102 | end repeat |
| 103 | #terminateAutomaticallyTerminableApplications 自動終了 |
| 104 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 105 | return true |
| 106 | end doQuitAutomatorRunner |