[Preview]画像ファイルの入っているフォルダを開く(半ゾンビ対策入り)
#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
on run
set aliasDefaultLocation to (path to desktop from user domain) as alias
set strPromptText to "フォルダをえらんでください"
set strPromptText to "フォルダをえらんでください"
try
set listFolderPath to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list
on error
log "エラーしました"
return
end try
open listFolderPath
end run
on open listFolderPath
####プレビューの半ゾンビ化対策
tell application id "com.apple.Preview"
set numCntWindow to count of window
end tell
####終了させてから処理させる
if numCntWindow = 0 then
tell application id "com.apple.Preview"
quit
end tell
end if
###enumeratorAtURL用のBoolean用
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)
###ファイルマネジャー初期化
set appFileManager to refMe's NSFileManager's defaultManager()
####フォルダの数だけ繰り返し
repeat with itemFolderPath in listFolderPath
######パス
set aliasDirPath to itemFolderPath as alias
set strDirPath to POSIX path of aliasDirPath as text
set ocidDirPath to (refMe's NSString's stringWithString:strDirPath)
set ocidDirPath to ocidDirPath's stringByStandardizingPath
set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true)
##プロパティ
set ocidPropertieKey to {refMe's NSURLPathKey, refMe's NSURLIsRegularFileKey}
##オプション
set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
####ディレクトリのコンテツを収集
set ocidEmuDict to (appFileManager's enumeratorAtURL:ocidDirPathURL includingPropertiesForKeys:ocidPropertieKey options:ocidOption errorHandler:(reference))
###戻り値
set ocidEmuFileURLArray to ocidEmuDict's allObjects()
##############################################
##URLリストなのでPathのArrayにしてから
##############################################
###ファイルURLのみを格納するリスト
set ocidFilePathArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
repeat with itemEmuFileURL in ocidEmuFileURLArray
####拡張子取って
set ocidExtension to itemEmuFileURL's pathExtension()
if (ocidExtension as text) is "url" then
set listResult to (appFileManager's trashItemAtURL:itemEmuFileURL resultingItemURL:(missing value) |error|:(reference))
else if (ocidExtension as text) is "db" then
set listResult to (appFileManager's trashItemAtURL:itemEmuFileURL resultingItemURL:(missing value) |error|:(reference))
else
####URLをforKeyで取り出し
set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
###リストからNSURLIsRegularFileKeyのBOOLを取り出し
set boolIsRegularFileKey to item 2 of listResult
####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
if boolIsRegularFileKey is ocidTrue then
###パスにして
set ocidFilePath to itemEmuFileURL's |path|()
####リストにする
(ocidFilePathArray's addObject:ocidFilePath)
end if
end if
end repeat
##############################
####並び替え
##############################
set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:(true) selector:"localizedStandardCompare:")
(ocidFilePathArray's sortUsingDescriptors:{ocidSortDescriptor})
##############################
####エリアスリストにして
##############################
set listAliasPath to {} as list
repeat with itemFilePathArray in ocidFilePathArray
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:itemFilePathArray isDirectory:false)
set aliasFilePath to ocidFilePathURL as alias
copy aliasFilePath to end of listAliasPath
end repeat
##############################
####起動
##############################
tell application id "com.apple.Preview"
launch
end tell
##############################
####プレビューで開く
##############################
tell application "Preview"
activate
set numWindow to count of window
if numWindow = 0 then
try
open listAliasPath
on error
log "ここでエラー"
end try
else
####新しいウィンドで開く方法がわからん
open listAliasPath
end if
end tell
end repeat
end open
| 固定リンク
「Preview」カテゴリの記事
- [Preview]ファイルをソートしてから開く(ドロップレット)終了方法を追加(2025.01.18)
- [Preview]ショートカット(Shortcuts)共有シート用 ファイルを他のアプリに渡す(2024.10.23)
- [qfilter]Quartz Filter 解像度別詰め合わせ(2024.07.19)
- [Preview]ファイルをソートしてから開く(sortDescriptorWithKey: absoluteStringの不具合対応)(2024.04.19)
- 画像が入っているフォルダを開く(ファイル名順)少し修正(2024.02.07)