Preview

[Preview]画像が入っているフォルダを、ファイル名順にソートして開くv4



ダウンロード - open_folderpreview.zip




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

画像フォルダを開く.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(* 
004プレビューアプリのドロップレット用
005画像の入ったフォルダをドロップすると
006ファイル名順にソートしてから
007プレビューアプリで開きます
008v1 初回公開
009v2 ファイル数が1000を超える場合は1000まで開くように変更
010v3 XPCとクイックルックの終了を追加
011v3 UI呼び出しを SystemUIServer に変更した
012v3.1 Windowサイズをリサイズ(縦系)にする処理を加えた
013
014v4.b  考え方を変えて 単体ファイルも開くように作り替えた
015v4.b1 PDFは別窓になるので除外する設定をテスト
016v4.b2 UTIの取得にmissing value対策入れた
017v4.b3 ソートをabsoluteStringからpathに変更
018
019ドロップレット用です
020ファイル>書き出すから『アプリケーション』で保存してください
021*)
022# com.cocolog-nifty.quicktimer.icefloe
023----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
024use AppleScript version "2.8"
025use framework "Foundation"
026use framework "AppKit"
027use framework "UniformTypeIdentifiers"
028use scripting additions
029
030property refMe : a reference to current application
031property strBundleID : "com.apple.Preview"
032
033##############################
034#設定項目 PDFを除外するか?
035property booldExPDF : true as boolean
036
037
038
039##############################
040###Wクリックで起動した場合
041on run
042   set strName to (name of current application) as text
043   if strName is "osascript" then
044      tell application "SystemUIServer" to activate
045   else
046      tell current application to activate
047   end if
048   set aliasDefaultLocation to (path to desktop from user domain) as alias
049   set strPromptText to "画像が入ったフォルダをえらんでください"
050   set strMesText to "画像が入ったフォルダをえらんでください"
051   try
052      tell application "SystemUIServer"
053         activate
054         set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list
055      end tell
056   on error
057      tell application "SystemUIServer" to quit
058      log "エラーしました"
059      return "エラーしました"
060   end try
061   tell application "SystemUIServer" to quit
062   ##############
063   #次工程に渡すArray
064   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
065   #ダイアログで選んだ『フォルダ』を順番に
066   repeat with itemFolderPath in listFolderPath
067      set aliasFolderPath to itemFolderPath as alias
068      set strFolderPath to (POSIX path of aliasFolderPath) as text
069      set ocidFolderPathStr to (refMe's NSString's stringWithString:(strFolderPath))
070      set ocidFolderPath to ocidFolderPathStr's stringByStandardizingPath()
071      set ocidFolderPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFolderPath))
072      #次工程に回すArrayに入れていく
073      (ocidSendNextArray's addObject:(ocidFolderPathURL))
074   end repeat
075   #ここから送られるのはフォルダのファイルパスURLArray
076   doFileOpen(ocidSendNextArray)
077   return
078end run
079
080
081####################################
082#ドロップで起動した場合
083on open listDropFilePath
084   set listDropFilePath to (every item of listDropFilePath) as list
085   ##############
086   #次工程に渡すArray
087   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
088   #ドロップされたファイルパスを全てファイルURLにしてから次に渡す
089   repeat with itemFilePath in listDropFilePath
090      set aliasItemFilePath to itemFilePath as alias
091      set strItemFilePath to (POSIX path of aliasItemFilePath) as text
092      set ocidItemFilePathStr to (refMe's NSString's stringWithString:(strItemFilePath))
093      set ocidItemFilePath to ocidItemFilePathStr's stringByStandardizingPath()
094      set ocidItemFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath))
095      (ocidSendNextArray's addObject:(ocidItemFilePathURL))
096   end repeat
097   #次工程に回す
098   #ここから送られるのはプレビューで開くことができるURLArray
099   doFileOpen(ocidSendNextArray)
100   return
101end open
102
103
104
105############################
106(* #本処理
107argFilePathURLArrayには
108ダイアログで選んだフォルダ
109
110ドロップされたpreviewで開くことができるファイルURLArray
111渡される
112*)
113on doFileOpen(argFilePathURLArray)
114   #プレビューが開くことができるファイルUTIのリストを取得する
115   set refResponse to doGetUTI() as list
116   if refResponse is false then
117      display alert "アプリケーションが開く事ができるUTIの一覧の取得に失敗しました" giving up after 2
118      return "エラー:UTIの取得失敗しました"
119   else
120      set listUTI to refResponse as list
121   end if
122   
123   ####プレビューを一度終了させてから処理開始
124   tell application id strBundleID to activate
125   repeat 5 times
126      try
127         tell application id strBundleID to close every window
128         delay 0.1
129      on error
130         tell application id strBundleID to close every document saving no
131         delay 0.1
132         tell application id strBundleID to quit
133      end try
134   end repeat
135   delay 0.1
136   tell application id strBundleID to quit
137   ####プレビューの半ゾンビ化対策   
138   set ocidRunningApplication to refMe's NSRunningApplication
139   set ocidAppArray to ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)
140   repeat with itemAppArray in ocidAppArray
141      delay 0.2
142      itemAppArray's terminate()
143   end repeat
144   set strAppName to ("プレビュー") as text
145   #クイックルック終了
146   set appRunApp to (refMe's NSRunningApplication)
147   set ocidRunAppArray to appRunApp's runningApplicationsWithBundleIdentifier:("com.apple.quicklook.QuickLookUIService")
148   set ocidRunAppAllArray to ocidRunAppArray's allObjects()
149   repeat with itemRunApp in ocidRunAppAllArray
150      set strLocalizedName to itemRunApp's localizedName() as text
151      if strLocalizedName contains strAppName then
152         itemRunApp's terminate()
153         delay 0.1
154         itemRunApp's forceTerminate()
155      end if
156   end repeat
157   #XPC終了
158   set appRunApp to (refMe's NSRunningApplication)
159   set ocidRunAppArray to appRunApp's runningApplicationsWithBundleIdentifier:("com.apple.appkit.xpc.openAndSavePanelService")
160   set ocidRunAppAllArray to ocidRunAppArray's allObjects()
161   repeat with itemRunApp in ocidRunAppAllArray
162      set strLocalizedName to itemRunApp's localizedName() as text
163      if strLocalizedName contains strAppName then
164         itemRunApp's terminate()
165         delay 0.1
166         itemRunApp's forceTerminate()
167      end if
168   end repeat
169   ####################################
170   #ファイルマネージャ初期化
171   set appFileManager to refMe's NSFileManager's defaultManager()
172   ################################
173   ##テンポラリーをゴミ箱に
174   ################################   
175   (*
176   #前処理で強制終了になっっている可能性があるので
177   #テンポラリーのクリーニングを行いたいが
178   #これを実施するとどうもよく無いので 処理しないことにした
179   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
180   set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
181   set ocidTmpDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.Preview/Data/tmp")
182   set listResult to (appFileManager's trashItemAtURL:(ocidTmpDirPathURL) resultingItemURL:(ocidTmpDirPathURL) |error|:(reference))
183   ##
184   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
185   # 777-->511 755-->493 700-->448 766-->502 
186   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
187   set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
188   *)
189   ################################
190   #パスリストにする
191   (*
192   ここに来た時点で
193   プレビューで開けるUTIを持つファイル
194   
195   フォルダのファイルURLになっている
196   *)
197   ################################
198   ###enumeratorAtURL用のBoolean
199   set ocidFalse to (refMe's NSNumber's numberWithBool:false)
200   set ocidTrue to (refMe's NSNumber's numberWithBool:true)
201   ###ファイルURLのみを格納するリスト
202   set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s init())
203   ###まずは全部のURLArrayに入れる
204   repeat with itemFilePathURL in argFilePathURLArray
205      #フォルダか?確認して
206      set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
207      set boolIsDir to (item 2 of listResourceValue)
208      #フォルダではないなら
209      if boolIsDir is ocidFalse then
210         #そのまま次工程へ回す
211         (ocidFilePathURLAllArray's addObject:(itemFilePathURL))
212         #フォルダなら
213      else if boolIsDir is ocidTrue then
214         ##################################
215         ##プロパティ
216         set ocidPropertieKey to {refMe's NSURLPathKey, refMe's NSURLIsRegularFileKey, refMe's NSURLContentTypeKey}
217         ##オプション(隠しファイルは含まない)
218         set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
219         ####ディレクトリのコンテツを収集(最下層まで)
220         set ocidEmuDict to (appFileManager's enumeratorAtURL:(itemFilePathURL) includingPropertiesForKeys:(ocidPropertieKey) options:(ocidOption) errorHandler:(reference))
221         ###戻り値をリストに格納
222         #   set ocidEmuFileURLArray to ocidEmuDict's allObjects()
223         #      log ocidEmuFileURLArray's |count|()
224         #   (ocidFilePathURLAllArray's addObjectsFromArray:ocidEmuFileURLArray)
225         repeat
226            ###戻り値をリストに格納
227            set ocidEnuURL to ocidEmuDict's nextObject()
228            if ocidEnuURL = (missing value) then
229               exit repeat
230            else
231               #この時点でフォルダのパスは不要なので
232               set listResourceValue to (ocidEnuURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
233               set boolIsDir to (item 2 of listResourceValue)
234               #URLがフォルダなら
235               if boolIsDir is ocidTrue then
236                  #何もしない
237                  #フォルダでは無いなら
238               else if boolIsDir is ocidFalse then
239                  #そのまま次工程へ回す
240                  (ocidFilePathURLAllArray's addObject:(ocidEnuURL))
241               end if
242            end if
243         end repeat
244      end if
245   end repeat
246   
247   
248   ################################
249   #ソート
250   (* 前工程でフォルダのURLからコンテンツの収集を行なって   
251   ここで渡されるのは全てファイル単体のURLArrayが渡される
252   OPEN直前にもソートするが
253   ここでのソートは、1000ファイル超えた場合の最初の1000を取得するために必要
254   *)
255   set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s init()
256   #ここを absoluteString --> path に変更
257   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(true) selector:"localizedStandardCompare:")
258   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
259   ocidFilePathURLAllArray's sortUsingDescriptors:(ocidSortDescriptorArray)
260   ################################################
261   #この時点で全てがファイルURLArray
262   #ファイル数が1000を超える場合は1000までで処理する
263   #ソート後のリストの数を数えて
264   set numCntArray to ocidFilePathURLAllArray's |count|()
265   if numCntArray > 1000 then
266      set strName to (name of current application) as text
267      if strName is "osascript" then
268         tell application "System Events" to activate
269      else
270         tell current application to activate
271      end if
272      tell application "System Events"
273         activate
274         display alert "ファイル数が1000以上なので最初から1000までで処理します" giving up after 2
275         #1000以上URLがある場合は1000コ目までのURLを次に回す
276         set ocidRange to refMe's NSRange's NSMakeRange(0, 1000)
277         set ocidFilePathURLAllArray to ocidFilePathURLAllArray's subarrayWithRange:(ocidRange)
278      end tell
279   end if
280   
281   ################################
282   ####必要なファイルだけのArrayにする
283   ################################
284   #次工程に回すArray
285   set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s init()
286   ####URLの数だけ繰り返し
287   repeat with itemFilePathURL in ocidFilePathURLAllArray
288      ###################不要なファイルをゴミ箱に入れちゃう
289      #拡張子を取得して
290      set ocidExtension to itemFilePathURL's pathExtension()
291      ###URLファイル削除
292      if (ocidExtension as text) is "url" then
293         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
294         ###WindowのサムネイルDB削除
295      else if (ocidExtension as text) is "db" then
296         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
297         ###webloc削除
298      else if (ocidExtension as text) is "webloc" then
299         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
300         ###非表示ファイルは収集していないが
301      else if (ocidExtension as text) is ".DS_Store" then
302         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
303      else
304         ####URLforKeyで取り出し
305         set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
306         ###リストからNSURLIsRegularFileKeyBOOLを取り出し
307         set boolIsRegularFileKey to item 2 of listResult
308         ####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
309         if boolIsRegularFileKey is ocidTrue then
310            if booldExPDF is true then
311               #PDFを除外する 次工程に渡さない
312               if (ocidExtension as text) is "pdf" then
313                  #PDFは次工程に渡さない
314               else
315                  ####リストに追加する
316                  (ocidFilePathURLArray's addObject:(itemFilePathURL))
317               end if
318            else if booldExPDF is false then
319               ####リストに追加する
320               (ocidFilePathURLArray's addObject:(itemFilePathURL))
321            end if
322         end if
323      end if
324   end repeat
325   ##   log ocidFilePathURLArray as list
326   
327   ################################
328   ####ファイルタイプのチェックをする
329   ################################
330   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
331   repeat with itemFilePathURL in ocidFilePathURLArray
332      ####UTIの取得
333      set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
334      set ocidContentType to (item 2 of listResourceValue)
335      set strUTI to ocidContentType's identifier() as text
336      #含まれている=プレビューで開くことができるなら
337      if listUTI contains strUTI then
338         #次工程に回す
339         (ocidSendNextArray's addObject:(itemFilePathURL))
340      end if
341   end repeat
342   
343   
344   #####################################
345   # 並び替え並び替え localizedStandardCompare
346   # 不要なファイルを削除したので再度ソートする
347   #####################################
348   (*
349   compare:
350   caseInsensitiveCompare:
351   localizedCompare:
352   localizedStandardCompare:
353   localizedCaseInsensitiveCompare:
354   *)
355   set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s init()
356   #ここを absoluteString --> path に変更
357   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(true) selector:"localizedStandardCompare:")
358   #   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
359   #   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"compare:")
360   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
361   ocidSendNextArray's sortUsingDescriptors:(ocidSortDescriptorArray)
362   
363   ##############################
364   ####エリアスリストにして
365   ##############################
366   ###ファイルマネジャー初期化
367   ###空のリスト=プレヴューに渡すため
368   set listAliasPath to {} as list
369   ###並び変わったファイルパスを順番に
370   repeat with itemFilePathURL in ocidSendNextArray
371      ###エイリアスにして
372      set aliasFilePath to (itemFilePathURL's absoluteURL()) as alias
373      ####リストに格納していく
374      set end of listAliasPath to aliasFilePath
375   end repeat
376   if listAliasPath is {} then
377      return "Openできる書類はありませんでした"
378   end if
379   
380   
381   ##############################
382   ####起動
383   ##############################
384   try
385      tell application id "com.apple.Preview" to launch
386   on error
387      tell application id "com.apple.Preview" to activate
388   end try
389   ##############################
390   ####プレビューで開く
391   ##############################
392   tell application id "com.apple.Preview"
393      activate
394      set numWindow to count of window
395      if numWindow = 0 then
396         try
397            open listAliasPath
398         on error
399            log "ここでエラー"
400         end try
401      else
402         ####新しいウィンドで開く方法がわからん
403         ####新しいインスタンス生成すれば良いのかな
404         open listAliasPath
405      end if
406   end tell
407   
408   
409   
410   ##############################
411   ####読みやすい感じのwindowサイズにする
412   ##############################
413   #モニタサイズを取得して
414   set ocidScreenArray to refMe's NSScreen's screens()
415   set ocidItemScreen to ocidScreenArray's firstObject()
416   set ocidItemScreenRect to ocidItemScreen's frame()
417   set numItemScreenW to (item 1 of (item 2 of ocidItemScreenRect)) as integer
418   set numItemScreenH to (item 2 of (item 2 of ocidItemScreenRect)) as integer
419   #ここの指数部分を変更することでWindowの縦横比が変更できる
420   set numSetWidh to (numItemScreenW * 0.75) as integer
421   #いい感じのサイズに変更する
422   tell application id "com.apple.Preview"
423      tell front window
424         properties
425         set bounds to {0, 25, numSetWidh, numItemScreenH}
426      end tell
427   end tell
428   tell application "System Events" to quit
429end doFileOpen
430
431
432
433##################################
434#UTIの取得
435#対象のアプリケーションが開くことができる
436#UTIのリストを取得作成します
437##################################
438to doGetUTI()
439   #アプリケーションのURLを取得
440   #NSバンドルをバンドルIDから取得
441   set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
442   #バンドルが取れない=launchDBの不具合?
443   if ocidAppBundle = (missing value) then
444      #NSバンドル取得できなかった場合
445      set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
446      #NSWorkspaceを使って取得する
447      set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
448   else
449      #無駄だけどいちおうFinderにも効いてみる
450      tell application "Finder"
451         try
452            set aliasAppApth to (application file id strBundleID) as alias
453         on error
454            log "アプリケーションが見つかりませんでした"
455            return false
456         end try
457         #えてしてFinderが最強だったりするなこの処理
458         set strAppFilePath to (POSIX path of aliasAppApth) as text
459         set ocidAppFilePathStr to refMe's NSString's stringWithString:(strAppFilePath)
460         set ocidAppFilePath to ocidAppFilePathStr's stringByStandardizingPath()
461         set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppFilePath) isDirectory:(false)
462      end tell
463      set ocidAppPathURL to ocidAppBundle's bundleURL()
464   end if
465   if ocidAppPathURL = (missing value) then
466      log "アプリケーションが見つかりませんでした"
467      return false
468   end if
469   #NSBundleを定義して
470   set ocidAppBundle to refMe's NSBundle's bundleWithURL:(ocidAppPathURL)
471   set ocidInfoDict to ocidAppBundle's infoDictionary()
472   set ocidDocTypeArray to ocidInfoDict's objectForKey:("CFBundleDocumentTypes")
473   if ocidDocTypeArray = (missing value) then
474      log "CFBundleDocumentTypesが見つかりませんでした"
475      return false
476   else
477      #出力戻し値にするリストの初期化
478      set listUTl to {} as list
479      #対応ドキュメントタイプをリストにしていく
480      repeat with itemDocTypeArray in ocidDocTypeArray
481         set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
482         if listContentTypes = (missing value) then
483            #拡張子の指定のみの場合
484            set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
485            set strClassName to ocidExtension's className() as text
486            #子要素の数だけ栗菓子
487            repeat with itemExtension in ocidExtension
488               set strExtension to itemExtension as text
489               #拡張子からUTI生成して
490               set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(strExtension))
491               #バンドルIDを取得
492               set strContentTypes to ocidContentTypes's identifier() as text
493               #戻し用のリストに追加していく
494               set end of listUTl to (strContentTypes)
495            end repeat
496         else
497            #収集できたUTIを順番に
498            repeat with itemContentTypes in listContentTypes
499               set strContentTypes to itemContentTypes as text
500               #戻し用のリストに追加していく
501               set end of listUTl to (strContentTypes)
502            end repeat
503         end if
504      end repeat
505   end if
506   #値を戻す
507   return listUTl
508end doGetUTI
AppleScriptで生成しました

| | コメント (0)

[Preview]画像が入っているフォルダを、ファイル名順にソートして開く(v4 テスト版 最適化中♪)

ファイルも開くことできるようにした

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

画像フォルダを開く.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(* ドロップレット用
004ファイル>書き出すから『アプリケーション』で保存してください
005画像の入ったフォルダをドロップすると
006ファイル名順にソートしてから
007プレビューアプリで開きます
008v1 初回公開
009v2 ファイル数が1000を超える場合は1000まで開くように変更
010v3 XPCとクイックルックの終了を追加
011v3 UI呼び出しを SystemUIServer に変更した
012v3.1 Windowサイズをリサイズ(縦系)にする処理を加えた
013
014v4.b  考え方を変えて 単体ファイルも開くように作り替えた
015v4.b1 PDFは別窓になるので除外する設定をテスト
016*)
017# com.cocolog-nifty.quicktimer.icefloe
018----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
019use AppleScript version "2.8"
020use framework "Foundation"
021use framework "AppKit"
022use framework "UniformTypeIdentifiers"
023use scripting additions
024
025property refMe : a reference to current application
026property strBundleID : "com.apple.Preview"
027
028##############################
029#設定項目 PDFを除外するか?
030property booldExPDF : true as boolean
031
032
033
034##############################
035###Wクリックで起動した場合
036on run
037   set strName to (name of current application) as text
038   if strName is "osascript" then
039      tell application "SystemUIServer" to activate
040   else
041      tell current application to activate
042   end if
043   set aliasDefaultLocation to (path to desktop from user domain) as alias
044   set strPromptText to "画像が入ったフォルダをえらんでください"
045   set strMesText to "画像が入ったフォルダをえらんでください"
046   try
047      tell application "SystemUIServer"
048         activate
049         set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list
050      end tell
051   on error
052      tell application "SystemUIServer" to quit
053      log "エラーしました"
054      return "エラーしました"
055   end try
056   tell application "SystemUIServer" to quit
057   ##############
058   #次工程に渡すArray
059   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
060   #ダイアログで選んだ『フォルダ』を順番に
061   repeat with itemFolderPath in listFolderPath
062      set aliasFolderPath to itemFolderPath as alias
063      set strFolderPath to (POSIX path of aliasFolderPath) as text
064      set ocidFolderPathStr to (refMe's NSString's stringWithString:(strFolderPath))
065      set ocidFolderPath to ocidFolderPathStr's stringByStandardizingPath()
066      set ocidFolderPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFolderPath))
067      #次工程に回すArrayに入れていく
068      (ocidSendNextArray's addObject:(ocidFolderPathURL))
069   end repeat
070   #ここから送られるのはフォルダのファイルパスURLArray
071   doFileOpen(ocidSendNextArray)
072   return
073end run
074
075
076####################################
077#ドロップで起動した場合
078on open listDropFilePath
079   set listDropFilePath to (every item of listDropFilePath) as list
080   ##############
081   #次工程に渡すArray
082   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
083   #ドロップされたファイルパスを全てファイルURLにしてから次に渡す
084   repeat with itemFilePath in listDropFilePath
085      set aliasItemFilePath to itemFilePath as alias
086      set strItemFilePath to (POSIX path of aliasItemFilePath) as text
087      set ocidItemFilePathStr to (refMe's NSString's stringWithString:(strItemFilePath))
088      set ocidItemFilePath to ocidItemFilePathStr's stringByStandardizingPath()
089      set ocidItemFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath))
090      (ocidSendNextArray's addObject:(ocidItemFilePathURL))
091   end repeat
092   #次工程に回す
093   #ここから送られるのはプレビューで開くことができるURLArray
094   doFileOpen(ocidSendNextArray)
095   return
096end open
097
098
099
100############################
101(* #本処理
102argFilePathURLArrayには
103ダイアログで選んだフォルダ
104
105ドロップされたpreviewで開くことができるファイルURLArray
106渡される
107*)
108on doFileOpen(argFilePathURLArray)
109   #プレビューが開くことができるファイルUTIのリストを取得する
110   set listUTI to doGetUTI() as list
111   ####プレビューを一度終了させてから処理開始
112   tell application id strBundleID to activate
113   repeat 5 times
114      try
115         tell application id strBundleID to close every window
116         delay 0.1
117      on error
118         tell application id strBundleID to close every document saving no
119         delay 0.1
120         tell application id strBundleID to quit
121      end try
122   end repeat
123   delay 0.1
124   tell application id strBundleID to quit
125   ####プレビューの半ゾンビ化対策   
126   set ocidRunningApplication to refMe's NSRunningApplication
127   set ocidAppArray to ocidRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)
128   repeat with itemAppArray in ocidAppArray
129      delay 0.2
130      itemAppArray's terminate()
131   end repeat
132   set strAppName to ("プレビュー") as text
133   #クイックルック終了
134   set appRunApp to (refMe's NSRunningApplication)
135   set ocidRunAppArray to appRunApp's runningApplicationsWithBundleIdentifier:("com.apple.quicklook.QuickLookUIService")
136   set ocidRunAppAllArray to ocidRunAppArray's allObjects()
137   repeat with itemRunApp in ocidRunAppAllArray
138      set strLocalizedName to itemRunApp's localizedName() as text
139      if strLocalizedName contains strAppName then
140         itemRunApp's terminate()
141         delay 0.1
142         itemRunApp's forceTerminate()
143      end if
144   end repeat
145   #XPC終了
146   set appRunApp to (refMe's NSRunningApplication)
147   set ocidRunAppArray to appRunApp's runningApplicationsWithBundleIdentifier:("com.apple.appkit.xpc.openAndSavePanelService")
148   set ocidRunAppAllArray to ocidRunAppArray's allObjects()
149   repeat with itemRunApp in ocidRunAppAllArray
150      set strLocalizedName to itemRunApp's localizedName() as text
151      if strLocalizedName contains strAppName then
152         itemRunApp's terminate()
153         delay 0.1
154         itemRunApp's forceTerminate()
155      end if
156   end repeat
157   ####################################
158   #ファイルマネージャ初期化
159   set appFileManager to refMe's NSFileManager's defaultManager()
160   ################################
161   ##テンポラリーをゴミ箱に
162   ################################   
163   (*
164   #前処理で強制終了になっっている可能性があるので
165   #テンポラリーのクリーニングを行いたいが
166   #これを実施するとどうもよく無いので 処理しないことにした
167   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
168   set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
169   set ocidTmpDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Containers/com.apple.Preview/Data/tmp")
170   set listResult to (appFileManager's trashItemAtURL:(ocidTmpDirPathURL) resultingItemURL:(ocidTmpDirPathURL) |error|:(reference))
171   ##
172   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
173   # 777-->511 755-->493 700-->448 766-->502 
174   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
175   set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidTmpDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
176   *)
177   ################################
178   #パスリストにする
179   (*
180   ここに来た時点で
181   プレビューで開けるUTIを持つファイル
182   
183   フォルダのファイルURLになっている
184   *)
185   ################################
186   ###enumeratorAtURL用のBoolean
187   set ocidFalse to (refMe's NSNumber's numberWithBool:false)
188   set ocidTrue to (refMe's NSNumber's numberWithBool:true)
189   ###ファイルURLのみを格納するリスト
190   set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s init())
191   ###まずは全部のURLArrayに入れる
192   repeat with itemFilePathURL in argFilePathURLArray
193      #フォルダか?確認して
194      set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
195      set boolIsDir to (item 2 of listResourceValue)
196      #フォルダではないなら
197      if boolIsDir is ocidFalse then
198         #そのまま次工程へ回す
199         (ocidFilePathURLAllArray's addObject:(itemFilePathURL))
200         #フォルダなら
201      else if boolIsDir is ocidTrue then
202         ##################################
203         ##プロパティ
204         set ocidPropertieKey to {refMe's NSURLPathKey, refMe's NSURLIsRegularFileKey, refMe's NSURLContentTypeKey}
205         ##オプション(隠しファイルは含まない)
206         set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
207         ####ディレクトリのコンテツを収集(最下層まで)
208         set ocidEmuDict to (appFileManager's enumeratorAtURL:(itemFilePathURL) includingPropertiesForKeys:(ocidPropertieKey) options:(ocidOption) errorHandler:(reference))
209         ###戻り値をリストに格納
210         #   set ocidEmuFileURLArray to ocidEmuDict's allObjects()
211         #      log ocidEmuFileURLArray's |count|()
212         #   (ocidFilePathURLAllArray's addObjectsFromArray:ocidEmuFileURLArray)
213         repeat
214            ###戻り値をリストに格納
215            set ocidEnuURL to ocidEmuDict's nextObject()
216            if ocidEnuURL = (missing value) then
217               exit repeat
218            else
219               #この時点でフォルダのパスは不要なので
220               set listResourceValue to (ocidEnuURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
221               set boolIsDir to (item 2 of listResourceValue)
222               #URLがフォルダなら
223               if boolIsDir is ocidTrue then
224                  #何もしない
225                  #フォルダでは無いなら
226               else if boolIsDir is ocidFalse then
227                  #そのまま次工程へ回す
228                  (ocidFilePathURLAllArray's addObject:(ocidEnuURL))
229               end if
230            end if
231         end repeat
232      end if
233   end repeat
234   
235   
236   ################################
237   #ソート
238   (* 前工程でフォルダのURLからコンテンツの収集を行なって   
239   ここで渡されるのは全てファイル単体のURLArrayが渡される
240   OPEN直前にもソートするが
241   ここでのソートは、1000ファイル超えた場合の最初の1000を取得するために必要
242   *)
243   set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s init()
244   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(true) selector:"localizedStandardCompare:")
245   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
246   ocidFilePathURLAllArray's sortUsingDescriptors:(ocidSortDescriptorArray)
247   ################################################
248   #この時点で全てがファイルURLArray
249   #ファイル数が1000を超える場合は1000までで処理する
250   #ソート後のリストの数を数えて
251   set numCntArray to ocidFilePathURLAllArray's |count|()
252   if numCntArray > 1000 then
253      set strName to (name of current application) as text
254      if strName is "osascript" then
255         tell application "System Events" to activate
256      else
257         tell current application to activate
258      end if
259      tell application "System Events"
260         activate
261         display alert "ファイル数が1000以上なので最初から1000までで処理します" giving up after 2
262         #1000以上URLがある場合は1000コ目までのURLを次に回す
263         set ocidRange to refMe's NSRange's NSMakeRange(0, 1000)
264         set ocidFilePathURLAllArray to ocidFilePathURLAllArray's subarrayWithRange:(ocidRange)
265      end tell
266   end if
267   
268   ################################
269   ####必要なファイルだけのArrayにする
270   ################################
271   #次工程に回すArray
272   set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s init()
273   ####URLの数だけ繰り返し
274   repeat with itemFilePathURL in ocidFilePathURLAllArray
275      ###################不要なファイルをゴミ箱に入れちゃう
276      #拡張子を取得して
277      set ocidExtension to itemFilePathURL's pathExtension()
278      ###URLファイル削除
279      if (ocidExtension as text) is "url" then
280         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
281         ###WindowのサムネイルDB削除
282      else if (ocidExtension as text) is "db" then
283         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
284         ###webloc削除
285      else if (ocidExtension as text) is "webloc" then
286         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
287         ###非表示ファイルは収集していないが
288      else if (ocidExtension as text) is ".DS_Store" then
289         set listResult to (appFileManager's trashItemAtURL:(itemFilePathURL) resultingItemURL:(missing value) |error|:(reference))
290      else
291         ####URLforKeyで取り出し
292         set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
293         ###リストからNSURLIsRegularFileKeyBOOLを取り出し
294         set boolIsRegularFileKey to item 2 of listResult
295         ####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
296         if boolIsRegularFileKey is ocidTrue then
297            if booldExPDF is true then
298               #PDFを除外する 次工程に渡さない
299               if (ocidExtension as text) is "pdf" then
300                  #PDFは次工程に渡さない
301               else
302                  ####リストに追加する
303                  (ocidFilePathURLArray's addObject:(itemFilePathURL))
304               end if
305            else if booldExPDF is false then
306               ####リストに追加する
307               (ocidFilePathURLArray's addObject:(itemFilePathURL))
308            end if
309         end if
310      end if
311   end repeat
312   ##   log ocidFilePathURLArray as list
313   
314   ################################
315   ####ファイルタイプのチェックをする
316   ################################
317   set ocidSendNextArray to refMe's NSMutableArray's alloc()'s init()
318   repeat with itemFilePathURL in ocidFilePathURLArray
319      ####UTIの取得
320      set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
321      set ocidContentType to (item 2 of listResourceValue)
322      set strUTI to ocidContentType's identifier() as text
323      #含まれている=プレビューで開くことができるなら
324      if listUTI contains strUTI then
325         #次工程に回す
326         (ocidSendNextArray's addObject:(itemFilePathURL))
327      end if
328   end repeat
329   
330   
331   #####################################
332   # 並び替え並び替え localizedStandardCompare
333   # 不要なファイルを削除したので再度ソートする
334   #####################################
335   (*
336   compare:
337   caseInsensitiveCompare:
338   localizedCompare:
339   localizedStandardCompare:
340   localizedCaseInsensitiveCompare:
341   *)
342   set ocidSortDescriptorArray to refMe's NSMutableArray's alloc()'s init()
343   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:")
344   #   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
345   #   set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"compare:")
346   ocidSortDescriptorArray's addObject:(ocidSortDescriptor)
347   ocidSendNextArray's sortUsingDescriptors:(ocidSortDescriptorArray)
348   
349   ##############################
350   ####エリアスリストにして
351   ##############################
352   ###ファイルマネジャー初期化
353   ###空のリスト=プレヴューに渡すため
354   set listAliasPath to {} as list
355   ###並び変わったファイルパスを順番に
356   repeat with itemFilePathURL in ocidSendNextArray
357      ###エイリアスにして
358      set aliasFilePath to (itemFilePathURL's absoluteURL()) as alias
359      ####リストに格納していく
360      set end of listAliasPath to aliasFilePath
361   end repeat
362   if listAliasPath is {} then
363      return "Openできる書類はありませんでした"
364   end if
365   
366   
367   ##############################
368   ####起動
369   ##############################
370   try
371      tell application id "com.apple.Preview" to launch
372   on error
373      tell application id "com.apple.Preview" to activate
374   end try
375   ##############################
376   ####プレビューで開く
377   ##############################
378   tell application id "com.apple.Preview"
379      activate
380      set numWindow to count of window
381      if numWindow = 0 then
382         try
383            open listAliasPath
384         on error
385            log "ここでエラー"
386         end try
387      else
388         ####新しいウィンドで開く方法がわからん
389         ####新しいインスタンス生成すれば良いのかな
390         open listAliasPath
391      end if
392   end tell
393   
394   
395   
396   ##############################
397   ####読みやすい感じのwindowサイズにする
398   ##############################
399   #モニタサイズを取得して
400   set ocidScreenArray to refMe's NSScreen's screens()
401   set ocidItemScreen to ocidScreenArray's firstObject()
402   set ocidItemScreenRect to ocidItemScreen's frame()
403   set numItemScreenW to (item 1 of (item 2 of ocidItemScreenRect)) as integer
404   set numItemScreenH to (item 2 of (item 2 of ocidItemScreenRect)) as integer
405   #ここの指数部分を変更することでWindowの縦横比が変更できる
406   set numSetWidh to (numItemScreenW * 0.75) as integer
407   #いい感じのサイズに変更する
408   tell application id "com.apple.Preview"
409      tell front window
410         properties
411         set bounds to {0, 25, numSetWidh, numItemScreenH}
412      end tell
413   end tell
414   tell application "System Events" to quit
415end doFileOpen
416
417
418
419##################################
420#UTIの取得
421#対象のアプリケーションが開くことができる
422#UTIのリストを取得作成します
423##################################
424to doGetUTI()
425   ###ファイルマネジャー初期化
426   set appFileManager to refMe's NSFileManager's defaultManager()
427   ###アプリケーションのURLを取得
428   ###NSバンドルをUTIから取得
429   set ocidAppBundle to refMe's NSBundle's bundleWithIdentifier:(strBundleID)
430   if ocidAppBundle = (missing value) then
431      ###NSバンドル取得できなかった場合
432      set appNSWorkspace to refMe's NSWorkspace's sharedWorkspace()
433      set ocidAppPathURL to appNSWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)
434   else
435      set ocidAppPathURL to ocidAppBundle's bundleURL()
436   end if
437   ###Plistのパス
438   set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false
439   set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL)
440   set ocidDocTypeArray to ocidPlistDict's objectForKey:"CFBundleDocumentTypes"
441   if ocidDocTypeArray = (missing value) then
442      set strOutPutText to "missing value" as text
443   else
444      ####リストにする
445      set listUTl to {} as list
446      ###対応ドキュメントタイプをリストにしていく
447      repeat with itemDocTypeArray in ocidDocTypeArray
448         set listContentTypes to (itemDocTypeArray's objectForKey:"LSItemContentTypes")
449         if listContentTypes = (missing value) then
450            ###拡張子の指定のみの場合
451            set ocidExtension to (itemDocTypeArray's objectForKey:"CFBundleTypeExtensions")
452            set strClassName to ocidExtension's className() as text
453            repeat with itemExtension in ocidExtension
454               set strExtension to itemExtension as text
455               set ocidContentTypes to (refMe's UTType's typeWithFilenameExtension:(strExtension))
456               set strContentTypes to ocidContentTypes's identifier() as text
457               set strContentTypes to ("" & strContentTypes & "") as text
458               set end of listUTl to (strContentTypes)
459            end repeat
460         else
461            repeat with itemContentTypes in listContentTypes
462               set strContentTypes to ("" & itemContentTypes & "") as text
463               set end of listUTl to (strContentTypes)
464            end repeat
465         end if
466      end repeat
467   end if
468   return listUTl
469end doGetUTI
AppleScriptで生成しました

| | コメント (0)

[preview]CONTACT SHEETで開く(モニタサイズを考慮するように変更した、要アクセシビリティ)

こんな感じのフォルダを
202504030547381_2880x1800
previewで開きます
202504030547511_1076x683

アクセシビリティ設定は
こちらを参考にしてください

ダウンロード - open_folder2contact20sheet.zip



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

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

| | コメント (0)

[preview.app]ファイル名の変更 縦横pxサイズと解像度を入れる

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

001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004プレビューで開いている画像『全部』が対象です
005ファイル名に縦横pxサイズと解像度を付与します
006
007
008com.cocolog-nifty.quicktimer.icefloe *)
009----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
010use AppleScript version "2.8"
011use framework "Foundation"
012use framework "AppKit"
013use scripting additions
014property refMe : a reference to current application
015
016####パス一覧
017tell application "Preview"
018   set listFilePath to (path of every document) as list
019end tell
020if listFilePath is {} then
021   return "ファイルを開いていません"
022end if
023###【1】ファイル名を変更
024###【2】並び替えるためにエリアスリストにする
025set listAliasFile to {} as list
026repeat with itemFilePath in listFilePath
027   #ファイルパス
028   set strFilePath to itemFilePath as text
029   set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
030   set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
031   set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
032   #ファイル名分解
033   set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
034   set strExtension to ocidFilePath's pathExtension() as text
035   #画像サイズ取得
036   #NSDATA
037   set ocidOption to (refMe's NSDataReadingMappedIfSafe)
038   set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
039   set ocidReadData to (item 1 of listResponse)
040   #NSIMAGE
041   set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
042   set ocidImageSize to ocidReadImage's |size|()
043   #ポイントサイズ
044   set ocidImageWpt to ocidImageSize's width()
045   set ocidImageHpt to ocidImageSize's height()
046   set strSizePt to ("" & ocidImageWpt & "x" & ocidImageHpt & " pt") as text
047   #MMサイズ
048   set numPos to 0.3527777778 as number
049   set strImageWmm to (ocidImageWpt * numPos) as text
050   set strImageHmm to (ocidImageHpt * numPos) as text
051   set ocidDecimalNumW to (refMe's NSDecimalNumber's decimalNumberWithString:(strImageWmm))
052   set ocidDecimalNumH to (refMe's NSDecimalNumber's decimalNumberWithString:(strImageHmm))
053   set strImageWmm to doRound2Dec(ocidDecimalNumW, 2)
054   set strImageHmm to doRound2Dec(ocidDecimalNumH, 2)
055   set strSizeMM to ("" & strImageWmm & "x" & strImageHmm & " mm ") as text
056   #NSBitmapImageRep
057   set ocidBitmapRep to (refMe's NSBitmapImageRep's alloc()'s initWithData:(ocidReadData))
058   #ピクセルサイズ
059   set numImageWpx to ocidBitmapRep's pixelsWide()
060   set numImageHpx to ocidBitmapRep's pixelsHigh()
061   set strImageWpx to numImageWpx as text
062   set strImageHpx to numImageHpx as text
063   #解像度
064   set numResolution to ((numImageWpx / ocidImageWpt) * 72) as integer
065   #ファイル名に追加
066   set strAddSuffix to ("" & strImageWpx & "_" & strImageHpx & "@" & numResolution & "." & strExtension & "") as text
067   #移動先
068   set ocidDistFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:(strAddSuffix))
069   #移動
070   set appFileManager to refMe's NSFileManager's defaultManager()
071   set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error|:(reference))
072   if (item 1 of listDone) is true then
073      set aliasDistFilePath to (ocidDistFilePathURL's absoluteURL()) as alias
074      copy aliasDistFilePath to end of listAliasFile
075   end if
076   
077end repeat
078
079
080####並び替え
081tell application "Finder"
082   set listSortedAliasFilePath to (sort (every item of listAliasFile) by name) as list
083end tell
084####Document file形式のリストをAliasリストに変換
085set listOpenAliasFilePath to {} as list
086repeat with itemAliasFilePath in listSortedAliasFilePath
087   #エイリアスパスにして
088   set aliasFilePath to itemAliasFilePath as alias
089   if aliasFilePathmissing value then
090      copy aliasFilePath to end of listOpenAliasFilePath
091   end if
092end repeat
093#strResponse
094#ドキュメントを閉じて
095tell application "Preview"
096   close every document
097end tell
098
099#ウィンドウを閉じた後で開く
100tell application "Preview"
101   close every window
102   open listOpenAliasFilePath
103   activate
104end tell
105
106
107
108##########################################
109# 小数点以下桁揃え  
110##########################################
111to doRound2Dec(argNoString, argDegi)
112   #set ocidDecimalNum to (refMe's NSDecimalNumber's decimalNumberWithString:(argNoString))
113   set appDeciHandler to refMe's NSDecimalNumberHandler's decimalNumberHandlerWithRoundingMode:(refMe's NSRoundPlain) scale:(argDegi) raiseOnExactness:(true) raiseOnOverflow:(true) raiseOnUnderflow:(true) raiseOnDivideByZero:(true)
114   set ocidRoundedNumber to argNoString's decimalNumberByRoundingAccordingToBehavior:(appDeciHandler)
115   #フォーマットを生成
116   set appNumberFormatter to refMe's NSNumberFormatter's alloc()'s init()
117   set strSetFormat to ("0.") as text
118   repeat argDegi times
119      set strSetFormat to ("" & strSetFormat & "0") as text
120   end repeat
121   #フォーマット適応
122   appNumberFormatter's setPositiveFormat:(strSetFormat)
123   set ocidReturnNO to appNumberFormatter's stringFromNumber:(ocidRoundedNumber)
124   return ocidReturnNO as text
125end doRound2Dec

| | コメント (0)

[プレビュー.app]開いている画像を名前(他)順にソートして開き直す



ダウンロード - sort2open.zip



並換_名前順.scpt

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004プレビューで開いている画像を
005並び替えて開き直す
006com.cocolog-nifty.quicktimer.icefloe *)
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use scripting additions
010
011
012####パス一覧
013tell application "Preview"
014  set listFilePath to (path of every document) as list
015end tell
016if listFilePath is {} then
017return "ファイルを開いていません"
018end if
019####並び替えるためにエリアスリストにする
020set listAliasFilePath to {} as list
021repeat with itemFilePath in listFilePath
022  set strFilePath to itemFilePath as text
023  tell application "Finder"
024    set aliasFilePath to (POSIX file strFilePath) as alias
025  end tell
026  if aliasFilePath  missing value then
027    copy aliasFilePath to end of listAliasFilePath
028  end if
029end repeat
030####並び替え
031tell application "Finder"
032  set listSortedFilePath to (sort (every item of listAliasFilePath) by name)
033end tell
034####Document file形式のリストをAliasリストに変換
035set listAliasFilePath to {} as list
036repeat with itemAliasFilePath in listSortedFilePath
037  set aliasFilePath to itemAliasFilePath as alias
038  if aliasFilePath  missing value then
039    copy aliasFilePath to end of listAliasFilePath
040  end if
041end repeat
042
043#ドキュメントを閉じて
044tell application "Preview"
045  close every document
046end tell
047
048#ウィンドウを閉じた後で開く
049tell application "Preview"
050  close every window
051  open listAliasFilePath
052  activate
053end tell
AppleScriptで生成しました

|

画像フォルダを開く (UI呼び出しをSystemUIServer経由に変更)

画像フォルダを開く.scpt

AppleScript サンプルコード

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

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

|

[Preview]画像の入ったフォルダをコンタクトシートモードで開く

202503080518101_1222x836
こんな感じで開きます


ダウンロード - save_scripteditor.zip


ダウンロード - save_osacompile.zip



com.cocolog-nifty.quicktimer.scpt

AppleScript サンプルコード

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

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

|

[applescript]プレビューで別名保存(画像変換)

Previewでのsave as in
as は保存する画像のUTIをテキストで指定する
PDFで書き出し=別名保存したい場合はこんな感じ
save as.scpt

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002tell application "Preview"
003  tell front document
004  save as "com.adobe.pdf" in file (posix file "/some/dir/some.pdf")
005  end tell
006  tell front document
007  close saving no
008  end tell
009end tell
AppleScriptで生成しました

Previewの別名で保存.scpt

AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004com.cocolog-nifty.quicktimer.icefloe
005プレビューでの別名保存
006ファイルのフォーマットの指定はUTIで指定して
007拡張子がUTIにマッチしている必要がある
008【注意】
009プレビューの別名保存は、同名のファイルがある場合
010『上書き』になって置換=置き換わります。
011保存先は常に別のフォルダにすることをお勧めします。
012*)
013----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
014use AppleScript version "2.8"
015use framework "Foundation"
016use framework "UniformTypeIdentifiers"
017use framework "AppKit"
018use scripting additions
019property refMe : a reference to current application
020set appFileManager to refMe's NSFileManager's defaultManager()
021
022
023#####################
024#設定項目
025#保存ファイル名にピクセルサイズと解像度を入れる
026# 入れる場合は true 入れない場合はfalse
027set boolSizeInName to false as boolean
028
029#####################
030#私が確認した限りだけど保存できるフォーマット
031set recordFileFormatUTI to {|com.adobe.pdf|:"pdf", |public.jpeg|:"jpeg", |public.png|:"png", |public.tiff|:"tif", |public.heic|:"heic", |public.heics|:"heics", |public.pbm|:"pbm", |org.khronos.astc|:"astc", |org.khronos.ktx|:"ktx", |com.microsoft.bmp|:"bmp", |com.microsoft.ico|:"ico", |com.truevision.tga-image|:"tga", |com.compuserve.gif|:"gif", |public.jpeg-2000|:"jp2", |com.apple.icns|:"icns"} as record
032
033#####################
034#DICTにしてキー一覧を取得する
035set ocidFileFormatUtiDict to refMe's NSMutableDictionary's alloc()'s init()
036ocidFileFormatUtiDict's setDictionary:(recordFileFormatUTI)
037set ocidAllKeys to ocidFileFormatUtiDict's allKeys()
038#ソート
039set ocidAllKeys to ocidAllKeys's sortedArrayUsingSelector:("localizedStandardCompare:")
040#ダイアログに渡すようにリストにする
041set listAllKeys to ocidAllKeys as list
042#####################
043#ファイル選択
044set strName to (name of current application) as text
045if strName is "osascript" then
046  tell application "SystemUIServer" to activate
047else
048  tell current application to activate
049end if
050tell application "Finder"
051  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
052end tell
053set strMes to ("ファイルを選んでください") as text
054set strPrompt to ("画像ファイルを選んでください\r com.microsoft.icoは256px以下の正方形\r com.apple.icnsは512px以下の正方形である必要があります") as text
055set listUTI to {"public.image"}
056try
057  tell application "SystemUIServer"
058    activate
059    ### ファイル選択時
060    set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
061  end tell
062on error
063  log "エラーしました"
064  return "エラーしました"
065end try
066if listAliasFilePath is {} then
067  return "選んでください"
068end if
069
070#####################
071#保存先指定
072set strName to (name of current application) as text
073if strName is "osascript" then
074  tell application "SystemUIServer" to activate
075else
076  tell current application to activate
077end if
078set strMes to "フォルダを選んでください" as text
079set strPrompt to "保存先フォルダを選択してください" as text
080tell application "Finder"
081  set aliasContainerDirPath to (container of (item 1 of listAliasFilePath)) as alias
082end tell
083try
084  tell application "SystemUIServer"
085    #Activateは必須
086    activate
087    set aliasSaveDirPath to (choose folder strMes with prompt strPrompt default location aliasContainerDirPath with invisibles and showing package contents without multiple selections allowed) as alias
088  end tell
089on error
090  log "エラーしました"
091  return "エラーしました"
092end try
093set strSaveDirPath to (POSIX path of aliasSaveDirPath) as text
094set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath)
095set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
096set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
097
098#####################
099#フォーマット選択
100set strName to (name of current application) as text
101if strName is "osascript" then
102  tell application "SystemUIServer" to activate
103else
104  tell current application to activate
105end if
106set strTitle to ("選んでください") as text
107set strPrompt to ("ひとつ選んでください\n") as text
108try
109  tell application "SystemUIServer"
110    activate
111    set valueResponse to (choose from list listAllKeys with title strTitle with prompt strPrompt default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" with empty selection allowed without multiple selections allowed)
112  end tell
113on error
114  log "Error choose from list"
115  return false
116end try
117if (class of valueResponse) is boolean then
118  log "Error キャンセルしました"
119  return false
120else if (class of valueResponse) is list then
121  if valueResponse is {} then
122    log "Error 何も選んでいません"
123    return false
124  else
125    set strUTI to (item 1 of valueResponse) as text
126  end if
127end if
128#DICTから拡張子を取得
129set ocidExtnsion to ocidFileFormatUtiDict's valueForKey:(strUTI)
130set strExtnsion to ocidExtnsion as text
131
132
133repeat with itemAliasFilePath in listAliasFilePath
134  
135  #パスをURLにしておく
136  set aliasFilePath to itemAliasFilePath as alias
137  set strFilePath to (POSIX path of aliasFilePath) as text
138  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
139  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
140  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false))
141  set ocidFileName to ocidFilePathURL's lastPathComponent()
142  set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
143  
144  #解像度と縦横PXサイズを取得しておく
145  set ocidImageRep to (refMe's NSImageRep's imageRepsWithContentsOfURL:(ocidFilePathURL))'s firstObject()
146  #PXサイズ
147  set ocidPxW to ocidImageRep's pixelsWide()
148  set ocidPxH to ocidImageRep's pixelsHigh()
149  #Ptサイズ
150  set recordImageSize to ocidImageRep's |size|()
151  set ocidPtW to recordImageSize's width()
152  set ocidPtH to recordImageSize's height()
153  #解像度
154  set numResolution to ((ocidPxW / ocidPtW) * 72) as integer
155  set strResolution to numResolution as text
156  if boolSizeInName is true then
157    #保存用の拡張子
158    set strNewExtension to ("" & (ocidPxW as text) & "x" & (ocidPxH as text) & "@" & strResolution & "ppi." & strExtnsion & "") as text
159  else if boolSizeInName is false then
160    set strNewExtension to strExtnsion as text
161  end if
162  #保存パス
163  set ocidSaveBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(false))
164  set ocidSaveFilePathURL to (ocidSaveBaseFilePathURL's URLByAppendingPathExtension:(strNewExtension))
165  set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as «class furl»
166  ######
167  #サイズ判定
168  if strUTI is "com.apple.icns" or strUTI is "com.microsoft.ico" then
169    if ocidPxW  ocidPxH then
170      return "アイコン画像変換は正方形である必要があります"
171    end if
172  end if
173  if strUTI is "com.apple.icns" then
174    if (ocidPxW as integer) > 512 then
175      return "AppleIcns画像変換は512px以下である必要があります"
176    end if
177  end if
178  if strUTI is "com.microsoft.ico" then
179    if (ocidPxW as integer) > 256 then
180      return "MicrosoftICo画像変換は256px以下である必要があります"
181    end if
182  end if
183  
184  tell application "Preview"
185    set refOpenDoc to open file aliasFilePath
186    #ファイルがOPENされるのをまつ
187    repeat 10 times
188      tell refOpenDoc
189        set strFilePath to path as text
190        set strFileName to name as text
191      end tell
192      tell front document
193        set strCurrentFilePath to path as text
194        set strCurrentFileName to name as text
195      end tell
196      #選択画面で選んだパスと同一になるまで待つ
197      if strCurrentFilePath is strFilePath then
198        exit repeat
199      else
200        delay 0.2
201      end if
202    end repeat
203  end tell
204  
205  tell application "Preview"
206    tell front document
207      save as strUTI in aliasSaveFilePath
208    end tell
209    tell front document
210      close saving no
211    end tell
212  end tell
213  
214end repeat
215
216
217#保存先を開く
218set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
219set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
220
AppleScriptで生成しました

|

[Preview]ファイルをソートしてから開く(ドロップレット)終了方法を追加

画像ファイルが入っているフォルダをドロップすると
ファイル名で表示順をソートしてから開きます
AppleScript サンプルコード

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

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

|

[Preview]ショートカット(Shortcuts)共有シート用 ファイルを他のアプリに渡す

20241023063246_1480x680
20241023062559_1706x591
AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
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
008use AppleScript version "2.8"
009use scripting additions
010
011tell application "Preview"
012  tell front window
013    tell front document
014      set strFilePath to path as text
015    end tell
016  end tell
017end tell
018set aliasFilePath to (POSIX file strFilePath) as alias
019tell application id "com.skitch.skitch"
020  open aliasFilePath
021end tell
AppleScriptで生成しました

|

より以前の記事一覧

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat 2024 Acrobat AddOn Acrobat Annotation Acrobat AppleScript Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat GentechAI Acrobat JS Acrobat JS Word Search Acrobat Maintenance Acrobat Manifest Acrobat Menu Acrobat Merge Acrobat Open Acrobat PDFPage Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat Print Acrobat Python Acrobat Reader Acrobat Reader Localized Acrobat Reference Acrobat Registered Products Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat URL List Mac Acrobat URL List Windows Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin ConfigCode Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin NetWork Admin Permission Admin Pkg Admin Power Management Admin Printer Admin Printer Basic Admin Printer Custompapers Admin SetUp Admin SMB Admin softwareupdate Admin Support Admin System Information Admin TCC Admin Tools Admin Umask Admin Users Admin Volumes Admin XProtect Adobe Adobe AUSST Adobe Bridge Adobe Documents Adobe FDKO Adobe Fonts Adobe Reference Adobe RemoteUpdateManager Adobe Sap Code AppKit Apple AppleScript AppleScript Duplicate AppleScript entire contents AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Video Applications AppStore Archive Archive Keka Attributes Automator BackUp Barcode Barcode Decode Barcode QR Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome Chromedriver CIImage CityCode CloudStorage Color Color NSColor Color NSColorList com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Desktop Position Device Diff Disk do shell script Dock Dock Launchpad DropBox Droplet eMail Encode % Encode Decode Encode HTML Entity Encode UTF8 Error EXIFData exiftool ffmpeg File File Name Finder Finder Window Firefox Folder FolderAction Font List FontCollections Fonts Fonts Asset_Font Fonts ATS Fonts Emoji Fonts Maintenance Fonts Morisawa Fonts Python Fonts Variable Foxit GIF github Guide HTML Icon Icon Assets.car Illustrator Image Events ImageOptim Input Dictionary iPhone iWork Javascript Jedit Ω Json Label Language Link locationd lsappinfo m3u8 Mail Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Microsoft Fonts Microsoft Office Microsoft Office Link Microsoft OneDrive Microsoft Teams Mouse Music Node Notes NSArray NSArray Sort NSAttributedString NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSGraphicsContext Crop NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMetadataItem NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSet NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSUbiquitous NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth PDF PDF Image2PDF PDF MakePDF PDF nUP PDF Pymupdf PDF Pypdf PDFContext PDFDisplayBox PDFImageRep PDFKit PDFKit Annotation PDFKit AnnotationWidget PDFKit DocumentPermissions PDFKit OCR PDFKit Outline PDFKit Start PDFPage PDFPage Rotation PDFView perl Photoshop PlistBuddy pluginkit plutil postalcode PostScript PowerShell prefPane Preview Python Python eyed3 Python pip QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver Script Editor Script Menu SF Symbols character id SF Symbols Entity Shortcuts Shortcuts Events sips Skype Slack Sound Spotlight sqlite StandardAdditions StationSearch Subtitles LRC Subtitles SRT Subtitles VTT Swift swiftDialog System Events System Settings TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom Weather webarchive webp Wifi Windows XML XML EPUB XML HTML XML LSSharedFileList XML LSSharedFileList sfl2 XML LSSharedFileList sfl3 XML objectsForXQuery XML OPML XML Plist XML Plist System Events XML RSS XML savedSearch XML SVG XML TTML XML webloc XML xmllint XML XMP YouTube Zero Padding zoom