[メンテナンス]ユーザーログファイルをゴミ箱に入れる
ログファイルはあまり大きいサイズにならないものが多く
また
アプリ側でローテーションするようになっているものも少なく無いので
頻繁に実行するようなものではないが
全部のフォルダメンテナンスするのは個数が多いので
半年に1回とか?で十分
また
アプリ側でローテーションするようになっているものも少なく無いので
頻繁に実行するようなものではないが
全部のフォルダメンテナンスするのは個数が多いので
半年に1回とか?で十分
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # V1 初回作成 |
004 | # V2 サンドボックスアプリのログファイルも対象にした |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use framework "UniformTypeIdentifiers" |
011 | use scripting additions |
012 | |
013 | property refMe : a reference to current application |
014 | |
015 | set appFileManager to refMe's NSFileManager's defaultManager() |
016 | |
017 | (* |
018 | Chromeエンジン使っているアプリは終了させてから実行しないと |
019 | chrome_crashpadがログを書き込めなくなるので |
020 | 恐ろしい数のエラーが出ます。 |
021 | *) |
022 | ###Chromeエンジン使っているアプリは終了させてから |
023 | tell application id "com.google.Chrome" to quit |
024 | tell application id "com.microsoft.edgemac" to quit |
025 | tell application id "com.microsoft.VSCode" to quit |
026 | ###終了を待つ |
027 | delay 5 |
028 | #################################### |
029 | ####対象ディレクトリ |
030 | #################################### |
031 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
032 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
033 | set ocidLogDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Logs") isDirectory:(true) |
034 | |
035 | ############################################## |
036 | ##準備 |
037 | ############################################## |
038 | ###enumeratorAtURL用のBoolean用 |
039 | set ocidFalse to (refMe's NSNumber's numberWithBool:false) |
040 | set ocidTrue to (refMe's NSNumber's numberWithBool:true) |
041 | ###enumeratorAtURLL格納用のレコード |
042 | set ocidEmuDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
043 | ###enumeratorAtURLL格納するリスト |
044 | set ocidEmuFileURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0 |
045 | ###ファイルURLのみを格納するリスト |
046 | set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0 |
047 | |
048 | ############################################## |
049 | ##ディレクトリのコンテツを収集 |
050 | ############################################## |
051 | ###収集する付随プロパティ |
052 | set ocidProperties to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
053 | ocidProperties's addObject:(refMe's NSURLIsRegularFileKey) |
054 | ocidProperties's addObject:(refMe's NSURLNameKey) |
055 | ocidProperties's addObject:(refMe's NSURLPathKey) |
056 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
057 | ####ディレクトリのコンテツを収集 |
058 | set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidLogDirPathURL) includingPropertiesForKeys:(ocidProperties) options:(ocidOption) errorHandler:(reference)) |
059 | ###戻り値用のリストに格納 |
060 | set ocidEmuFileURLArray to ocidEmuDict's allObjects() |
061 | |
062 | ############################################## |
063 | ##『ファイル』だけ取り出したリストにする |
064 | ############################################## |
065 | |
066 | ####enumeratorAtURLのかずだけ繰り返し |
067 | repeat with itemEmuFileURL in ocidEmuFileURLArray |
068 | ####URLをforKeyで取り出し |
069 | set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error| :(reference)) |
070 | ###リストからNSURLIsRegularFileKeyのBOOLを取り出し |
071 | set boolIsRegularFileKey to item 2 of listResult |
072 | ####ファイルのみを(ディレクトリやリンボリックリンクは含まない) |
073 | if boolIsRegularFileKey is ocidTrue then |
074 | ####リストにする |
075 | (ocidFilePathURLArray's addObject:itemEmuFileURL) |
076 | end if |
077 | end repeat |
078 | |
079 | ###解放 |
080 | set ocidEmuFileURLArray to "" |
081 | set ocidEmuDict to "" |
082 | |
083 | ############################################## |
084 | ##ゴミ箱に入れる |
085 | ############################################## |
086 | |
087 | repeat with itemFilePathURL in ocidFilePathURLArray |
088 | |
089 | log itemFilePathURL's |path|() as text |
090 | ####ゴミ箱 |
091 | set listResult to (appFileManager's trashItemAtURL:itemFilePathURL resultingItemURL:(missing value) |error| :(reference)) |
092 | |
093 | end repeat |
094 | |
095 | |
096 | ############################################## |
097 | ##Containers以下サンドボックスアプリのLOGも処理する |
098 | ############################################## |
099 | #Containers |
100 | set ocidContainersDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers") isDirectory:(true) |
101 | ##OPTION |
102 | set ocidPropertiesForKeys to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
103 | ocidPropertiesForKeys's addObject:(refMe's NSURLIsRegularFileKey) |
104 | ocidPropertiesForKeys's addObject:(refMe's NSURLNameKey) |
105 | ocidPropertiesForKeys's addObject:(refMe's NSURLPathKey) |
106 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
107 | #URLの収集 |
108 | set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidContainersDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference) |
109 | set ocidItemURLArray to (item 1 of listResponse) |
110 | |
111 | ############################## |
112 | # サードパーティチェック |
113 | #Apple以外のコンテナの場合は グリーンのインデックスをつける |
114 | repeat with itemURL in ocidItemURLArray |
115 | #フォルダ名が |
116 | set strDirName to (itemURL's lastPathComponent()) as text |
117 | #Apple |
118 | if strDirName starts with "com.apple" then |
119 | else |
120 | set ocidLabelNo to (refMe's NSNumber's numberWithInteger:(2)) |
121 | set listDone to (itemURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
122 | if (item 1 of listDone) is true then |
123 | else if (item 2 of listDone) ≠ (missing value) then |
124 | set strErrorNO to (item 2 of listDone)'s code() as text |
125 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
126 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
127 | return "エラーしました" & strErrorNO & strErrorMes |
128 | end if |
129 | end if |
130 | end repeat |
131 | |
132 | ############################## |
133 | # ログディレクトリ 有無チェック |
134 | repeat with itemURL in ocidItemURLArray |
135 | set ocidItemLogDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/Logs") isDirectory:(true)) |
136 | set ocidItemLogDirPath to ocidItemLogDirPathURL's |path|() |
137 | set boolExist to (appFileManager's fileExistsAtPath:(ocidItemLogDirPath) isDirectory:(true)) |
138 | if boolExist is false then |
139 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)) |
140 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
141 | set listDone to (appFileManager's createDirectoryAtURL:(ocidItemLogDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference)) |
142 | end if |
143 | end repeat |
144 | |
145 | ############################## |
146 | # 実在チェック |
147 | repeat with itemURL in ocidItemURLArray |
148 | set ocidItemLogDirPathURL to (itemURL's URLByAppendingPathComponent:("Data/Library/Logs") isDirectory:(true)) |
149 | #対象のフォルダの中のURLを収集して |
150 | #プロパティ |
151 | set ocidPropertiesArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
152 | (ocidPropertiesArray's addObject:(refMe's NSURLIsDirectoryKey)) |
153 | (ocidPropertiesArray's addObject:(refMe's NSURLNameKey)) |
154 | (ocidPropertiesArray's addObject:(refMe's NSURLPathKey)) |
155 | #オプション |
156 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
157 | #収集 |
158 | set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidItemLogDirPathURL) includingPropertiesForKeys:(ocidPropertiesArray) options:(ocidOption) errorHandler:(reference)) |
159 | set ocidEmuFileURLArray to ocidEmuDict's allObjects() |
160 | #順番にチェック |
161 | repeat with itemEmuURL in ocidEmuFileURLArray |
162 | #フォルダ?かチェックして |
163 | set listResponse to (itemEmuURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
164 | set boolIsDir to (item 2 of listResponse) as boolean |
165 | if boolIsDir is false then |
166 | #シンボリックリンクでないか?も確認する |
167 | set listResponse to (itemEmuURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference)) |
168 | set boolIsSym to (item 2 of listResponse) as boolean |
169 | if boolIsSym is false then |
170 | log itemEmuURL's |path| as text |
171 | set listDone to (appFileManager's trashItemAtURL:(itemEmuURL) resultingItemURL:(missing value) |error| :(reference)) |
172 | if (item 2 of listDone) ≠ (missing value) then |
173 | set strErrorNO to (item 2 of listDone)'s code() as text |
174 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
175 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
176 | return "エラーしました" & strErrorNO & strErrorMes |
177 | end if |
178 | end if |
179 | end if |
180 | end repeat |
181 | end repeat |
182 | |
AppleScriptで生成しました |
| 固定リンク
「Admin Maintenance」カテゴリの記事
- [メンテナンス]ユーザーログファイルをゴミ箱に入れる(2025.01.08)
- [XProtect]標準アップデート macOS15.2用更新 修正(2024.12.27)
- [macOS]15.2アップデート OSのアップデータダウンロードまで(2024.12.12)
- [メンテナンス]ユーザーログファイルをゴミ箱に入れる(2024.11.30)
- [メンテナンス] Containers以下サンドボックスアプリのキャッシュを全部ゴミ箱に入れる(2024.11.09)