« UTIからアプリケーションのインストール先を求める | トップページ | [コンソール]ストリームスタート »

[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リストなのでPathArrayにしてから
    ##############################################
    ###ファイル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
        ####URLforKeyで取り出し
        set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
        ###リストからNSURLIsRegularFileKeyBOOLを取り出し
        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

|

« UTIからアプリケーションのインストール先を求める | トップページ | [コンソール]ストリームスタート »

Preview」カテゴリの記事