[メンテナンス] Containers以下サンドボックスアプリのキャッシュを全部ゴミ箱に入れる
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | (* |
005 | Data/Library/Caches |
006 | Data/Library/HTTPStorages |
007 | Data/tmp |
008 | の中身をゴミ箱に入れて |
009 | |
010 | APPLE以外の名称のバンドル名にはグリーンのタグをつけます |
011 | |
012 | *) |
013 | # com.cocolog-nifty.quicktimer.icefloe |
014 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
015 | ##自分環境がos12なので2.8にしているだけです |
016 | use AppleScript version "2.8" |
017 | use framework "Foundation" |
018 | use framework "UniformTypeIdentifiers" |
019 | use framework "AppKit" |
020 | use scripting additions |
021 | |
022 | property refMe : a reference to current application |
023 | |
024 | |
025 | ############################## |
026 | #Library |
027 | set appFileManager to refMe's NSFileManager's defaultManager() |
028 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
029 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
030 | #Containers |
031 | set ocidContainersDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers") isDirectory:(true) |
032 | |
033 | ############################## |
034 | #Containersの項目を収集 |
035 | set ocidPropertiesForKeys to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
036 | ocidPropertiesForKeys's addObject:(refMe's NSURLIsRegularFileKey) |
037 | ocidPropertiesForKeys's addObject:(refMe's NSURLNameKey) |
038 | ocidPropertiesForKeys's addObject:(refMe's NSURLPathKey) |
039 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
040 | #URLの収集 |
041 | set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidContainersDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference) |
042 | set ocidItemURLArray to (item 1 of listResponse) |
043 | |
044 | ############################## |
045 | # 有無チェック |
046 | repeat with itemURL in ocidItemURLArray |
047 | #フォルダ名が |
048 | set strDirName to (itemURL's lastPathComponent()) as text |
049 | #Apple |
050 | if strDirName starts with "com.apple" then |
051 | ##ここはAppleの設定が入っているので何もしない |
052 | else |
053 | log strDirName |
054 | #Apple以外のコンテナの場合は グリーンのインデックスをつける |
055 | set ocidLabelNo to (refMe's NSNumber's numberWithInteger:(2)) |
056 | set listDone to (itemURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
057 | if (item 1 of listDone) is true then |
058 | log "正常処理" |
059 | else if (item 2 of listDone) ≠ (missing value) then |
060 | set strErrorNO to (item 2 of listDone)'s code() as text |
061 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
062 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
063 | return "エラーしました" & strErrorNO & strErrorMes |
064 | end if |
065 | |
066 | end if |
067 | |
068 | end repeat |
069 | |
070 | ############################## |
071 | # 有無チェック |
072 | repeat with itemURL in ocidItemURLArray |
073 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/Caches") isDirectory:(true)) |
074 | set ocidItemCachesDirPath to ocidItemCachesDirPathURL's |path| |
075 | set boolExist to (appFileManager's fileExistsAtPath:(ocidItemCachesDirPath) isDirectory:(true)) |
076 | #キャッシュディレクトリが無いなら作っちゃう |
077 | if boolExist is false then |
078 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
079 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
080 | set listDone to (appFileManager's createDirectoryAtURL:(ocidItemCachesDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
081 | end if |
082 | end repeat |
083 | |
084 | |
085 | |
086 | |
087 | ############################## |
088 | # 実在チェック |
089 | repeat with itemURL in ocidItemURLArray |
090 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/Caches") isDirectory:(true)) |
091 | #フォルダ?かチェックして |
092 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
093 | set boolIsDir to (item 2 of listResponse) as boolean |
094 | if boolIsDir is true then |
095 | #シンボリックリンクでないか?も確認する |
096 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)) |
097 | set boolIsSym to (item 2 of listResponse) as boolean |
098 | if boolIsSym is false then |
099 | ##フォルダで シンボリックリンクでも無いならフォルダでしょ |
100 | #対象のフォルダの中のURLを収集して |
101 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidItemCachesDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference)) |
102 | set ocidCachesURLArray to (item 1 of listResponse) |
103 | #順番にゴミ箱に入れる |
104 | repeat with itemCachesURL in ocidCachesURLArray |
105 | log itemCachesURL's |path| as text |
106 | set listDone to (appFileManager's trashItemAtURL:(itemCachesURL) resultingItemURL:(missing value) |error| :(reference)) |
107 | if (item 2 of listDone) ≠ (missing value) then |
108 | set strErrorNO to (item 2 of listDone)'s code() as text |
109 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
110 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
111 | return "エラーしました" & strErrorNO & strErrorMes |
112 | end if |
113 | end repeat |
114 | end if |
115 | end if |
116 | end repeat |
117 | |
118 | |
119 | ############################## |
120 | # 有無チェック |
121 | repeat with itemURL in ocidItemURLArray |
122 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/tmp") isDirectory:(true)) |
123 | set ocidItemCachesDirPath to ocidItemCachesDirPathURL's |path| |
124 | set boolExist to (appFileManager's fileExistsAtPath:(ocidItemCachesDirPath) isDirectory:(true)) |
125 | #キャッシュディレクトリが無いなら作っちゃう |
126 | if boolExist is false then |
127 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
128 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
129 | set listDone to (appFileManager's createDirectoryAtURL:(ocidItemCachesDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
130 | end if |
131 | end repeat |
132 | |
133 | ############################## |
134 | # 実在チェック |
135 | repeat with itemURL in ocidItemURLArray |
136 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/tmp") isDirectory:(true)) |
137 | #フォルダ?かチェックして |
138 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
139 | set boolIsDir to (item 2 of listResponse) as boolean |
140 | if boolIsDir is true then |
141 | #シンボリックリンクでないか?も確認する |
142 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)) |
143 | set boolIsSym to (item 2 of listResponse) as boolean |
144 | if boolIsSym is false then |
145 | ##フォルダで シンボリックリンクでも無いならフォルダでしょ |
146 | #対象のフォルダの中のURLを収集して |
147 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidItemCachesDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference)) |
148 | set ocidCachesURLArray to (item 1 of listResponse) |
149 | #順番にゴミ箱に入れる |
150 | repeat with itemCachesURL in ocidCachesURLArray |
151 | log itemCachesURL's |path| as text |
152 | set listDone to (appFileManager's trashItemAtURL:(itemCachesURL) resultingItemURL:(missing value) |error| :(reference)) |
153 | if (item 2 of listDone) ≠ (missing value) then |
154 | set strErrorNO to (item 2 of listDone)'s code() as text |
155 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
156 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
157 | return "エラーしました" & strErrorNO & strErrorMes |
158 | end if |
159 | end repeat |
160 | end if |
161 | end if |
162 | end repeat |
163 | |
164 | |
165 | ############################## |
166 | # 有無チェック |
167 | repeat with itemURL in ocidItemURLArray |
168 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/HTTPStorages") isDirectory:(true)) |
169 | set ocidItemCachesDirPath to ocidItemCachesDirPathURL's |path| |
170 | set boolExist to (appFileManager's fileExistsAtPath:(ocidItemCachesDirPath) isDirectory:(true)) |
171 | #キャッシュディレクトリが無いなら作っちゃう |
172 | if boolExist is false then |
173 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
174 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
175 | set listDone to (appFileManager's createDirectoryAtURL:(ocidItemCachesDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
176 | end if |
177 | end repeat |
178 | |
179 | ############################## |
180 | # 実在チェック |
181 | repeat with itemURL in ocidItemURLArray |
182 | set ocidItemCachesDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/HTTPStorages") isDirectory:(true)) |
183 | #フォルダ?かチェックして |
184 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
185 | set boolIsDir to (item 2 of listResponse) as boolean |
186 | if boolIsDir is true then |
187 | #シンボリックリンクでないか?も確認する |
188 | set listResponse to (ocidItemCachesDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)) |
189 | set boolIsSym to (item 2 of listResponse) as boolean |
190 | if boolIsSym is false then |
191 | ##フォルダで シンボリックリンクでも無いならフォルダでしょ |
192 | #対象のフォルダの中のURLを収集して |
193 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidItemCachesDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference)) |
194 | set ocidCachesURLArray to (item 1 of listResponse) |
195 | #順番にゴミ箱に入れる |
196 | repeat with itemCachesURL in ocidCachesURLArray |
197 | log itemCachesURL's |path| as text |
198 | set listDone to (appFileManager's trashItemAtURL:(itemCachesURL) resultingItemURL:(missing value) |error| :(reference)) |
199 | if (item 2 of listDone) ≠ (missing value) then |
200 | set strErrorNO to (item 2 of listDone)'s code() as text |
201 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
202 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
203 | return "エラーしました" & strErrorNO & strErrorMes |
204 | end if |
205 | end repeat |
206 | end if |
207 | end if |
208 | end repeat |
AppleScriptで生成しました |
| 固定リンク