Messages

[Messages.app]添付ファイルをゴミ箱に入れる macOS15.5対応

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


202505310618361_1430x940

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

[Messages.app]キャッシュをゴミ箱に入れる macOS15対応.scpt
ソース
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スクリプトを実行してキャッシュをゴミ箱に入れる
017mvコマンドを実行します
018
019v1 初回作成
020v2 macOS15.5のセキュリティ対応
021フルディスクアクセスが無い場合はターミナルでゴミ箱に入れるうように変更
022
023com.cocolog-nifty.quicktimer.icefloe  *)
024----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
025##自分環境がos12なので2.8にしているだけです
026use AppleScript version "2.8"
027use framework "Foundation"
028use framework "AppKit"
029use framework "UniformTypeIdentifiers"
030
031use scripting additions
032
033property refMe : a reference to current application
034
035set appFileManager to refMe's NSFileManager's defaultManager()
036
037###################################
038########まずは処理するアプリケーションを終了させる
039###################################
040doQuitApp2UTI("com.apple.MobileSMS")
041
042###################################
043########キャッシュ
044###################################
045
046log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Caches")
047log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Caches")
048log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Caches")
049log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Caches")
050log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Caches")
051log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Caches")
052log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Caches")
053log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Caches")
054log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Caches")
055
056
057log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Previews")
058log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Previews")
059log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Previews")
060log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Previews")
061log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Previews")
062log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Previews")
063log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Previews")
064log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Previews")
065log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Previews")
066
067
068###################################
069########Saved Application State
070###################################
071log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Saved Application State")
072log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Saved Application State")
073log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Saved Application State")
074log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Saved Application State")
075log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Saved Application State")
076log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Saved Application State")
077log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Saved Application State")
078log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Saved Application State")
079
080###################################
081########Messages
082###################################
083log doMoveToTrash("~/Library/Messages/Caches/Previews/Attachments")
084log doMoveToTrash("~/Library/Messages/Caches/Previews/Search")
085log doMoveToTrash("~/Library/Messages/Attachments")
086
087###################################
088########処理 ゴミ箱に入れる
089###################################
090
091to 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
212end doMoveToTrash
213
214###################################
215########アプリケーションを終了させる
216###################################
217to 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
258end doQuitApp2UTI
259
260
261to 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   
319end doExecTerminal
AppleScriptで生成しました

| | コメント (0)

