| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 |
|
| 007 |
|
| 008 |
|
| 009 | com.cocolog-nifty.quicktimer.icefloe *) |
| 010 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 011 | use AppleScript version "2.8" |
| 012 | use framework "Foundation" |
| 013 | use framework "AppKit" |
| 014 | use framework "UniformTypeIdentifiers" |
| 015 | use scripting additions |
| 016 |
|
| 017 | property refMe : a reference to current application |
| 018 |
|
| 019 | ################################ |
| 020 | #バンドルIDで終了させる |
| 021 | #今回のこのラインナップでは無意味だけど一応先に処理する |
| 022 | set listBundleID to {"com.apple.automator.runner", "com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner", "com.apple.XprotectFramework.AnalysisService", "com.apple.filesystems.netfs.PlugInLibraryService"} as list |
| 023 |
|
| 024 | #まずはOBJCで終了 |
| 025 | repeat with itemBundleID in listBundleID |
| 026 | set strBundleID to itemBundleID as text |
| 027 | set boolDone to doQuitBundleApplication(strBundleID) |
| 028 | end repeat |
| 029 |
|
| 030 |
|
| 031 | #SIGTERM |
| 032 | repeat with itemBundleID in listBundleID |
| 033 | set strBundleID to itemBundleID as text |
| 034 | set boolDone to doSIGTERM(strBundleID) |
| 035 | end repeat |
| 036 |
|
| 037 | #SIGINT |
| 038 | repeat with itemBundleID in listBundleID |
| 039 | set strBundleID to itemBundleID as text |
| 040 | set boolDone to doSIGINT(strBundleID) |
| 041 | end repeat |
| 042 |
|
| 043 | #SIGHUP |
| 044 | repeat with itemBundleID in listBundleID |
| 045 | set strBundleID to itemBundleID as text |
| 046 | set boolDone to doSIGHUP(strBundleID) |
| 047 | end repeat |
| 048 |
|
| 049 | #ABRT |
| 050 | repeat with itemBundleID in listBundleID |
| 051 | set strBundleID to itemBundleID as text |
| 052 | set boolDone to doABRT(strBundleID) |
| 053 | end repeat |
| 054 |
|
| 055 | #KILL |
| 056 | repeat with itemBundleID in listBundleID |
| 057 | set strBundleID to itemBundleID as text |
| 058 | set boolDone to doKILL(strBundleID) |
| 059 | end repeat |
| 060 |
|
| 061 | ################################ |
| 062 | #バイナリー名指定して終了させる |
| 063 |
|
| 064 | set listBinName to {"PlugInLibraryService", "WorkflowServiceRunner", "XprotectService", "com.apple.automator.runner"} as list |
| 065 |
|
| 066 | #SIGTERM |
| 067 | repeat with itemBinName in listBinName |
| 068 | set boolDone to doBinName2SIGTERM(itemBinName) |
| 069 | end repeat |
| 070 |
|
| 071 | #SIGINT |
| 072 | repeat with itemBinName in listBinName |
| 073 | set boolDone to doBinName2SIGINT(itemBinName) |
| 074 | end repeat |
| 075 |
|
| 076 | #SIGHUP |
| 077 | repeat with itemBinName in listBinName |
| 078 | set boolDone to doBinName2SIGHUP(itemBinName) |
| 079 | end repeat |
| 080 | #ABRT |
| 081 | repeat with itemBinName in listBinName |
| 082 | set boolDone to doBinName2SIGHUP(itemBinName) |
| 083 | end repeat |
| 084 |
|
| 085 | #KILL |
| 086 | repeat with itemBinName in listBinName |
| 087 | set boolDone to doBinName2KILL(itemBinName) |
| 088 | end repeat |
| 089 |
|
| 090 |
|
| 091 | return "処理終了" |
| 092 |
|
| 093 | ################################ |
| 094 | #KILL |
| 095 | to doBinName2KILL(argBinName) |
| 096 | set strBinName to argBinName as text |
| 097 | set listPID to doGetProcessID(strBinName) as list |
| 098 | repeat with itemPID in listPID |
| 099 | set strCmd to ("/bin/kill -KILL " & itemPID & "") as text |
| 100 | try |
| 101 | do shell script strCmd |
| 102 | on error strErrMsg number numErrNo |
| 103 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 104 | return false |
| 105 | end try |
| 106 | end repeat |
| 107 | #terminateAutomaticallyTerminableApplications |
| 108 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 109 | return true |
| 110 | end doBinName2KILL |
| 111 |
|
| 112 | ################################ |
| 113 | #ABRT |
| 114 | to doBinName2ABRT(argBinName) |
| 115 | set strBinName to argBinName as text |
| 116 | set listPID to doGetProcessID(strBinName) as list |
| 117 | repeat with itemPID in listPID |
| 118 | set strCmd to ("/bin/kill -ABRT " & itemPID & "") as text |
| 119 | try |
| 120 | do shell script strCmd |
| 121 | on error strErrMsg number numErrNo |
| 122 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 123 | return false |
| 124 | end try |
| 125 | end repeat |
| 126 | #terminateAutomaticallyTerminableApplications |
| 127 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 128 | return true |
| 129 | end doBinName2ABRT |
| 130 |
|
| 131 | ################################ |
| 132 | #SIGHUP |
| 133 | to doBinName2SIGHUP(argBinName) |
| 134 | set strBinName to argBinName as text |
| 135 | set listPID to doGetProcessID(strBinName) as list |
| 136 | repeat with itemPID in listPID |
| 137 | set strCmd to ("/bin/kill -SIGHUP " & itemPID & "") as text |
| 138 | try |
| 139 | do shell script strCmd |
| 140 | on error strErrMsg number numErrNo |
| 141 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 142 | return false |
| 143 | end try |
| 144 | end repeat |
| 145 | #terminateAutomaticallyTerminableApplications |
| 146 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 147 | return true |
| 148 | end doBinName2SIGHUP |
| 149 |
|
| 150 | ################################ |
| 151 | #SIGINT |
| 152 | to doBinName2SIGINT(argBinName) |
| 153 | set strBinName to argBinName as text |
| 154 | set listPID to doGetProcessID(strBinName) as list |
| 155 | repeat with itemPID in listPID |
| 156 | set strCmd to ("/bin/kill -SIGINT " & itemPID & "") as text |
| 157 | try |
| 158 | do shell script strCmd |
| 159 | on error strErrMsg number numErrNo |
| 160 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 161 | return false |
| 162 | end try |
| 163 | end repeat |
| 164 | #terminateAutomaticallyTerminableApplications |
| 165 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 166 | return true |
| 167 | end doBinName2SIGINT |
| 168 |
|
| 169 | ################################ |
| 170 | #SIGTERM |
| 171 | to doBinName2SIGTERM(argBinName) |
| 172 | set strBinName to argBinName as text |
| 173 | set listPID to doGetProcessID(strBinName) as list |
| 174 | repeat with itemPID in listPID |
| 175 | set strCmd to ("/bin/kill -SIGTERM " & itemPID & "") as text |
| 176 | try |
| 177 | do shell script strCmd |
| 178 | on error strErrMsg number numErrNo |
| 179 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 180 | return false |
| 181 | end try |
| 182 | end repeat |
| 183 | #terminateAutomaticallyTerminableApplications |
| 184 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 185 | return true |
| 186 | end doBinName2SIGTERM |
| 187 |
|
| 188 | ################################ |
| 189 | #doGetProcessID |
| 190 | to doGetProcessID(argProcessName) |
| 191 | set strProcessName to argProcessName as text |
| 192 | |
| 193 | #ユーザーIDを取得 |
| 194 | set ocidUserShotName to refMe's NSUserName() |
| 195 | |
| 196 | #ユーザーIDでコマンドを整形して |
| 197 | set strCmd to ("/usr/bin/pgrep -u " & ocidUserShotName & " " & strProcessName & " || true") as text |
| 198 | #実行 |
| 199 | set strStdOut to (do shell script strCmd) as text |
| 200 | if strStdOut is "" then |
| 201 | set listProcessID to {} as list |
| 202 | return listProcessID |
| 203 | end if |
| 204 | #リストにする |
| 205 | set strDelim to AppleScript's text item delimiters |
| 206 | set AppleScript's text item delimiters to return |
| 207 | set listProcessID to every text item of strStdOut |
| 208 | set AppleScript's text item delimiters to strDelim |
| 209 | #terminateAutomaticallyTerminableApplications |
| 210 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 211 | return listProcessID |
| 212 | end doGetProcessID |
| 213 |
|
| 214 |
|
| 215 | ################################### |
| 216 | #KILL |
| 217 | to doKILL(argBundleID) |
| 218 | set strBundleID to argBundleID as text |
| 219 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 220 | repeat with itemApp in ocidAppArray |
| 221 | set numPID to itemApp's processIdentifier() |
| 222 | set boolDone to itemApp's forceTerminate() |
| 223 | set strCmd to ("/bin/zsh -c '/bin/kill -KILL " & numPID & "' 2>/dev/null || true") as text |
| 224 | try |
| 225 | do shell script strCmd |
| 226 | on error strErrMsg number numErrNo |
| 227 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 228 | return false |
| 229 | end try |
| 230 | end repeat |
| 231 | #terminateAutomaticallyTerminableApplications |
| 232 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 233 | return true |
| 234 | end doKILL |
| 235 |
|
| 236 |
|
| 237 | ################################### |
| 238 | #ABRT |
| 239 | to doABRT(argBundleID) |
| 240 | set strBundleID to argBundleID as text |
| 241 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 242 | repeat with itemApp in ocidAppArray |
| 243 | set numPID to itemApp's processIdentifier() |
| 244 | set boolDone to itemApp's forceTerminate() |
| 245 | set strCmd to ("/bin/zsh -c '/bin/kill -ABRT " & numPID & "' 2>/dev/null || true") as text |
| 246 | try |
| 247 | do shell script strCmd |
| 248 | on error strErrMsg number numErrNo |
| 249 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 250 | return false |
| 251 | end try |
| 252 | end repeat |
| 253 | #terminateAutomaticallyTerminableApplications |
| 254 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 255 | return true |
| 256 | end doABRT |
| 257 |
|
| 258 | ################################### |
| 259 | #ソフトな終了SIGHUP |
| 260 | to doSIGHUP(argBundleID) |
| 261 | set strBundleID to argBundleID as text |
| 262 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 263 | repeat with itemApp in ocidAppArray |
| 264 | set numPID to itemApp's processIdentifier() |
| 265 | set boolDone to itemApp's terminate() |
| 266 | set strCmd to ("/bin/zsh -c '/bin/kill -SIGHUP " & numPID & "' 2>/dev/null || true") as text |
| 267 | try |
| 268 | do shell script strCmd |
| 269 | on error strErrMsg number numErrNo |
| 270 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 271 | return false |
| 272 | end try |
| 273 | end repeat |
| 274 | #terminateAutomaticallyTerminableApplications |
| 275 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 276 | return true |
| 277 | end doSIGHUP |
| 278 |
|
| 279 | # |
| 280 | ################################### |
| 281 | #ソフトな終了SIGINT |
| 282 | to doSIGINT(argBundleID) |
| 283 | set strBundleID to argBundleID as text |
| 284 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 285 | repeat with itemApp in ocidAppArray |
| 286 | set numPID to itemApp's processIdentifier() |
| 287 | set boolDone to itemApp's terminate() |
| 288 | set strCmd to ("/bin/zsh -c '/bin/kill -SIGINT " & numPID & "' 2>/dev/null || true") as text |
| 289 | try |
| 290 | do shell script strCmd |
| 291 | on error strErrMsg number numErrNo |
| 292 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 293 | return false |
| 294 | end try |
| 295 | end repeat |
| 296 | #terminateAutomaticallyTerminableApplications |
| 297 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 298 | return true |
| 299 | end doSIGINT |
| 300 |
|
| 301 | ################################### |
| 302 | #ソフトな終了SIGTERM |
| 303 | to doSIGTERM(argBundleID) |
| 304 | set strBundleID to argBundleID as text |
| 305 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 306 | repeat with itemApp in ocidAppArray |
| 307 | set numPID to itemApp's processIdentifier() |
| 308 | set boolDone to itemApp's terminate() |
| 309 | set strCmd to ("/bin/zsh -c '/bin/kill -SIGTERM " & numPID & "' 2>/dev/null || true") as text |
| 310 | try |
| 311 | do shell script strCmd |
| 312 | on error strErrMsg number numErrNo |
| 313 | log "No: " & numErrNo & linefeed & "Error:" & strErrMsg |
| 314 | return false |
| 315 | end try |
| 316 | end repeat |
| 317 | #terminateAutomaticallyTerminableApplications |
| 318 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 319 | return true |
| 320 | end doSIGTERM |
| 321 |
|
| 322 | ################################## |
| 323 | #NSRunningApplication |
| 324 | to doQuitBundleApplication(argBundleID) |
| 325 | set strBundleID to argBundleID as text |
| 326 | set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
| 327 | set numCntArray to ocidAppArray's |count|() |
| 328 | if numCntArray = 0 then |
| 329 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 330 | set ocidAppArray to appSharedWorkspace's runningApplications() |
| 331 | repeat with itemApp in ocidAppArray |
| 332 | set ocidAppBundleID to itemApp's bundleIdentifier() |
| 333 | if ocidAppBundleID ≠ (missing value) then |
| 334 | set boolsEQ to (ocidAppBundleID's isEqualToString:(strBundleID)) |
| 335 | if boolsEQ is true then |
| 336 | set boolDone to itemApp's terminate() |
| 337 | end if |
| 338 | end if |
| 339 | end repeat |
| 340 | end if |
| 341 | return |
| 342 | repeat with itemApp in ocidAppArray |
| 343 | set boolDone to itemApp's terminate() |
| 344 | if boolDone is false then |
| 345 | set boolDone to itemApp's forceTerminate() |
| 346 | end if |
| 347 | end repeat |
| 348 | #terminateAutomaticallyTerminableApplications |
| 349 | refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications() |
| 350 | return true |
| 351 | end doQuitBundleApplication |