[launchctl] 停止処理(v2 plistの値の変更)
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* # サービスの停止 要管理者権限 |
004 | v1 初回作成 |
005 | v2 PLISTの値を停止にする処理も入れた(いっそ削除した方が早いような) |
006 | |
007 | キーの値はOBJCで取得して |
008 | 設定の変更はzshとAppleScriptならでわの処理 |
009 | |
010 | com.cocolog-nifty.quicktimer.icefloe *) |
011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
012 | use AppleScript version "2.8" |
013 | use framework "Foundation" |
014 | use framework "UniformTypeIdentifiers" |
015 | use framework "AppKit" |
016 | use scripting additions |
017 | |
018 | property refMe : a reference to current application |
019 | |
020 | ################################### |
021 | #####収集 |
022 | ################################### |
023 | set ocidURLArray to refMe's NSMutableArray's alloc()'s init() |
024 | # |
025 | set appFileManager to refMe's NSFileManager's defaultManager() |
026 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSLocalDomainMask)) |
027 | set ocidLocalLibraryDirPathURL to ocidURLsArray's firstObject() |
028 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
029 | set ocidUseerLibraryDirPathURL to ocidURLsArray's firstObject() |
030 | # |
031 | set ocidLocalAgentsDirPathURL to ocidLocalLibraryDirPathURL's URLByAppendingPathComponent:("LaunchAgents") isDirectory:(true) |
032 | set ocidLocalDaemonsDirPathURL to ocidLocalLibraryDirPathURL's URLByAppendingPathComponent:("LaunchDaemons") isDirectory:(true) |
033 | set ocidUserAgentsDirPathURL to ocidUseerLibraryDirPathURL's URLByAppendingPathComponent:("LaunchAgents") isDirectory:(true) |
034 | # |
035 | set ocidHomeDirURL to appFileManager's homeDirectoryForCurrentUser() |
036 | set strHomeDirURL to ocidHomeDirURL's |path|() as text |
037 | # |
038 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
039 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
040 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
041 | ocidKeyArray's addObject:(refMe's NSURLNameKey) |
042 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidLocalAgentsDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
043 | set ocidSubPathURLArray to (item 1 of listResponse) |
044 | ocidURLArray's addObjectsFromArray:(ocidSubPathURLArray) |
045 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidLocalDaemonsDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
046 | set ocidSubPathURLArray to (item 1 of listResponse) |
047 | ocidURLArray's addObjectsFromArray:(ocidSubPathURLArray) |
048 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidUserAgentsDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error|:(reference)) |
049 | set ocidSubPathURLArray to (item 1 of listResponse) |
050 | ocidURLArray's addObjectsFromArray:(ocidSubPathURLArray) |
051 | #ソートして |
052 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:") |
053 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
054 | set ocidURLSortArray to ocidURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
055 | #ダイアログ用にテキストのパスリストにする |
056 | set listFilePath to {} as list |
057 | repeat with itemURL in ocidURLSortArray |
058 | set strFilePath to itemURL's |path|() as text |
059 | set end of listFilePath to strFilePath |
060 | end repeat |
061 | ################################### |
062 | #####入力ダイアログ |
063 | ################################### |
064 | set strName to (name of current application) as text |
065 | if strName is "osascript" then |
066 | tell application "Finder" to activate |
067 | else |
068 | tell current application to activate |
069 | end if |
070 | set strPrompt to "\r ファイルを選んでください\r launchctl unloadして停止します(再起動後も有効)\r \r " as text |
071 | set strTitle to "ファイルを選んでください" as text |
072 | try |
073 | set listResponse to (choose from list listFilePath with title strTitle with prompt strPrompt default items (item 1 of listFilePath) OK button name "OK" cancel button name "キャンセル" with multiple selections allowed without empty selection allowed) as list |
074 | on error |
075 | log "エラーしました" |
076 | return "エラーしました" |
077 | error "エラーしました" number -200 |
078 | end try |
079 | if listResponse = {} then |
080 | log "何も選択していない" |
081 | # 止めるなら ここに returnを入れる |
082 | else if (item 1 of listResponse) is false then |
083 | return "キャンセルしました" |
084 | error "キャンセルしました" number -200 |
085 | else |
086 | set listFilePath to listResponse as list |
087 | end if |
088 | ################################### |
089 | #####判定材料 |
090 | ################################### |
091 | #UID |
092 | set recordSystemInfo to (system info) as record |
093 | set strUID to (user ID of recordSystemInfo) as text |
094 | set strSetUID to ("gui/" & strUID) as text |
095 | |
096 | ################################### |
097 | #####MachServices をFalseにする |
098 | ################################### |
099 | |
100 | repeat with itemFilePath in listFilePath |
101 | set strFilePath to itemFilePath as text |
102 | log strFilePath |
103 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
104 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
105 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)) |
106 | #設定されているキー名称はOBJCで取得して |
107 | set listResponse to (refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference)) |
108 | set ocidPlistDict to (item 1 of listResponse) |
109 | set ocidMachServicesDict to (ocidPlistDict's objectForKey:("MachServices")) |
110 | if ocidMachServicesDict ≠ (missing value) then |
111 | set ocidAllKeys to ocidMachServicesDict's allKeys() |
112 | #コマンドはzshで実行 |
113 | repeat with itemKey in ocidAllKeys |
114 | set strKey to itemKey as text |
115 | if strFilePath contains strHomeDirURL then |
116 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:MachServices:" & strKey & " false\" \"" & strFilePath & "\" ") as text |
117 | log doZshShellScript(strCommandText) |
118 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"" & strFilePath & "\" ") as text |
119 | log doZshShellScript(strCommandText) |
120 | else |
121 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:MachServices:" & strKey & " false\" \"" & strFilePath & "\" ") as text |
122 | log doZshShellScriptSUDO(strCommandText) |
123 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"" & strFilePath & "\" ") as text |
124 | log doZshShellScriptSUDO(strCommandText) |
125 | end if |
126 | end repeat |
127 | end if |
128 | set boolRunAtLoad to (ocidPlistDict's valueForKey:("RunAtLoad")) |
129 | if boolRunAtLoad ≠ (missing value) then |
130 | set boolRunAtLoad to boolRunAtLoad as boolean |
131 | end if |
132 | if boolRunAtLoad is true then |
133 | if strFilePath contains strHomeDirURL then |
134 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:RunAtLoad false\" \"" & strFilePath & "\" ") as text |
135 | log doZshShellScript(strCommandText) |
136 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"" & strFilePath & "\" ") as text |
137 | log doZshShellScript(strCommandText) |
138 | else |
139 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Set:RunAtLoad false\" \"" & strFilePath & "\" ") as text |
140 | log doZshShellScriptSUDO(strCommandText) |
141 | set strCommandText to ("/usr/libexec/PlistBuddy -c \"Save\" \"" & strFilePath & "\" ") as text |
142 | log doZshShellScriptSUDO(strCommandText) |
143 | end if |
144 | end if |
145 | |
146 | end repeat |
147 | |
148 | |
149 | ################################### |
150 | #####本処理 disable |
151 | ################################### |
152 | |
153 | repeat with itemFilePath in listFilePath |
154 | set strFilePath to itemFilePath as text |
155 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
156 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
157 | set ocidFileName to ocidFilePath's lastPathComponent() |
158 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
159 | set strBaseFileName to ocidBaseFileName as text |
160 | set strSetService to (strSetUID & "/" & strBaseFileName) as text |
161 | ## |
162 | if strFilePath contains strHomeDirURL then |
163 | set strCommandText to ("/bin/launchctl disable \"" & strSetService & "\"") as text |
164 | log doZshShellScript(strCommandText) |
165 | else |
166 | set strCommandText to ("/bin/launchctl disable \"" & strSetService & "\"") as text |
167 | log doZshShellScript(strCommandText) |
168 | set strSetSudoService to ("system/" & strBaseFileName) as text |
169 | set strCommandText to ("/bin/launchctl disable \"" & strSetSudoService & "\"") as text |
170 | log doZshShellScriptSUDO(strCommandText) |
171 | end if |
172 | end repeat |
173 | |
174 | |
175 | |
176 | |
177 | ################################### |
178 | #####本処理 unload |
179 | ################################### |
180 | |
181 | repeat with itemFilePath in listFilePath |
182 | set strFilePath to itemFilePath as text |
183 | ## |
184 | if strFilePath contains strHomeDirURL then |
185 | set strCommandText to ("/bin/launchctl unload -w \"" & strFilePath & "\"") as text |
186 | log doZshShellScript(strCommandText) |
187 | else |
188 | set strCommandText to ("/bin/launchctl unload -w \"" & strFilePath & "\"") as text |
189 | log doZshShellScript(strCommandText) |
190 | set strCommandText to ("/bin/launchctl unload -w \"" & strFilePath & "\"") as text |
191 | log doZshShellScriptSUDO(strCommandText) |
192 | end if |
193 | |
194 | |
195 | end repeat |
196 | |
197 | |
198 | |
199 | ################################### |
200 | #####本処理 bootout |
201 | ################################### |
202 | |
203 | repeat with itemFilePath in listFilePath |
204 | set strFilePath to itemFilePath as text |
205 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
206 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
207 | set ocidFileName to ocidFilePath's lastPathComponent() |
208 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
209 | set strBaseFileName to ocidBaseFileName as text |
210 | set strSetService to (strSetUID & "/" & strBaseFileName) as text |
211 | |
212 | ## |
213 | if strFilePath contains strHomeDirURL then |
214 | set strCommandText to ("/bin/launchctl bootout \"" & strSetService & "\"") as text |
215 | log doZshShellScript(strCommandText) |
216 | else |
217 | set strCommandText to ("/bin/launchctl bootout \"" & strSetService & "\"") as text |
218 | log doZshShellScript(strCommandText) |
219 | #SUDO |
220 | set strSetSudoService to ("system/" & strBaseFileName) as text |
221 | set strCommandText to ("/bin/launchctl bootout \"" & strSetSudoService & "\"") as text |
222 | log doZshShellScriptSUDO(strCommandText) |
223 | |
224 | end if |
225 | end repeat |
226 | |
227 | |
228 | ########################## |
229 | # 【通常】ZSH 実行 |
230 | to doZshShellScript(argCommandText) |
231 | set strCommandText to argCommandText as text |
232 | log "\r" & strCommandText & "\r" |
233 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
234 | log "\r" & strExec & "\r" |
235 | ########## |
236 | #コマンド実行 |
237 | try |
238 | log "コマンド開始" |
239 | set strResnponse to (do shell script strExec) as text |
240 | log "コマンド終了" |
241 | on error |
242 | return false |
243 | end try |
244 | return strResnponse |
245 | end doZshShellScript |
246 | |
247 | |
248 | |
249 | ########################## |
250 | # 【SUDO】ZSH 実行 |
251 | to doZshShellScriptSUDO(argCommandText) |
252 | set strCommandText to argCommandText as text |
253 | log "\r" & strCommandText & "\r" |
254 | set strExec to ("/bin/zsh -c '/usr/bin/sudo " & strCommandText & "'") as text |
255 | log "\r" & strExec & "\r" |
256 | ########## |
257 | #コマンド実行 |
258 | try |
259 | log "コマンド開始" |
260 | set strResnponse to (do shell script strExec) as text |
261 | log "コマンド終了" |
262 | on error |
263 | return false |
264 | end try |
265 | return strResnponse |
266 | end doZshShellScriptSUDO |
AppleScriptで生成しました |