20260513

[AppleScript] 出来るだけソフトにプロセス終了

[AppleScript] 出来るだけソフトにプロセス終了

NOTE記事一覧ですnote.com
 

【スクリプトエディタで開く】 |

出来るだけソフトにプロセス終了.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006
007
008
009com.cocolog-nifty.quicktimer.icefloe *)
010----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
011use AppleScript version "2.8"
012use framework "Foundation"
013use framework "AppKit"
014use framework "UniformTypeIdentifiers"
015use scripting additions
016
017property refMe : a reference to current application
018
019################################
020#バンドルIDで終了させる
021#今回のこのラインナップでは無意味だけど一応先に処理する
022set 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で終了
025repeat with itemBundleID in listBundleID
026   set strBundleID to itemBundleID as text
027   set boolDone to doQuitBundleApplication(strBundleID)
028end repeat
029
030
031#SIGTERM
032repeat with itemBundleID in listBundleID
033   set strBundleID to itemBundleID as text
034   set boolDone to doSIGTERM(strBundleID)
035end repeat
036
037#SIGINT
038repeat with itemBundleID in listBundleID
039   set strBundleID to itemBundleID as text
040   set boolDone to doSIGINT(strBundleID)
041end repeat
042
043#SIGHUP
044repeat with itemBundleID in listBundleID
045   set strBundleID to itemBundleID as text
046   set boolDone to doSIGHUP(strBundleID)
047end repeat
048
049#ABRT
050repeat with itemBundleID in listBundleID
051   set strBundleID to itemBundleID as text
052   set boolDone to doABRT(strBundleID)
053end repeat
054
055#KILL
056repeat with itemBundleID in listBundleID
057   set strBundleID to itemBundleID as text
058   set boolDone to doKILL(strBundleID)
059end repeat
060
061################################
062#バイナリー名指定して終了させる
063
064set listBinName to {"PlugInLibraryService", "WorkflowServiceRunner", "XprotectService", "com.apple.automator.runner"} as list
065
066#SIGTERM
067repeat with itemBinName in listBinName
068   set boolDone to doBinName2SIGTERM(itemBinName)
069end repeat
070
071#SIGINT
072repeat with itemBinName in listBinName
073   set boolDone to doBinName2SIGINT(itemBinName)
074end repeat
075
076#SIGHUP
077repeat with itemBinName in listBinName
078   set boolDone to doBinName2SIGHUP(itemBinName)
079end repeat
080#ABRT
081repeat with itemBinName in listBinName
082   set boolDone to doBinName2SIGHUP(itemBinName)
083end repeat
084
085#KILL
086repeat with itemBinName in listBinName
087   set boolDone to doBinName2KILL(itemBinName)
088end repeat
089
090
091return "処理終了"
092
093################################
094#KILL
095to 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
110end doBinName2KILL
111
112################################
113#ABRT
114to 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
129end doBinName2ABRT
130
131################################
132#SIGHUP
133to 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
148end doBinName2SIGHUP
149
150################################
151#SIGINT
152to 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
167end doBinName2SIGINT
168
169################################
170#SIGTERM
171to 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
186end doBinName2SIGTERM
187
188################################
189#doGetProcessID
190to 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
212end doGetProcessID
213
214
215###################################
216#KILL
217to 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
234end doKILL
235
236
237###################################
238#ABRT
239to 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
256end doABRT
257
258###################################
259#ソフトな終了SIGHUP
260to 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
277end doSIGHUP
278
279#
280###################################
281#ソフトな終了SIGINT
282to 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
299end doSIGINT
300
301###################################
302#ソフトな終了SIGTERM
303to 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
320end doSIGTERM
321
322##################################
323#NSRunningApplication
324to 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
351end doQuitBundleApplication
AppleScriptで生成しました