[Messages.app]添付ファイルをゴミ箱に入れる macOS15.5対応
何か方法はあるとは思うんだけど
現時点では対応方法がわからないので
ゴミ箱に入れる処理を
一部ターミナルで処理する手順を追加した
うーん何か違うような気もする

ソース | |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | 各種キャッシュファイルをゴミ箱に移動します |
005 | メッセージアプリの各種キャッシュを |
006 | ゴミ箱に移動します |
007 | 削除するか?は判断まかせるタイプです |
008 | |
009 | システム設定>プライバシーとセキュリティ>フルディスクアクセスを |
010 | 『スクリプトエディタ』に対して与えていない場合は |
011 | コンテンツの収集が出来ないディレクトリ |
012 | 要は『メッセージ』だから、プライバシー保護が強めなディレクトリなので |
013 | フルディスクアクセスを与えていない場合でも |
014 | エラーにならないように |
015 | 実行するとターミナルが複数回起動して |
016 | スクリプトを実行してキャッシュをゴミ箱に入れる |
017 | mvコマンドを実行します |
018 | |
019 | v1 初回作成 |
020 | v2 macOS15.5のセキュリティ対応 |
021 | フルディスクアクセスが無い場合はターミナルでゴミ箱に入れるうように変更 |
022 | |
023 | com.cocolog-nifty.quicktimer.icefloe *) |
024 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
025 | ##自分環境がos12なので2.8にしているだけです |
026 | use AppleScript version "2.8" |
027 | use framework "Foundation" |
028 | use framework "AppKit" |
029 | use framework "UniformTypeIdentifiers" |
030 | |
031 | use scripting additions |
032 | |
033 | property refMe : a reference to current application |
034 | |
035 | set appFileManager to refMe's NSFileManager's defaultManager() |
036 | |
037 | ################################### |
038 | ########まずは処理するアプリケーションを終了させる |
039 | ################################### |
040 | doQuitApp2UTI("com.apple.MobileSMS") |
041 | |
042 | ################################### |
043 | ########キャッシュ |
044 | ################################### |
045 | |
046 | log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Caches") |
047 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Caches") |
048 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Caches") |
049 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Caches") |
050 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Caches") |
051 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Caches") |
052 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Caches") |
053 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Caches") |
054 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Caches") |
055 | |
056 | |
057 | log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Previews") |
058 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Previews") |
059 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Previews") |
060 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Previews") |
061 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Previews") |
062 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Previews") |
063 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Previews") |
064 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Previews") |
065 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Previews") |
066 | |
067 | |
068 | ################################### |
069 | ########Saved Application State |
070 | ################################### |
071 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Saved Application State") |
072 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Saved Application State") |
073 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Saved Application State") |
074 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Saved Application State") |
075 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Saved Application State") |
076 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Saved Application State") |
077 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Saved Application State") |
078 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Saved Application State") |
079 | |
080 | ################################### |
081 | ########Messages |
082 | ################################### |
083 | log doMoveToTrash("~/Library/Messages/Caches/Previews/Attachments") |
084 | log doMoveToTrash("~/Library/Messages/Caches/Previews/Search") |
085 | log doMoveToTrash("~/Library/Messages/Attachments") |
086 | |
087 | ################################### |
088 | ########処理 ゴミ箱に入れる |
089 | ################################### |
090 | |
091 | to doMoveToTrash(argFilePath) |
092 | ###ファイルマネジャー初期化 |
093 | set appFileManager to refMe's NSFileManager's defaultManager() |
094 | ######################################### |
095 | ###渡された値のClassを調べてとりあえずNSURLにする |
096 | set refClass to class of argFilePath |
097 | if refClass is list then |
098 | return "エラー:リストは処理しません" |
099 | else if refClass is text then |
100 | log "テキストパスです" |
101 | set ocidArgFilePathStr to (refMe's NSString's stringWithString:argFilePath) |
102 | set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath |
103 | set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath) |
104 | else if refClass is alias then |
105 | log "エイリアスパスです" |
106 | set strArgFilePath to (POSIX path of argFilePath) as text |
107 | set ocidArgFilePathStr to (refMe's NSString's stringWithString:strArgFilePath) |
108 | set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath |
109 | set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath) |
110 | else |
111 | set refClass to (className() of argFilePath) as text |
112 | if refClass contains "NSPathStore2" then |
113 | log "NSPathStore2です" |
114 | set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:argFilePath) |
115 | else if refClass contains "NSCFString" then |
116 | log "NSCFStringです" |
117 | set ocidArgFilePath to argFilePath's stringByStandardizingPath |
118 | set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath) |
119 | else if refClass contains "NSURL" then |
120 | set ocidArgFilePathURL to argFilePath |
121 | log "NSURLです" |
122 | end if |
123 | end if |
124 | ######################################### |
125 | ### |
126 | -->false |
127 | set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue |
128 | -->true |
129 | set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue |
130 | ######################################### |
131 | ###NSURLがエイリアス実在するか? |
132 | set ocidArgFilePath to ocidArgFilePathURL's |path|() |
133 | set boolFileAlias to appFileManager's fileExistsAtPath:(ocidArgFilePath) |
134 | ###パス先が実在しないなら処理はここまで |
135 | if boolFileAlias = false then |
136 | log ocidArgFilePath as text |
137 | log "処理中止 パス先が実在しない" |
138 | return false |
139 | end if |
140 | ######################################### |
141 | ###NSURLがディレクトリなのか?ファイルなのか? |
142 | set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference) |
143 | # log (item 1 of listBoolDir) |
144 | # log (item 2 of listBoolDir) |
145 | # log (item 3 of listBoolDir) |
146 | if (item 2 of listBoolDir) = ocidTrue then |
147 | ######################################### |
148 | log "ディレクトリです" |
149 | log ocidArgFilePathURL's |path| as text |
150 | ##内包リスト |
151 | set listResult to appFileManager's contentsOfDirectoryAtURL:(ocidArgFilePathURL) includingPropertiesForKeys:{refMe's NSURLPathKey} options:0 |error|:(reference) |
152 | ###結果 |
153 | set ocidContentsPathURLArray to item 1 of listResult |
154 | #15.5 missing value 対策 |
155 | if ocidContentsPathURLArray ≠ (missing value) then |
156 | |
157 | repeat with itemContentsPathURL in ocidContentsPathURLArray |
158 | ###ゴミ箱に入れる |
159 | set listResult to (appFileManager's trashItemAtURL:itemContentsPathURL resultingItemURL:(missing value) |error|:(reference)) |
160 | end repeat |
161 | |
162 | else if ocidContentsPathURLArray = (missing value) then |
163 | # |
164 | set strArgDirPathURL to (ocidArgFilePathURL's |path|()) as text |
165 | set aliasTrashDirPath to (path to trash from user domain) as alias |
166 | set strTrashDirPath to (POSIX path of aliasTrashDirPath) as text |
167 | set strTrashDir to ("/usr/bin/mktemp -d \"" & strTrashDirPath & "TrashITEM_XXXXXXXX\"") |
168 | set strItemTrashPath to (do shell script strTrashDir) as text |
169 | # |
170 | if strArgDirPathURL contains " " then |
171 | set strDelim to AppleScript's text item delimiters |
172 | set AppleScript's text item delimiters to " " |
173 | set listSubPath to every text item of strArgDirPathURL |
174 | set AppleScript's text item delimiters to "\\ " |
175 | set strArgDirPathURL to listSubPath as text |
176 | set AppleScript's text item delimiters to strDelim |
177 | end if |
178 | |
179 | set strCommandText to ("/bin/mv " & strArgDirPathURL & "/* " & strItemTrashPath & "") |
180 | log "\r" & strCommandText & "\r" |
181 | |
182 | doExecTerminal(strCommandText) |
183 | |
184 | |
185 | else if (count of ocidContentsPathURLArray) = 0 then |
186 | return false |
187 | end if |
188 | ###リストの数だけ繰り返し |
189 | |
190 | else |
191 | ######################################### |
192 | log "ファイルです" |
193 | set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error|:(reference) |
194 | if (item 2 of listBoolDir) = ocidTrue then |
195 | log "エイリアスは処理しません" |
196 | return false |
197 | end if |
198 | set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error|:(reference) |
199 | if (item 2 of listBoolDir) = ocidTrue then |
200 | log "シンボリックリンクは処理しません" |
201 | return false |
202 | end if |
203 | set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSystemImmutableKey) |error|:(reference) |
204 | if (item 2 of listBoolDir) = ocidTrue then |
205 | log "システムファイルは処理しません" |
206 | return false |
207 | end if |
208 | ###ファイルをゴミ箱に入れる |
209 | set listResult to (appFileManager's trashItemAtURL:ocidArgFilePathURL resultingItemURL:(missing value) |error|:(reference)) |
210 | end if |
211 | return true |
212 | end doMoveToTrash |
213 | |
214 | ################################### |
215 | ########アプリケーションを終了させる |
216 | ################################### |
217 | to doQuitApp2UTI(argUTI) |
218 | set strUTI to argUTI as text |
219 | ### まずは普通に終了を試みる |
220 | tell application id strUTI to quit |
221 | delay 2 |
222 | set ocidResultsArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI |
223 | set numCntArray to ocidResultsArray count |
224 | if numCntArray ≠ 0 then |
225 | set ocidRunApp to ocidResultsArray's objectAtIndex:0 |
226 | |
227 | ###通常終了 |
228 | set boolDone to ocidRunApp's terminate() |
229 | ####強制終了 |
230 | set boolDone to ocidRunApp's forceTerminate() |
231 | |
232 | #### killallを使う場合 |
233 | set ocidExecAppURL to ocidRunApp's executableURL() |
234 | set ocidFileName to ocidExecAppURL's lastPathComponent() |
235 | set strFileName to ocidFileName as text |
236 | |
237 | set strCommandText to ("/usr/bin/killall -z " & strFileName & "") as text |
238 | set ocidCommandText to refMe's NSString's stringWithString:strCommandText |
239 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
240 | ocidTermTask's setLaunchPath:"/bin/zsh" |
241 | ocidTermTask's setArguments:({"-c", ocidCommandText}) |
242 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
243 | |
244 | |
245 | ####killを使う場合 |
246 | set ocidPID to ocidRunApp's processIdentifier() |
247 | set strPID to ocidPID as text |
248 | log strPID |
249 | set strCommandText to ("/bin/kill -9 " & strPID & "") as text |
250 | set ocidCommandText to refMe's NSString's stringWithString:strCommandText |
251 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
252 | ocidTermTask's setLaunchPath:"/bin/zsh" |
253 | ocidTermTask's setArguments:({"-c", ocidCommandText}) |
254 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
255 | |
256 | |
257 | end if |
258 | end doQuitApp2UTI |
259 | |
260 | |
261 | to doExecTerminal(argCommandText) |
262 | |
263 | tell application "Terminal" |
264 | activate |
265 | set objWindowID to (do script "\n\n") |
266 | delay 1 |
267 | do script argCommandText in objWindowID |
268 | end tell |
269 | delay 3 |
270 | set listTime to {0, 0, 0} as list |
271 | set numInterval to 10 as integer |
272 | repeat |
273 | tell application "Terminal" |
274 | tell front window |
275 | tell front tab |
276 | set boolBusy to busy as boolean |
277 | end tell |
278 | end tell |
279 | end tell |
280 | if boolBusy is true then |
281 | ###スタートタイム |
282 | set strStartTime to doHMSms(listTime) |
283 | log "経過時間: " & strStartTime |
284 | delay numInterval |
285 | |
286 | ##まず秒が加算されるといくつになるか? |
287 | set numChkDigUp to (item 3 of listTime) + numInterval |
288 | ###↑この結果が59より大きければ繰り上がる |
289 | if numChkDigUp ≥ 60 then |
290 | set (item 3 of listTime) to numChkDigUp - 60 |
291 | if (item 2 of listTime) = 59 then |
292 | set (item 2 of listTime) to 0 |
293 | ####修正箇所 |
294 | #### set (item 1 of listTime) to (item 1 of listTime) + 1 |
295 | set (item 1 of listTime) to (item 1 of listTime) |
296 | else |
297 | set (item 2 of listTime) to (item 2 of listTime) + 1 |
298 | end if |
299 | else |
300 | set (item 3 of listTime) to (item 3 of listTime) + numInterval |
301 | end if |
302 | |
303 | ####エンドタイム |
304 | set strEndTime to doHMSms(listTime) |
305 | log strEndTime |
306 | |
307 | else if boolBusy is false then |
308 | tell application "Terminal" |
309 | do script "\n\n" in objWindowID |
310 | do script "exit" in objWindowID |
311 | delay 1 |
312 | close front window saving no |
313 | |
314 | end tell |
315 | exit repeat |
316 | end if |
317 | end repeat |
318 | |
319 | end doExecTerminal |
AppleScriptで生成しました |