001 | #! /usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | #ユーザードメインの |
---|
004 | # backgrounditems.btmのブックマークデータを表示します |
---|
005 | #unarchivedObjectOfClassesが失敗するので |
---|
006 | #できるようになるまでの仮 |
---|
007 | #com.cocolog-nifty.quicktimer.icefloe |
---|
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
009 | use AppleScript version "2.8" |
---|
010 | use framework "Foundation" |
---|
011 | use framework "AppKit" |
---|
012 | use scripting additions |
---|
013 | property refMe : a reference to current application |
---|
014 | |
---|
015 | ####入力ファイルパス |
---|
016 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
017 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationSupportDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
018 | set ocidApplicatioocidupportDirPathURL to ocidURLsArray's firstObject() |
---|
019 | set ocidFilePathURL to ocidApplicatioocidupportDirPathURL's URLByAppendingPathComponent:("com.apple.backgroundtaskmanagementagent/backgrounditems.btm") isDirectory:(false) |
---|
020 | (* |
---|
021 | set strFilePath to ("~/Library/Application Support/com.apple.backgroundtaskmanagementagent/backgrounditems.btm") as text |
---|
022 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
---|
023 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
---|
024 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
---|
025 | *) |
---|
026 | # NSDataに読み込んで |
---|
027 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(refMe's NSDataReadingMappedIfSafe) |error| :(reference) |
---|
028 | if (item 2 of listResponse) = (missing value) then |
---|
029 | log "正常処理" |
---|
030 | set ocidReadData to (item 1 of listResponse) |
---|
031 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
032 | log (item 2 of listResponse)'s code() as text |
---|
033 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
034 | return "エラーしました" |
---|
035 | end if |
---|
036 | |
---|
037 | # unarchivedObjectOfClassで解凍する |
---|
038 | #DATAを解凍する |
---|
039 | set ocidClassListArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
---|
040 | (ocidClassListArray's addObject:(refMe's NSDictionary's class)) |
---|
041 | (ocidClassListArray's addObject:(refMe's NSMutableDictionary's class)) |
---|
042 | (ocidClassListArray's addObject:(refMe's NSArray's class)) |
---|
043 | (ocidClassListArray's addObject:(refMe's NSMutableArray's class)) |
---|
044 | (ocidClassListArray's addObject:(refMe's NSObject's classForKeyedUnarchiver)) |
---|
045 | (ocidClassListArray's addObject:(refMe's NSObject's class)) |
---|
046 | (ocidClassListArray's addObject:(refMe's NSKeyedArchiver's class)) |
---|
047 | (ocidClassListArray's addObject:(refMe's NSKeyedUnarchiver's class)) |
---|
048 | #クラスセット |
---|
049 | set ocidSetClass to refMe's NSSet's alloc()'s initWithArray:(ocidClassListArray) |
---|
050 | #解凍 |
---|
051 | set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClasses:(ocidSetClass) fromData:(ocidReadData) |error| :(reference) |
---|
052 | set ocidPlistDict to (item 1 of listResponse) |
---|
053 | if ocidPlistDict = (missing value) then |
---|
054 | log "解凍に失敗しましたので解凍しないでブックマーク一覧を取得します" |
---|
055 | set ocidFormat to refMe's NSPropertyListBinaryFormat_v1_0 |
---|
056 | set ocidOption to refMe's NSPropertyListMutableContainersAndLeaves |
---|
057 | set listResponse to refMe's NSPropertyListSerialization's propertyListWithData:(ocidReadData) options:(ocidOption) format:(ocidFormat) |error| :(reference) |
---|
058 | if (item 2 of listResponse) = (missing value) then |
---|
059 | log "正常処理" |
---|
060 | set ocidReadDict to (item 1 of listResponse) |
---|
061 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
062 | log (item 2 of listResponse)'s code() as text |
---|
063 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
064 | return "エラーしました" |
---|
065 | end if |
---|
066 | #必要なデータを格納するArray |
---|
067 | set ocidBookMarkArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
068 | #ROOTから取得 |
---|
069 | set ocidObjectArray to ocidReadDict's objectForKey:("$objects") |
---|
070 | #リストの数を数えて |
---|
071 | set numCntArray to ocidObjectArray's |count|() as integer |
---|
072 | #ocidObjectArrayの数だけ繰り返し |
---|
073 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
---|
074 | |
---|
075 | #Arrayから取り出して |
---|
076 | set ocidItemData to (ocidObjectArray's objectAtIndex:(itemNo)) |
---|
077 | #クラスを調べて |
---|
078 | set strClassName to ocidItemData's className() as text |
---|
079 | #DATAなら処理する |
---|
080 | if strClassName contains "NSCFData" then |
---|
081 | #NSURL情報を格納するARRAY |
---|
082 | set ocidItemURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
---|
083 | #格納用のARRAY |
---|
084 | set ocidKeyArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
---|
085 | #パスと名前を取得して |
---|
086 | (ocidKeyArray's addObject:(refMe's NSURLPathKey)) |
---|
087 | (ocidKeyArray's addObject:(refMe's NSURLNameKey)) |
---|
088 | set ocidResourceValues to (refMe's NSURL's resourceValuesForKeys:(ocidKeyArray) fromBookmarkData:(ocidItemData)) |
---|
089 | #ファイル名を格納します |
---|
090 | set ocidFileName to (ocidResourceValues's objectForKey:(refMe's NSURLNameKey)) |
---|
091 | (ocidItemURLArray's addObject:(ocidFileName)) |
---|
092 | #DATA=ブックマークをNSURLにして |
---|
093 | set listResponse to (refMe's NSURL's URLByResolvingBookmarkData:(ocidItemData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:(false) |error| :(reference)) |
---|
094 | if (item 2 of listResponse) = (missing value) then |
---|
095 | log "正常処理" |
---|
096 | set ocidBookMarkFilePathURL to (item 1 of listResponse) |
---|
097 | #値を格納する |
---|
098 | (ocidItemURLArray's addObject:(ocidBookMarkFilePathURL)) |
---|
099 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
100 | log (item 2 of listResponse)'s code() as text |
---|
101 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
102 | return "エラーしました" |
---|
103 | end if |
---|
104 | #値の格納 |
---|
105 | (ocidBookMarkArray's addObject:(ocidItemURLArray)) |
---|
106 | end if |
---|
107 | |
---|
108 | end repeat |
---|
109 | else |
---|
110 | return "解凍出来ちゃいましたいつか解凍方法がわかったらやろう" |
---|
111 | end if |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | ##出力用タブ区切りテキストにする |
---|
116 | set ocidOutPutstring to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
---|
117 | #項番用3桁ゼロパディング |
---|
118 | set numNo to 1 as integer |
---|
119 | set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init() |
---|
120 | ocidFormatter's setMinimumIntegerDigits:(3) |
---|
121 | ocidFormatter's setMaximumIntegerDigits:(3) |
---|
122 | |
---|
123 | repeat with itemArray in ocidBookMarkArray |
---|
124 | log itemArray as list |
---|
125 | set strDec3 to (ocidFormatter's stringFromNumber:(numNo)) as text |
---|
126 | set strFileName to (item 1 of itemArray) as text |
---|
127 | set strFilePath to (item 2 of itemArray)'s |path|() as text |
---|
128 | set strFilePathURL to (item 2 of itemArray)'s absoluteString() as text |
---|
129 | # |
---|
130 | (ocidOutPutstring's appendString:(strDec3)) |
---|
131 | (ocidOutPutstring's appendString:("\t")) |
---|
132 | (ocidOutPutstring's appendString:(strFileName)) |
---|
133 | (ocidOutPutstring's appendString:("\t")) |
---|
134 | (ocidOutPutstring's appendString:(strFilePath)) |
---|
135 | (ocidOutPutstring's appendString:("\t")) |
---|
136 | (ocidOutPutstring's appendString:(strFilePathURL)) |
---|
137 | (ocidOutPutstring's appendString:("\n")) |
---|
138 | #カウントアップ |
---|
139 | set numNo to (numNo + 1) as integer |
---|
140 | end repeat |
---|
141 | |
---|
142 | ##保存 書類フォルダ |
---|
143 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
144 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
---|
145 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/LoginItem") isDirectory:(true) |
---|
146 | #保存先ディレクトリ |
---|
147 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
148 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
---|
149 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
150 | if (item 1 of listDone) is true then |
---|
151 | log "保存先ディレクトリ作成 正常処理" |
---|
152 | else if (item 2 of listDone) ≠ (missing value) then |
---|
153 | log (item 2 of listDone)'s code() as text |
---|
154 | log (item 2 of listDone)'s localizedDescription() as text |
---|
155 | return "エラーしました" |
---|
156 | end if |
---|
157 | #ファイル名は日付 |
---|
158 | set strDateNO to doGetDateNo("yyyy-MM-dd") as text |
---|
159 | set strFileName to ("dump_ backgrounditems.btm." & strDateNO & ".tsv") |
---|
160 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false) |
---|
161 | #保存 |
---|
162 | set listDone to ocidOutPutstring's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
---|
163 | if (item 1 of listDone) is true then |
---|
164 | log "保存先ディレクトリ作成 正常処理" |
---|
165 | else if (item 2 of listDone) ≠ (missing value) then |
---|
166 | log (item 2 of listDone)'s code() as text |
---|
167 | log (item 2 of listDone)'s localizedDescription() as text |
---|
168 | return "エラーしました" |
---|
169 | end if |
---|
170 | |
---|
171 | |
---|
172 | ## |
---|
173 | #保存先を開く |
---|
174 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
175 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
---|
176 | if (item 1 of listDone) is true then |
---|
177 | log "正常処理" |
---|
178 | else if (item 2 of listDone) ≠ (missing value) then |
---|
179 | log (item 2 of listDone)'s code() as text |
---|
180 | log (item 2 of listDone)'s localizedDescription() as text |
---|
181 | return "エラーしました" |
---|
182 | end if |
---|
183 | |
---|
184 | |
---|
185 | ##ダイアログを出す |
---|
186 | tell current application |
---|
187 | set strName to name as text |
---|
188 | end tell |
---|
189 | if strName is "osascript" then |
---|
190 | tell application "Finder" |
---|
191 | activate |
---|
192 | end tell |
---|
193 | else |
---|
194 | tell current application |
---|
195 | activate |
---|
196 | end tell |
---|
197 | end if |
---|
198 | set strMes to ("backgrounditems.btmの\n登録項目のダンプ\nタブ区切りテキストですので\n Excelやnumbersにペーストしてください") as text |
---|
199 | set aliasIconPath to (POSIX file "/System/Library/PrivateFrameworks/AMPSharing.framework/Versions/A/PlugIns/SharingPrefsExtension.appex/Contents/Resources/AppIcon.icns") as alias |
---|
200 | try |
---|
201 | set recordResult to (display dialog strMes with title "戻り値です" default answer (ocidOutPutstring as text) buttons {"クリップボードにコピー", "終了", "再実行"} default button "クリップボードにコピー" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record |
---|
202 | on error |
---|
203 | return "エラーしました" |
---|
204 | end try |
---|
205 | if (gave up of recordResult) is true then |
---|
206 | return "時間切れです" |
---|
207 | end if |
---|
208 | ############################## |
---|
209 | #####自分自身を再実行 |
---|
210 | ############################## |
---|
211 | if button returned of recordResult is "再実行" then |
---|
212 | tell application "Finder" |
---|
213 | set aliasPathToMe to (path to me) as alias |
---|
214 | end tell |
---|
215 | run script aliasPathToMe with parameters "再実行" |
---|
216 | end if |
---|
217 | ############################## |
---|
218 | #####値のコピー |
---|
219 | ############################## |
---|
220 | if button returned of recordResult is "クリップボードにコピー" then |
---|
221 | try |
---|
222 | set strText to text returned of recordResult as text |
---|
223 | ####ペーストボード宣言 |
---|
224 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
---|
225 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
---|
226 | appPasteboard's clearContents() |
---|
227 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
---|
228 | on error |
---|
229 | tell application "Finder" |
---|
230 | set the clipboard to strText as text |
---|
231 | end tell |
---|
232 | end try |
---|
233 | end if |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | return 0 |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | ############################## |
---|
244 | ### 今の日付日間 テキスト |
---|
245 | ############################## |
---|
246 | to doGetDateNo(argDateFormat) |
---|
247 | ####日付情報の取得 |
---|
248 | set ocidDate to current application's NSDate's |date|() |
---|
249 | ###日付のフォーマットを定義 |
---|
250 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
---|
251 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
---|
252 | set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo" |
---|
253 | ocidNSDateFormatter's setTimeZone:(ocidTimeZone) |
---|
254 | ocidNSDateFormatter's setDateFormat:(argDateFormat) |
---|
255 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
---|
256 | set strDateAndTime to ocidDateAndTime as text |
---|
257 | return strDateAndTime |
---|
258 | end doGetDateNo |
---|