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にしているだけです |
---|
009 | use AppleScript version "2.8" |
---|
010 | use framework "Foundation" |
---|
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 | ########まずは処理するアプリケーションを終了させる |
---|
019 | ################################### |
---|
020 | doQuitApp2UTI("com.apple.MobileSMS") |
---|
021 | |
---|
022 | ################################### |
---|
023 | ########キャッシュ |
---|
024 | ################################### |
---|
025 | |
---|
026 | log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Caches") |
---|
027 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Caches") |
---|
028 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Caches") |
---|
029 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Caches") |
---|
030 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Caches") |
---|
031 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Caches") |
---|
032 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Caches") |
---|
033 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Caches") |
---|
034 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Caches") |
---|
035 | |
---|
036 | |
---|
037 | log doMoveToTrash("~/Library/Caches/com.apple.MobileSMS/Previews") |
---|
038 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Previews") |
---|
039 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Previews") |
---|
040 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Previews") |
---|
041 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Previews") |
---|
042 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Previews") |
---|
043 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Previews") |
---|
044 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Previews") |
---|
045 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Previews") |
---|
046 | |
---|
047 | |
---|
048 | ################################### |
---|
049 | ########Saved Application State |
---|
050 | ################################### |
---|
051 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS/Data/Library/Saved Application State") |
---|
052 | log doMoveToTrash("~/Library/Containers/com.apple.share.Messages.window/Data/Library/Saved Application State") |
---|
053 | log doMoveToTrash("~/Library/Containers/com.apple.ScreenSaver.Computer-Name/Data/Library/Saved Application State") |
---|
054 | log doMoveToTrash("~/Library/Containers/com.apple.messages.StorageManagementExtension/Data/Library/Saved Application State") |
---|
055 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ShareExtension/Data/Library/Saved Application State") |
---|
056 | log doMoveToTrash("~/Library/Containers/com.apple.messages.ReplyExtension/Data/Library/Saved Application State") |
---|
057 | log doMoveToTrash("~/Library/Containers/com.apple.messages.AssistantExtension/Data/Library/Saved Application State") |
---|
058 | log doMoveToTrash("~/Library/Containers/com.apple.MobileSMS.MessagesActionExtension/Data/Library/Saved Application State") |
---|
059 | |
---|
060 | ################################### |
---|
061 | ########Messages |
---|
062 | ################################### |
---|
063 | log doMoveToTrash("~/Library/Messages/Caches/Previews/Attachments") |
---|
064 | log doMoveToTrash("~/Library/Messages/Caches/Previews/Search") |
---|
065 | log doMoveToTrash("~/Library/Messages/Attachments") |
---|
066 | |
---|
067 | ################################### |
---|
068 | ########処理 ゴミ箱に入れる |
---|
069 | ################################### |
---|
070 | |
---|
071 | to 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 |
---|
166 | end doMoveToTrash |
---|
167 | |
---|
168 | ################################### |
---|
169 | ########アプリケーションを終了させる |
---|
170 | ################################### |
---|
171 | to 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 |
---|
212 | end doQuitApp2UTI |
---|
213 | |
---|