« [AVAsset]ビデオのサイズ(縦横サイズ)の取得 | トップページ | ファイルサイズ表記 »

[ReName]フォルダにサイズを入れてリネームする


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

#!/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

###Wクリックで起動した場合
on run
  set aliasDefaultLocation to (path to desktop from user domain) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set listFolderPath to (choose folder strMesText 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
  ####################################
  ####フォルタ以外は処理しない
  repeat with itemFolderPath in listFolderPath
    #パス
    set aliasFolderPath to itemFolderPath as alias
    set strDirPath to (POSIX path of aliasFolderPath) as text
    set ocidDirPathStr to (refMe's NSString's stringWithString:(strDirPath))
    set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
    set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath))
    #フォルダ名
    set strDirName to ocidDirPathURL's lastPathComponent() as text
    #コンテナ
    set ocidContainerDirPathURL to ocidDirPathURL's URLByDeletingLastPathComponent()
    #コンテンツの収集の設定
    #オプション:不可視ファイルを除外
    set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
    #プロパティ:ファイルサイズ
    set ocidPropertieArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
(ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey))
(ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey))
(ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey))
    #コンテンツの収集実行
    set appFileManager to refMe's NSFileManager's defaultManager()
    set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference))
    #コンテンツをリストに
    set ocidEmuFileURLArray to ocidEmuDict's allObjects()
    ########
    #【1】ディレクトリを除外して単純に『ファイルだけ』のリストにする
    set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
    set numTotalFileSize to 0 as integer
    #ファイルサイズを収集して加算していく
    repeat with itemFilePathURL in ocidEmuFileURLArray
      set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
      set boolIsRegularFileKey to item 2 of listResult
      if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
        set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileSizeKey) |error|:(reference))
        set numFileSize to (item 2 of listResult) as integer
        set numTotalFileSize to (numTotalFileSize + numFileSize)
      end if
    end repeat
    
log numTotalFileSize as integer
    set numByteUnits to 1000 as integer
    # バイト単位 10.5以前やWindowsターゲットの場合は1024に
    if numTotalFileSize < numByteUnits then
log "b:バイト単位"
      set strFileSize to doRound2Dec(numTotalFileSize)
      set strFileSize to strFileSize & "B"
    else if numTotalFileSize < (numByteUnits * numByteUnits) then
log "kb:キロバイト単位"
      set strFileSize to doRound2Dec(numTotalFileSize / numByteUnits)
      set strFileSize to strFileSize & "KB"
    else if numTotalFileSize < (numByteUnits * numByteUnits * numByteUnits) then
log "mb:メガバイト単位"
      set strFileSize to doRound2Dec(numTotalFileSize / (numByteUnits * numByteUnits))
      set strFileSize to strFileSize & "MB"
    else
log "gb:ギガバイト単位"
      set strFileSize to doRound2Dec(numTotalFileSize / (numByteUnits * numByteUnits * numByteUnits))
      set strFileSize to strFileSize & "GB"
    end if
    #フォルダ名
    set strDirName to (strDirName & "@" & strFileSize) as text
    #リネームURL
    set ocidMoveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strDirName))
    #リネーム
    set listDone to (appFileManager's moveItemAtURL:(ocidDirPathURL) toURL:(ocidMoveDirPathURL) |error|:(reference))
    
  end repeat
end open


to doRound2Dec(argNumber)
  set strDecNo to ((round (argNumber * 100)) / 100) as text
return strDecNo
end doRound2Dec


|

« [AVAsset]ビデオのサイズ(縦横サイズ)の取得 | トップページ | ファイルサイズ表記 »

AppleScript Folder」カテゴリの記事