« 経過時間 | トップページ | [ffmpeg] タイムスケールを1000に設定する »

[メンテナンス]ユーザーログファイルをゴミ箱に入れる

ログファイルはあまり大きいサイズにならないものが多く
また
アプリ側でローテーションするようになっているものも少なく無いので
頻繁に実行するようなものではないが
全部のフォルダメンテナンスするのは個数が多いので
半年に1回とか?で十分


AppleScript サンプルコード

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

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
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use framework "UniformTypeIdentifiers"
011use scripting additions
012
013property refMe : a reference to current application
014
015set appFileManager to refMe's NSFileManager's defaultManager()
016
017(*
018Chromeエンジン使っているアプリは終了させてから実行しないと
019chrome_crashpadがログを書き込めなくなるので
020恐ろしい数のエラーが出ます。
021*)
022###Chromeエンジン使っているアプリは終了させてから
023tell application id "com.google.Chrome" to quit
024tell application id "com.microsoft.edgemac" to quit
025tell application id "com.microsoft.VSCode" to quit
026###終了を待つ
027delay 5
028####################################
029####対象ディレクトリ
030####################################
031set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
032set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
033set ocidLogDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Logs") isDirectory:(true)
034
035##############################################
036##準備
037##############################################
038###enumeratorAtURL用のBoolean用
039set ocidFalse to (refMe's NSNumber's numberWithBool:false)
040set ocidTrue to (refMe's NSNumber's numberWithBool:true)
041###enumeratorAtURLL格納用のレコード
042set ocidEmuDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
043###enumeratorAtURLL格納するリスト
044set ocidEmuFileURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
045###ファイルURLのみを格納するリスト
046set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
047
048##############################################
049##ディレクトリのコンテツを収集
050##############################################
051###収集する付随プロパティ
052set ocidProperties to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
053ocidProperties's addObject:(refMe's NSURLIsRegularFileKey)
054ocidProperties's addObject:(refMe's NSURLNameKey)
055ocidProperties's addObject:(refMe's NSURLPathKey)
056set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
057####ディレクトリのコンテツを収集
058set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidLogDirPathURL) includingPropertiesForKeys:(ocidProperties) options:(ocidOption) errorHandler:(reference))
059###戻り値用のリストに格納
060set ocidEmuFileURLArray to ocidEmuDict's allObjects()
061
062##############################################
063##『ファイル』だけ取り出したリストにする
064##############################################
065
066####enumeratorAtURLのかずだけ繰り返し
067repeat 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
077end repeat
078
079###解放
080set ocidEmuFileURLArray to ""
081set ocidEmuDict to ""
082
083##############################################
084##ゴミ箱に入れる
085##############################################
086
087repeat 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  
093end repeat
094
095
096##############################################
097##Containers以下サンドボックスアプリのLOGも処理する
098##############################################
099#Containers
100set ocidContainersDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers") isDirectory:(true)
101##OPTION
102set ocidPropertiesForKeys to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
103ocidPropertiesForKeys's addObject:(refMe's NSURLIsRegularFileKey)
104ocidPropertiesForKeys's addObject:(refMe's NSURLNameKey)
105ocidPropertiesForKeys's addObject:(refMe's NSURLPathKey)
106set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
107#URLの収集
108set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidContainersDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error| :(reference)
109set ocidItemURLArray to (item 1 of listResponse)
110
111##############################
112# サードパーティチェック
113#Apple以外のコンテナの場合は グリーンのインデックスをつける
114repeat 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
130end repeat
131
132##############################
133# ログディレクトリ 有無チェック
134repeat 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
143end repeat
144
145##############################
146# 実在チェック
147repeat 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
181end repeat
182
AppleScriptで生成しました

|

« 経過時間 | トップページ | [ffmpeg] タイムスケールを1000に設定する »

Admin Maintenance」カテゴリの記事