[Messages.app]アカウント情報を取得する


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#com.cocolog-nifty.quicktimer.icefloe
004----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
005##自分環境がos12なので2.8にしているだけです
006use AppleScript version "2.8"
007use framework "Foundation"
008use scripting additions
009property refMe : a reference to current application
010
011
012set strOutPutText to ("") as text
013
014tell application id "com.apple.MobileSMS"
015  set listAccount to every account as list
016  repeat with itemAccount in listAccount
017    tell itemAccount
018      set strOutPutText to strOutPutText & "サービスタイプ:" & (service type as text) & "\r"
019      set strOutPutText to strOutPutText & "アカウントID:" & (id as text) & "\r"
020      set strOutPutText to strOutPutText & "詳細:" & (description as text) & "\r"
021      set strOutPutText to strOutPutText & "----+----1----+----2----+-----3----+----4----+----5\r"
022    end tell
023  end repeat
024end tell
025
026################################
027######ダイアログ
028################################
029tell current application
030  set strName to name as text
031end tell
032####スクリプトメニューから実行したら
033if strName is "osascript" then
034  tell application "Finder"
035    activate
036  end tell
037else
038  tell current application
039    activate
040  end tell
041end if
042set aliasIconPath to POSIX file "/Applications/Messenger.app/Contents/Resources/messenger.icns" as alias
043
044try
045  set recordResponse to (display dialog "詳しく" with title "入力してください" default answer strOutPutText buttons {"クリップボードにコピー", "OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
046  
047on error
048  log "エラーしました"
049  return "エラーしました"
050end try
051if true is equal to (gave up of recordResponse) then
052  return "時間切れですやりなおしてください"
053end if
054if "OK" is equal to (button returned of recordResponse) then
055  set strResponse to (text returned of recordResponse) as text
056else if button returned of recordResponse is "クリップボードにコピー" then
057  set strText to text returned of recordResponse as text
058  ####ペーストボード宣言
059  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
060  ##結果をペーストボードにテキストで入れる
061  set ocidText to (refMe's NSString's stringWithString:(strText))
062  appPasteboard's clearContents()
063  appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
064  return
065else
066  log "キャンセルしました"
067  return "キャンセルしました"
068end if
069
070
AppleScriptで生成しました

|

[Messages.app]キャッシュをゴミ箱に入れる macos14対応

202505310618361_1430x940

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

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

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# 各種キャッシュファイルをゴミ箱に移動します
005# 
006#com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008##自分環境がos12なので2.8にしているだけです
009use AppleScript version "2.8"
010use framework "Foundation"
011use scripting additions
012
013property refMe : a reference to current application
014
015set appFileManager to refMe's NSFileManager's defaultManager()
016
017###################################
018########まずは処理するアプリケーションを終了させる
019###################################
020doQuitApp2UTI("com.apple.MobileSMS")
021
022###################################
023########キャッシュ
024###################################
025
026log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Caches")
027log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Caches")
028log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Caches")
029log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Caches")
030log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Caches")
031log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Caches")
032log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Caches")
033log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Caches")
034log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Caches")
035
036
037log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Previews")
038log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Previews")
039log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Previews")
040log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Previews")
041log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Previews")
042log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Previews")
043log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Previews")
044log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Previews")
045log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Previews")
046
047
048###################################
049########Saved Application State
050###################################
051log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Saved Application State")
052log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Saved Application State")
053log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Saved Application State")
054log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Saved Application State")
055log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Saved Application State")
056log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Saved Application State")
057log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Saved Application State")
058log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Saved Application State")
059
060###################################
061########Messages
062###################################
063log doMoveToTrash("~/Library/Messages/Caches/Previews/Attachments")
064log doMoveToTrash("~/Library/Messages/Caches/Previews/Search")
065log doMoveToTrash("~/Library/Messages/Attachments")
066
067###################################
068########処理 ゴミ箱に入れる
069###################################
070
071to doMoveToTrash(argFilePath)
072  ###ファイルマネジャー初期化
073  set appFileManager to refMe's NSFileManager's defaultManager()
074  #########################################
075  ###渡された値のClassを調べてとりあえずNSURLにする
076  set refClass to class of argFilePath
077  if refClass is list then
078    return "エラー:リストは処理しません"
079  else if refClass is text then
080    log "テキストパスです"
081    set ocidArgFilePathStr to (refMe's NSString's stringWithString:argFilePath)
082    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
083    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
084  else if refClass is alias then
085    log "エイリアスパスです"
086    set strArgFilePath to (POSIX path of argFilePath) as text
087    set ocidArgFilePathStr to (refMe's NSString's stringWithString:strArgFilePath)
088    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
089    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
090  else
091    set refClass to (className() of argFilePath) as text
092    if refClass contains "NSPathStore2" then
093      log "NSPathStore2です"
094      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:argFilePath)
095    else if refClass contains "NSCFString" then
096      log "NSCFStringです"
097      set ocidArgFilePath to argFilePath's stringByStandardizingPath
098      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
099    else if refClass contains "NSURL" then
100      set ocidArgFilePathURL to argFilePath
101      log "NSURLです"
102    end if
103  end if
104  #########################################
105  ###
106  -->false
107  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
108  -->true
109  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
110  #########################################
111  ###NSURLがエイリアス実在するか
112  set ocidArgFilePath to ocidArgFilePathURL's |path|()
113  set boolFileAlias to appFileManager's fileExistsAtPath:(ocidArgFilePath)
114  ###パス先が実在しないなら処理はここまで
115  if boolFileAlias = false then
116    log ocidArgFilePath as text
117    log "処理中止 パス先が実在しない"
118    return false
119  end if
120  #########################################
121  ###NSURLがディレクトリなのか?ファイルなのか?
122  set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)
123  #   log (item 1 of listBoolDir)
124  #   log (item 2 of listBoolDir)
125  #   log (item 3 of listBoolDir)
126  if (item 2 of listBoolDir) = ocidTrue then
127    #########################################
128    log "ディレクトリです"
129    log ocidArgFilePathURL's |path| as text
130    ##内包リスト
131    set listResult to appFileManager's contentsOfDirectoryAtURL:ocidArgFilePathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:0  |error| :(reference)
132    ###結果
133    set ocidContentsPathURLArray to item 1 of listResult
134    if ocidContentsPathURLArray = (missing value) then
135      return false
136    else if (count of ocidContentsPathURLArray) = 0 then
137      return false
138    end if
139    ###リストの数だけ繰り返し
140    repeat with itemContentsPathURL in ocidContentsPathURLArray
141      ###ゴミ箱に入れる
142      set listResult to (appFileManager's trashItemAtURL:itemContentsPathURL resultingItemURL:(missing value) |error| :(reference))
143    end repeat
144  else
145    #########################################
146    log "ファイルです"
147    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error| :(reference)
148    if (item 2 of listBoolDir) = ocidTrue then
149      log "エイリアスは処理しません"
150      return false
151    end if
152    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)
153    if (item 2 of listBoolDir) = ocidTrue then
154      log "シンボリックリンクは処理しません"
155      return false
156    end if
157    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSystemImmutableKey) |error| :(reference)
158    if (item 2 of listBoolDir) = ocidTrue then
159      log "システムファイルは処理しません"
160      return false
161    end if
162    ###ファイルをゴミ箱に入れる
163    set listResult to (appFileManager's trashItemAtURL:ocidArgFilePathURL resultingItemURL:(missing value) |error| :(reference))
164  end if
165  return true
166end doMoveToTrash
167
168###################################
169########アプリケーションを終了させる
170###################################
171to doQuitApp2UTI(argUTI)
172  set strUTI to argUTI as text
173  ### まずは普通に終了を試みる
174  tell application id strUTI to quit
175  delay 2
176  set ocidResultsArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
177  set numCntArray to ocidResultsArray count
178  if numCntArray ≠ 0 then
179    set ocidRunApp to ocidResultsArray's objectAtIndex:0
180    
181    ###通常終了
182    set boolDone to ocidRunApp's terminate()
183    ####強制終了
184    set boolDone to ocidRunApp's forceTerminate()
185    
186    #### killallを使う場合
187    set ocidExecAppURL to ocidRunApp's executableURL()
188    set ocidFileName to ocidExecAppURL's lastPathComponent()
189    set strFileName to ocidFileName as text
190    
191    set strCommandText to ("/usr/bin/killall -z " & strFileName & "") as text
192    set ocidCommandText to refMe's NSString's stringWithString:strCommandText
193    set ocidTermTask to refMe's NSTask's alloc()'s init()
194    ocidTermTask's setLaunchPath:"/bin/zsh"
195    ocidTermTask's setArguments:({"-c", ocidCommandText})
196    set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
197    
198    
199    ####killを使う場合
200    set ocidPID to ocidRunApp's processIdentifier()
201    set strPID to ocidPID as text
202    log strPID
203    set strCommandText to ("/bin/kill -9 " & strPID & "") as text
204    set ocidCommandText to refMe's NSString's stringWithString:strCommandText
205    set ocidTermTask to refMe's NSTask's alloc()'s init()
206    ocidTermTask's setLaunchPath:"/bin/zsh"
207    ocidTermTask's setArguments:({"-c", ocidCommandText})
208    set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
209    
210    
211  end if
212end doQuitApp2UTI
213
AppleScriptで生成しました

|

[Messages.app]添付ファイルをゴミ箱に入れる

202505310618361_1430x940

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

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

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# 各種キャッシュファイルをゴミ箱に移動します
005# 削除はご自分で判断してください
006# 壁紙等 再度ダウンロードされてしまう物は除外しています
007#com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009##自分環境がos12なので2.8にしているだけです
010use AppleScript version "2.8"
011use framework "Foundation"
012use scripting additions
013
014property refMe : a reference to current application
015
016set appFileManager to refMe's NSFileManager's defaultManager()
017
018###################################
019########まずは処理するアプリケーションを終了させる
020###################################
021doQuitApp2UTI("com.apple.MobileSMS")
022
023###################################
024########キャッシュ
025###################################
026log doMoveToTrash("~/Library/Messages/Caches/Previews/Attachments")
027log doMoveToTrash("~/Library/Messages/Caches/Previews/Search")
028log doMoveToTrash("~/Library/Messages/Attachments")
029
030###################################
031########処理 ゴミ箱に入れる
032###################################
033
034to doMoveToTrash(argFilePath)
035  ###ファイルマネジャー初期化
036  set appFileManager to refMe's NSFileManager's defaultManager()
037  #########################################
038  ###渡された値のClassを調べてとりあえずNSURLにする
039  set refClass to class of argFilePath
040  if refClass is list then
041    return "エラー:リストは処理しません"
042  else if refClass is text then
043    log "テキストパスです"
044    set ocidArgFilePathStr to (refMe's NSString's stringWithString:argFilePath)
045    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
046    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
047  else if refClass is alias then
048    log "エイリアスパスです"
049    set strArgFilePath to (POSIX path of argFilePath) as text
050    set ocidArgFilePathStr to (refMe's NSString's stringWithString:strArgFilePath)
051    set ocidArgFilePath to ocidArgFilePathStr's stringByStandardizingPath
052    set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
053  else
054    set refClass to (className() of argFilePath) as text
055    if refClass contains "NSPathStore2" then
056      log "NSPathStore2です"
057      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:argFilePath)
058    else if refClass contains "NSCFString" then
059      log "NSCFStringです"
060      set ocidArgFilePath to argFilePath's stringByStandardizingPath
061      set ocidArgFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidArgFilePath)
062    else if refClass contains "NSURL" then
063      set ocidArgFilePathURL to argFilePath
064      log "NSURLです"
065    end if
066  end if
067  #########################################
068  ###
069  -->false
070  set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
071  -->true
072  set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
073  #########################################
074  ###NSURLがエイリアス実在するか
075  set ocidArgFilePath to ocidArgFilePathURL's |path|()
076  set boolFileAlias to appFileManager's fileExistsAtPath:(ocidArgFilePath)
077  ###パス先が実在しないなら処理はここまで
078  if boolFileAlias = false then
079    log ocidArgFilePath as text
080    log "処理中止 パス先が実在しない"
081    return false
082  end if
083  #########################################
084  ###NSURLがディレクトリなのか?ファイルなのか?
085  set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)
086  #   log (item 1 of listBoolDir)
087  #   log (item 2 of listBoolDir)
088  #   log (item 3 of listBoolDir)
089  if (item 2 of listBoolDir) = ocidTrue then
090    #########################################
091    log "ディレクトリです"
092    log ocidArgFilePathURL's |path| as text
093    ##内包リスト
094    set listResult to appFileManager's contentsOfDirectoryAtURL:ocidArgFilePathURL includingPropertiesForKeys:{refMe's NSURLPathKey} options:0  |error| :(reference)
095    ###結果
096    set ocidContentsPathURLArray to item 1 of listResult
097    ###リストの数だけ繰り返し
098    repeat with itemContentsPathURL in ocidContentsPathURLArray
099      ###ゴミ箱に入れる
100      set listResult to (appFileManager's trashItemAtURL:itemContentsPathURL resultingItemURL:(missing value) |error| :(reference))
101    end repeat
102  else
103    #########################################
104    log "ファイルです"
105    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error| :(reference)
106    if (item 2 of listBoolDir) = ocidTrue then
107      log "エイリアスは処理しません"
108      return false
109    end if
110    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)
111    if (item 2 of listBoolDir) = ocidTrue then
112      log "シンボリックリンクは処理しません"
113      return false
114    end if
115    set listBoolDir to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSystemImmutableKey) |error| :(reference)
116    if (item 2 of listBoolDir) = ocidTrue then
117      log "システムファイルは処理しません"
118      return false
119    end if
120    ###ファイルをゴミ箱に入れる
121    set listResult to (appFileManager's trashItemAtURL:ocidArgFilePathURL resultingItemURL:(missing value) |error| :(reference))
122  end if
123  return true
124end doMoveToTrash
125
126###################################
127########アプリケーションを終了させる
128###################################
129to doQuitApp2UTI(argUTI)
130  set strUTI to argUTI as text
131  ### まずは普通に終了を試みる
132  tell application id strUTI to quit
133  delay 2
134  set ocidResultsArray to refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:strUTI
135  set numCntArray to ocidResultsArray count
136  if numCntArray ≠ 0 then
137    set ocidRunApp to ocidResultsArray's objectAtIndex:0
138    
139    ###通常終了
140    set boolDone to ocidRunApp's terminate()
141    ####強制終了
142    set boolDone to ocidRunApp's forceTerminate()
143    
144    #### killallを使う場合
145    set ocidExecAppURL to ocidRunApp's executableURL()
146    set ocidFileName to ocidExecAppURL's lastPathComponent()
147    set strFileName to ocidFileName as text
148    
149    set strCommandText to ("/usr/bin/killall -z " & strFileName & "") as text
150    set ocidCommandText to refMe's NSString's stringWithString:strCommandText
151    set ocidTermTask to refMe's NSTask's alloc()'s init()
152    ocidTermTask's setLaunchPath:"/bin/zsh"
153    ocidTermTask's setArguments:({"-c", ocidCommandText})
154    set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
155    
156    
157    ####killを使う場合
158    set ocidPID to ocidRunApp's processIdentifier()
159    set strPID to ocidPID as text
160    log strPID
161    set strCommandText to ("/bin/kill -9 " & strPID & "") as text
162    set ocidCommandText to refMe's NSString's stringWithString:strCommandText
163    set ocidTermTask to refMe's NSTask's alloc()'s init()
164    ocidTermTask's setLaunchPath:"/bin/zsh"
165    ocidTermTask's setArguments:({"-c", ocidCommandText})
166    set listDoneReturn to ocidTermTask's launchAndReturnError:(reference)
167    
168    
169  end if
170end doQuitApp2UTI
171
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom