001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | # com.cocolog-nifty.quicktimer.icefloe |
---|
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
006 | use AppleScript version "2.8" |
---|
007 | use framework "Foundation" |
---|
008 | use framework "AppKit" |
---|
009 | use framework "UniformTypeIdentifiers" |
---|
010 | use scripting additions |
---|
011 | |
---|
012 | property refMe : a reference to current application |
---|
013 | property strBundleID : "com.apple.Preview" |
---|
014 | |
---|
015 | ###Wクリックで起動した場合 |
---|
016 | on run |
---|
017 | set strName to (name of current application) as text |
---|
018 | if strName is "osascript" then |
---|
019 | tell application "Finder" to activate |
---|
020 | else |
---|
021 | tell current application to activate |
---|
022 | end if |
---|
023 | |
---|
024 | set aliasDefaultLocation to (path to desktop from user domain) as alias |
---|
025 | set strPromptText to "画像が入ったフォルダをえらんでください" |
---|
026 | set strMesText to "画像が入ったフォルダをえらんでください" |
---|
027 | try |
---|
028 | set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list |
---|
029 | on error |
---|
030 | log "エラーしました" |
---|
031 | return "エラーしました" |
---|
032 | end try |
---|
033 | open listFolderPath |
---|
034 | end run |
---|
035 | |
---|
036 | ###ドロップで起動した場合 |
---|
037 | on open listFolderPath |
---|
038 | #################################### |
---|
039 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
040 | ####プレビューを一度終了させてから処理させる |
---|
041 | tell application id strBundleID to activate |
---|
042 | repeat 3 times |
---|
043 | try |
---|
044 | delay 0.2 |
---|
045 | tell application id strBundleID to close every window |
---|
046 | on error |
---|
047 | tell application id strBundleID to close every document saving no |
---|
048 | tell application id strBundleID to quit |
---|
049 | end try |
---|
050 | delay 0.2 |
---|
051 | end repeat |
---|
052 | tell application id strBundleID to quit |
---|
053 | ####プレビューの半ゾンビ化対策 |
---|
054 | set ocidRunningApplication to refMe's NSRunningApplication |
---|
055 | set ocidAppArray to ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID) |
---|
056 | repeat with itemAppArray in ocidAppArray |
---|
057 | delay 0.2 |
---|
058 | itemAppArray's terminate() |
---|
059 | end repeat |
---|
060 | |
---|
061 | |
---|
062 | |
---|
063 | #################################### |
---|
064 | ####フォルタ以外は処理しない |
---|
065 | repeat with itemFolderPath in listFolderPath |
---|
066 | set aliasFolderPath to itemFolderPath as alias |
---|
067 | set strFilePath to (POSIX path of aliasFolderPath) as text |
---|
068 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
---|
069 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
---|
070 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
---|
071 | set listBoole to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference)) |
---|
072 | set boolIsDir to (item 2 of listBoole) as boolean |
---|
073 | if boolIsDir is true then |
---|
074 | log "フォルダなので処理開始" |
---|
075 | else |
---|
076 | set strName to (name of current application) as text |
---|
077 | if strName is "osascript" then |
---|
078 | tell application "Finder" to activate |
---|
079 | else |
---|
080 | tell current application to activate |
---|
081 | end if |
---|
082 | display alert "フォルダ以外は処理しないで終了します" giving up after 2 |
---|
083 | return "" |
---|
084 | end if |
---|
085 | end repeat |
---|
086 | |
---|
087 | ################################ |
---|
088 | ##テンポラリーをゴミ箱に |
---|
089 | ################################ |
---|
090 | |
---|
091 | (* |
---|
092 | ###ファイルマネジャー初期化 |
---|
093 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
094 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
---|
095 | set ocidTmpDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.Preview/Data/tmp") |
---|
096 | set listResult to (appFileManager's trashItemAtURL:(ocidTmpDirPathURL) resultingItemURL:(ocidTmpDirPathURL) |error| :(reference)) |
---|
097 | ## |
---|
098 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
099 | # 777-->511 755-->493 700-->448 766-->502 |
---|
100 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
---|
101 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
102 | *) |
---|
103 | ################################ |
---|
104 | ##【本処理】フォルダの数だけ繰り返し |
---|
105 | ################################ |
---|
106 | ###enumeratorAtURL用のBoolean用 |
---|
107 | set ocidFalse to (refMe's NSNumber's numberWithBool:false) |
---|
108 | set ocidTrue to (refMe's NSNumber's numberWithBool:true) |
---|
109 | |
---|
110 | ###ファイルURLのみを格納するリスト |
---|
111 | set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
---|
112 | ###まずは全部のURLをArrayに入れる |
---|
113 | repeat with itemFolderPath in listFolderPath |
---|
114 | ######パス フォルダのエイリアス |
---|
115 | set aliasDirPath to itemFolderPath as alias |
---|
116 | ###UNIXパスにして |
---|
117 | set strDirPath to (POSIX path of aliasDirPath) as text |
---|
118 | ###Stringsに |
---|
119 | set ocidDirPathStr to (refMe's NSString's stringWithString:(strDirPath)) |
---|
120 | ###パス確定させて |
---|
121 | set ocidDirPath to ocidDirPathStr's stringByStandardizingPath |
---|
122 | ###NSURLに |
---|
123 | set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:(true)) |
---|
124 | ################################## |
---|
125 | ##プロパティ |
---|
126 | set ocidPropertieKey to {refMe's NSURLPathKey, refMe's NSURLIsRegularFileKey, refMe's NSURLContentTypeKey} |
---|
127 | ##オプション(隠しファイルは含まない) |
---|
128 | set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles |
---|
129 | ####ディレクトリのコンテツを収集(最下層まで) |
---|
130 | set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieKey) options:(ocidOption) errorHandler:(reference)) |
---|
131 | ###戻り値をリストに格納 |
---|
132 | #set ocidEmuFileURLArray to ocidEmuDict's allObjects() |
---|
133 | # (ocidFilePathURLAllArray's addObjectsFromArray:ocidEmuFileURLArray) |
---|
134 | repeat |
---|
135 | set ocidEnuURL to ocidEmuDict's nextObject() |
---|
136 | if ocidEnuURL = (missing value) then |
---|
137 | exit repeat |
---|
138 | else |
---|
139 | (ocidFilePathURLAllArray's addObject:ocidEnuURL) |
---|
140 | end if |
---|
141 | end repeat |
---|
142 | end repeat |
---|
143 | ################################################ |
---|
144 | ####ファイル数が1000を超える場合は1000までで処理する |
---|
145 | ################################################ |
---|
146 | set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
147 | set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"localizedStandardCompare:") |
---|
148 | ocidSortDescriptorArray's addObject:(ocidSortDescriptor) |
---|
149 | ocidFilePathURLAllArray's sortUsingDescriptors:(ocidSortDescriptorArray) |
---|
150 | set numCntArray to ocidFilePathURLAllArray's |count|() |
---|
151 | if numCntArray > 1000 then |
---|
152 | set strName to (name of current application) as text |
---|
153 | if strName is "osascript" then |
---|
154 | tell application "Finder" to activate |
---|
155 | else |
---|
156 | tell current application to activate |
---|
157 | end if |
---|
158 | display alert "ファイル数が1000以上なので最初から1000までで処理します" giving up after 2 |
---|
159 | set ocidRange to refMe's NSRange's NSMakeRange(0, 1000) |
---|
160 | set ocidFilePathURLAllArray to ocidFilePathURLAllArray's subarrayWithRange:(ocidRange) |
---|
161 | end if |
---|
162 | |
---|
163 | ################################ |
---|
164 | ####必要なファイルだけのArrayにする |
---|
165 | ################################ |
---|
166 | set ocidFilePathURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
---|
167 | ####URLの数だけ繰り返し |
---|
168 | repeat with itemFilePathURL in ocidFilePathURLAllArray |
---|
169 | ###################不要なファイルをゴミ箱に入れちゃう |
---|
170 | ####拡張子取って |
---|
171 | set ocidExtension to itemFilePathURL's pathExtension() |
---|
172 | |
---|
173 | ###非表示ファイルは収集していないが |
---|
174 | if (ocidExtension as text) is ".DS_Store" then |
---|
175 | set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error| :(reference)) |
---|
176 | else |
---|
177 | ####URLをforKeyで取り出し |
---|
178 | set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error| :(reference)) |
---|
179 | ###リストからNSURLIsRegularFileKeyのBOOLを取り出し |
---|
180 | set boolIsRegularFileKey to item 2 of listResult |
---|
181 | ####ファイルのみを(ディレクトリやリンボリックリンクは含まない) |
---|
182 | if boolIsRegularFileKey is ocidTrue then |
---|
183 | ####リストにする |
---|
184 | (ocidFilePathURLArray's addObject:(itemFilePathURL)) |
---|
185 | end if |
---|
186 | end if |
---|
187 | end repeat |
---|
188 | ## log ocidFilePathURLArray as list |
---|
189 | ################################ |
---|
190 | ####ファイルタイプのチェックをする |
---|
191 | ################################ |
---|
192 | ###ファイルマネジャー初期化 |
---|
193 | set listUTI to doGetUTI() as list |
---|
194 | ###ファイルURLのみを格納するリスト |
---|
195 | set ocidFilePathURLArrayM to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
---|
196 | repeat with itemFilePathURL in ocidFilePathURLArray |
---|
197 | ####UTIの取得 |
---|
198 | set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
---|
199 | set ocidContentType to (item 2 of listResourceValue) |
---|
200 | set strUTI to (ocidContentType's identifier) as text |
---|
201 | if listUTI contains strUTI then |
---|
202 | (ocidFilePathURLArrayM's addObject:(itemFilePathURL)) |
---|
203 | end if |
---|
204 | end repeat |
---|
205 | |
---|
206 | log ocidFilePathURLArrayM's firstObject()'s absoluteString as text |
---|
207 | |
---|
208 | ## log ocidFilePathURLArrayM as list |
---|
209 | ############################## |
---|
210 | ####並び替え並び替え compare |
---|
211 | ############################## |
---|
212 | (* |
---|
213 | compare: |
---|
214 | caseInsensitiveCompare: |
---|
215 | localizedCompare: |
---|
216 | localizedStandardCompare: |
---|
217 | localizedCaseInsensitiveCompare: |
---|
218 | *) |
---|
219 | |
---|
220 | set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
221 | set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:") |
---|
222 | #ocidSortDescriptorArray's addObject:(ocidSortDescriptor) |
---|
223 | # set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"compare:") |
---|
224 | ocidSortDescriptorArray's addObject:(ocidSortDescriptor) |
---|
225 | ocidFilePathURLArrayM's sortUsingDescriptors:(ocidSortDescriptorArray) |
---|
226 | log ocidFilePathURLArrayM's firstObject()'s absoluteString as text |
---|
227 | |
---|
228 | ############################## |
---|
229 | ####エリアスリストにして |
---|
230 | ############################## |
---|
231 | ###ファイルマネジャー初期化 |
---|
232 | ###空のリスト=プレヴューに渡すため |
---|
233 | set listAliasPath to {} as list |
---|
234 | ###並び変わったファイルパスを順番に |
---|
235 | repeat with itemFilePathURL in ocidFilePathURLArrayM |
---|
236 | ###エイリアスにして |
---|
237 | set aliasFilePath to (itemFilePathURL's absoluteURL()) as alias |
---|
238 | ####リストに格納していく |
---|
239 | set end of listAliasPath to aliasFilePath |
---|
240 | end repeat |
---|
241 | if listAliasPath is {} then |
---|
242 | return "Openできる書類はありませんでした" |
---|
243 | end if |
---|
244 | ############################## |
---|
245 | ####起動 |
---|
246 | ############################## |
---|
247 | try |
---|
248 | tell application id "com.apple.Preview" to launch |
---|
249 | on error |
---|
250 | tell application id "com.apple.Preview" to activate |
---|
251 | end try |
---|
252 | ############################## |
---|
253 | ####プレビューで開く |
---|
254 | ############################## |
---|
255 | tell application id "com.apple.Preview" |
---|
256 | activate |
---|
257 | set numWindow to count of window |
---|
258 | if numWindow = 0 then |
---|
259 | try |
---|
260 | open listAliasPath |
---|
261 | on error |
---|
262 | log "ここでエラー" |
---|
263 | end try |
---|
264 | else |
---|
265 | ####新しいウィンドで開く方法がわからん |
---|
266 | ####新しいインスタンス生成すれば良いのかな |
---|
267 | open listAliasPath |
---|
268 | end if |
---|
269 | end tell |
---|
270 | |
---|
271 | end open |
---|
272 | |
---|
273 | |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | to doGetUTI() |
---|
278 | ###ファイルマネジャー初期化 |
---|
279 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
280 | ###アプリケーションのURLを取得 |
---|
281 | ###NSバンドルをUTIから取得 |
---|
282 | set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID) |
---|
283 | if ocidAppBundle = (missing value) then |
---|
284 | ###NSバンドル取得できなかった場合 |
---|
285 | set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
286 | set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID) |
---|
287 | else |
---|
288 | set ocidAppPathURL to ocidAppBundle's bundleURL() |
---|
289 | end if |
---|
290 | ###Plistのパス |
---|
291 | set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false |
---|
292 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |
---|
293 | set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes" |
---|
294 | if ocidDocTypeArray = (missing value) then |
---|
295 | set strOutPutText to "missing value" as text |
---|
296 | else |
---|
297 | ####リストにする |
---|
298 | set listUTl to {} as list |
---|
299 | ###対応ドキュメントタイプをリストにしていく |
---|
300 | repeat with itemDocTypeArray in ocidDocTypeArray |
---|
301 | set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes") |
---|
302 | if listContentTypes = (missing value) then |
---|
303 | ###拡張子の指定のみの場合 |
---|
304 | set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions") |
---|
305 | set strClassName to ocidExtension's className() as text |
---|
306 | repeat with itemExtension in ocidExtension |
---|
307 | set strExtension to itemExtension as text |
---|
308 | set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(strExtension)) |
---|
309 | set strContentTypes to ocidContentTypes's identifier() as text |
---|
310 | set strContentTypes to ("" & strContentTypes & "") as text |
---|
311 | set end of listUTl to (strContentTypes) |
---|
312 | end repeat |
---|
313 | else |
---|
314 | repeat with itemContentTypes in listContentTypes |
---|
315 | set strContentTypes to ("" & itemContentTypes & "") as text |
---|
316 | set end of listUTl to (strContentTypes) |
---|
317 | end repeat |
---|
318 | end if |
---|
319 | end repeat |
---|
320 | end if |
---|
321 | return listUTl |
---|
322 | end doGetUTI |
---|