AppleScript Folder

フォルダ内一覧HTML(サイズ表記修正)


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

#!/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 framework "AppKIt"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


###Wクリックで起動した場合
on run
  ###ダイアログを前面に出す
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  ##デフォルト=デスクトップ
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
  set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set aliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias
  on error
log "エラーしました"
return "エラーしました"
  end try
open aliasDirPath
end run


on open aliasDirPath
  ##パス
  set aliasDirPath to aliasDirPath as alias
  set strDirPath to (POSIX path of aliasDirPath) 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 strDirPathURL to ocidDirPathURL's absoluteString() as text
  ##フォルダ判定
  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
  set boolIsDir to (item 2 of listBoole) as boolean
  if boolIsDir is false then
return "フォルダ以外です処理を終了します"
  end if
  ##フォルダ名→保存先HTMLパス
  set ocidDirName to ocidDirPathURL's lastPathComponent()
  set ocidSaveFileName to refMe's NSMutableString's alloc()'s initWithCapacity:0
ocidSaveFileName's appendString:("_ファイルリスト")
ocidSaveFileName's appendString:(ocidDirName)
  set ocidBaseFilePathURL to ocidDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)
  set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
  
  ##ファイルの各種プロパティを取得
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  set ocidPropertieArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidPropertieArray's addObject:(refMe's NSURLPathKey)
ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLCreationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLContentModificationDateKey)
ocidPropertieArray's addObject:(refMe's NSURLNameKey)
ocidPropertieArray's addObject:(refMe's NSURLContentTypeKey)
ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey)
ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey)
  ########################################
  ##コンテンツの収集 A 第一階層のみの場合
  ########################################
  (*
set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) |error|:(reference))
set ocidFilePathURLArray to item 1 of listResponse
#パスリストをファイル名でソート並び替え
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:")
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
*)
  ########################################
  ##コンテンツの収集 B 最下層までの場合
  ########################################
  set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference)
  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
  set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
ocidFilePathURLAllArray's addObjectsFromArray:(ocidEmuFileURLArray)
  #
  set ocidFilePathURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  repeat with itemFilePathURL in ocidFilePathURLAllArray
    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
      ####リストにする
(ocidFilePathURLArray's addObject:(itemFilePathURL))
    end if
  end repeat
  #パスリストをファイル名でソート並び替え absoluteString localizedStandardCompare
  set ocidDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:")
  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
  
  ##############################
  # XML 生成開始
  ##############################
  #XML初期化
  set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
  # DTD付与
  set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("html")
ocidXMLDoc's setDTD:(ocidDTD)
  # XML主要部分を生成
  set ocidRootElement to doMakeRootElement()
  #ボディエレメント
  set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
  #ヘッダー
  set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header")
ocidHeaderElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header")
ocidHeaderElement's addAttribute:(ocidAddNode)
ocidBodyElement's addChild:(ocidHeaderElement)
  #アーティクル
  set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article")
ocidArticleElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article")
ocidArticleElement's addAttribute:(ocidAddNode)
ocidBodyElement's addChild:(ocidArticleElement)
  #フッター
  set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer")
ocidFooterElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer")
ocidFooterElement's addAttribute:(ocidAddNode)
  #リンク付与(不要なら削除可)
  set ocidAElement to refMe's NSXMLElement's elementWithName:("a")
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
(ocidAElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
  set strContents to ("AppleScriptで生成しました") as text
(ocidAElement's setStringValue:(strContents))
ocidFooterElement's addChild:(ocidAElement)
ocidBodyElement's addChild:(ocidFooterElement)
  ##############################
  # TABLE コンテンツ部分生成開始
  ##############################
  #テーブル部生成開始
  set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
  #【caption】
  set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
ocidCaptionElement's setStringValue:("【ファイルリスト】: 行番号に※表示はCMYKデータ(PDFを除く)")
ocidTableElement's addChild:(ocidCaptionElement)
  #【colgroup】
  set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
  #テーブルのタイトル部
  set listColName to {"行番号", "ファイル名", "サイズ", "種類", "作成日", "修正日"} as list
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    #【col】col生成
    set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
  end repeat
  #テーブルエレメントに追加
ocidTableElement's addChild:(ocidColgroupElement)
  #【thead】
  set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
  #TR
  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    #ここはTDではなくてTHを利用
    set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
    #値を入れる
(ocidAddElement's setStringValue:(itemColName))
    #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
  end repeat
  #TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
  #THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
  #【tbody】
  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
  #行番号
  set numCntLineNo to 1 as integer
  #合計ファイルサイズ用
  set numAllFileSize to 0 as integer
  #ファイルのパスの数だけ繰り返し
  repeat with itemFilePathURL in ocidSortedArray
    ##UTIを求めて
    set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
    set ocidContentType to (item 2 of listResourceValue)
    set strUTI to (ocidContentType's identifier) as text
    set strUTILIST to "public.jpeg, com.adobe.photoshop-image,public.tiff,com.adobe.pd" as text
    if strUTILIST contains strUTI then
log "CMYKチェック対象ファイル"
      set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(itemFilePathURL))
      set ocidImageRepArray to ocidReadImage's representations()
      set ocidImageRep to ocidImageRepArray's firstObject()
      set ocidColorSpace to ocidImageRep's colorSpace()
      #シンプルにコンポーネント番号で判定する場合
      # 1 = GS 3=RGB 4=CMYK
      set numColorCompNo to ocidColorSpace's numberOfColorComponents as integer
      #colorSpaceModelで判定したい場合
      # set ocidColorModel to ocidColorSpace's colorSpaceModel()
      # if ocidColorModel = (refMe's NSColorSpaceModelCMYK) then
log numColorCompNo
      if numColorCompNo = 4 then
log "CMYYKデータです"
        set ocidReadImage to (missing value)
        set ocidImageRep to (missing value)
        set boolCMYK to true as boolean
      else
        set boolCMYK to false as boolean
      end if
    else
      set boolCMYK to false as boolean
    end if
    ##
    #ファイルのリソースを取得
    set listResponse to (itemFilePathURL's resourceValuesForKeys:(ocidPropertieArray) |error|:(reference))
    set ocidValueDict to (item 1 of listResponse)
    #TRの開始
    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
    #【行番号】をTHでセット
    set strZeroSupp to ("00") as text
    set strZeroSupp to ("00" & numCntLineNo) as text
    set strLineNO to (text -3 through -1 of strZeroSupp) as text
    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
    if boolCMYK is true then
      set strSetLineNO to (strLineNO & "*") as text
    else if boolCMYK is false then
      set strSetLineNO to (strLineNO) as text
    end if
(ocidThElement's setStringValue:(strSetLineNO))
(ocidTrElement's addChild:(ocidThElement))
    #【ファイル名】をTDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLNameKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイル名"))
(ocidTdElement's addAttribute:(ocidAddNode))
    ####
    set itemFilePath to itemFilePathURL's |path|
    set ocidRange to (itemFilePath's rangeOfString:(ocidDirPath))
    set numLength to (|length| of ocidRange) + 1 as integer
    set ocidRlativePath to (itemFilePath's substringFromIndex:(numLength))
    
    #↑のTDの内容=ファイル名にリンクを付与
    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
    set strHref to ("./" & ocidRlativePath) as text
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strHref))
(ocidAElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
    set strHref to (ocidRlativePath) as text
(ocidAElement's setStringValue:(strHref))
    #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
    #TDをTRにセット
(ocidTrElement's addChild:(ocidTdElement))
    #【ファイルサイズ】TD
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLFileSizeKey))
    #ファイルサイズの合計に加算
    set numAllFileSize to (numAllFileSize + (ocidValue as integer))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイルサイズ"))
(ocidTdElement's addAttribute:(ocidAddNode))
    #単位による分岐
    set numFileSize to ocidValue as integer
    #
    set numByteUnits to 1000 as integer
    # バイト単位 10.5以前やWindowsターゲットの場合は1024に
    
    if (numFileSize) < numByteUnits then
log "b:バイト単位"
      set strFileSize to doRound2Dec(numFileSize)
      set strValue to (strFileSize & " b") as text
    else if numFileSize < (numByteUnits * numByteUnits) then
log "kb:キロバイト単位"
      set strFileSize to doRound2Dec(numFileSize / numByteUnits)
      set strValue to (strFileSize & "kb") as text
    else if numFileSize < (numByteUnits * numByteUnits * numByteUnits) then
log "mb:メガバイト単位"
      set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits))
      set strValue to (strFileSize & " mb") as text
    else
log "gb:ギガバイト単位"
      set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits * numByteUnits))
      set strValue to (strFileSize & " gb") as text
    end if
(ocidTdElement's setStringValue:(strValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【種類】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("種類"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidValue))
(ocidTrElement's addChild:(ocidTdElement))
    #【作成日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLCreationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("作成"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #【修正日】TDでセット
    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentModificationDateKey))
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("修正日"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
(ocidTdElement's addAttribute:(ocidAddNode))
    set strDate to (ocidValue as date) as text
(ocidTdElement's setStringValue:(strDate))
(ocidTrElement's addChild:(ocidTdElement))
    #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
    #項目番号のカウントアップ
    set numCntLineNo to (numCntLineNo + 1) as integer
  end repeat
  #TBODYをテーブルにセット
ocidTableElement's addChild:(ocidTbodyElement)
  #【tfoot】 TRで
  set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot")
  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
  #項目数を取得して
  set numCntCol to (count of listColName) as integer
  #colspan指定して1行でセット
  set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
(ocidThElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text))
(ocidThElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
(ocidThElement's addAttribute:(ocidAddNode))
  #
  set numByteUnits to 1000 as integer
  # バイト単位 10.5以前やWindowsターゲットの場合は1024に
  if numAllFileSize < numByteUnits then
log "b:バイト単位"
    set strFileSize to doRound2Dec(numAllFileSize)
    set strValue to "フォルダ合計:" & strFileSize & " b" as text
    
  else if numAllFileSize < (numByteUnits * numByteUnits) then
log "kb:キロバイト単位"
    set strFileSize to doRound2Dec(numAllFileSize / numByteUnits)
    set strValue to "フォルダ合計:" & strFileSize & " kb" as text
    
  else if numAllFileSize < (numByteUnits * numByteUnits * numByteUnits) then
log "mb:メガバイト単位"
    set strFileSize to doRound2Dec(numAllFileSize / (numByteUnits * numByteUnits))
    set strValue to "フォルダ合計:" & strFileSize & " mb" as text
  else
log "gb:ギガバイト単位"
    set strFileSize to doRound2Dec(numAllFileSize / (numByteUnits * numByteUnits * numByteUnits))
    set strValue to "フォルダ合計:" & strFileSize & " gb" as text
  end if
  
  
  
  
  
  
  
  
  if (numAllFileSize) < 102400 then
    set numValue to (numAllFileSize / 1024) as integer
    set strValue to "フォルダ合計:" & numValue & " kb" as text
  else if (numAllFileSize) > (10240 * 100000) then
    set numValue to (numAllFileSize / (10240 * 100000))
    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
(ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
(ocidFormatter's setMinimumFractionDigits:(2))
(ocidFormatter's setMaximumFractionDigits:(2))
    set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
    set strValue to "フォルダ合計:" & strValue & " gb" as text
  else
    set numValue to (numAllFileSize / 1024000) as integer
    set strValue to "フォルダ合計:" & numValue & " mb" as text
  end if
ocidThElement's setStringValue:(strValue)
  
  #THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
  #TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
  #TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
  # 出来上がったテーブルをArticleエレメントにセット
ocidArticleElement's addChild:(ocidTableElement)
  #
ocidRootElement's addChild:(ocidBodyElement)
  ##############################
  # TABLE
  ##############################
  #ROOTエレメントをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #読み取りやすい表示
  set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
  #保存
  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
  set ocidSaveFilePath to ocidSaveFilePathURL's |path|
  set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
  set ocidContainerDirPath to ocidContainerDirPathURL's |path|
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  set boolDone to appSharedWorkspace's selectFile:(ocidSaveFilePath) inFileViewerRootedAtPath:(ocidContainerDirPath)
  set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
  if (boolDone as boolean) is false then
    #ファイルを開く
    set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
    tell application "Finder"
open file aliasSaveFilePath
    end tell
  end if
  ##処理終了
return "処理終了"
end open

##############################
# 小数点以下2桁処理
##############################

to doRound2Dec(argNumber)
  set strDecNo to ((round (argNumber * 100)) / 100) as text
return strDecNo
end doRound2Dec
##############################
# 基本的なHTMLの構造
##############################
to doMakeRootElement()
  #
  set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
ocidRootElement's addAttribute:(ocidAddNode)
  #
  set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
ocidAddElement's setStringValue:("ファイル一覧")
ocidHeadElement's addChild:(ocidAddElement)
  # http-equiv
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Style-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Script-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
ocidAddElement's setStringValue:("body { margin: 10px; background-color: #FFFFFF; } table { border-spacing: 0; caption-side: top; font-family: system-ui; } thead th { border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:first-of-type { border-start-start-radius: .5em } &:last-of-type { border-start-end-radius: .5em; border-inline-end-width: 1px } } tbody td { word-wrap: break-word;max-width: 360px;border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; &:last-of-type { border-inline-end-width: 1px } } tbody th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } tbody tr:nth-of-type(odd) { background: #F2F2F2; } .kind_string { font-size: 0.75em; } .date_string { font-size: 0.5em; } tfoot th { border: solid 1px #666666; padding: .5ch 1ch; &:first-of-type { border-end-start-radius: .5em } &:last-of-type { border-end-end-radius: .5em; border-inline-end-width: 1px } }")
ocidHeadElement's addChild:(ocidAddElement)
  #
ocidRootElement's addChild:(ocidHeadElement)
  #
return ocidRootElement
end doMakeRootElement

|

[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


|

フォルダーのリネーム(エラーにならないようにする)


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


###デスクトップに
tell application "Finder"
  set aliasDesktopDirPath to (path to desktop folder from user domain) as alias
end tell

#####フォルダ名『NewNameA』がなければ作る
tell application "Finder"
  ##有無を確認して
  set boolExists to exists of (folder "NewNameA" of folder aliasDesktopDirPath)
  if boolExists is false then
    ###なければ作る
make new folder at aliasDesktopDirPath with properties {name:"NewNameA"}
  end if
  set aliasNewFolderAPath to (folder "NewNameA" of folder aliasDesktopDirPath) as alias
end tell

#####フォルダ名『NewNameB』がなければ『NewNameA』を『NewNameB』に名前を変える
tell application "Finder"
  ##有無を確認して
  set boolExists to exists of (folder "NewNameB" of folder aliasDesktopDirPath)
  tell folder aliasNewFolderAPath
    if boolExists is false then
      ###なければフォルダ名を変更する
      set name to "NewNameB"
    end if
  end tell
end tell

|

フォルダ内に内包されているファイルのリスト

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

tell application "Finder"
  set aliasDownloadsFolderPath to path to downloads folder from user domain as alias
  
end tell

###全部リスト
tell application "Finder"
  set listAliasContents to every item in aliasDownloadsFolderPath
end tell



###ドキュメントだけのリスト
tell application "Finder"
  set listAliasContents to every document file in aliasDownloadsFolderPath
end tell

####エリアスだけのリスト
tell application "Finder"
  set listAliasContents to every alias file in aliasDownloadsFolderPath
end tell

####フォルダだけのリスト
tell application "Finder"
  set listAliasContents to every folder in aliasDownloadsFolderPath
end tell


###全部リスト
tell application "Finder"
  set listAliasContents to every item of contents of aliasDownloadsFolderPath
end tell



###ドキュメントだけのリスト
tell application "Finder"
  set listAliasContents to every document file of contents of aliasDownloadsFolderPath
end tell

####エリアスだけのリスト
tell application "Finder"
  set listAliasContents to every alias file of contents of aliasDownloadsFolderPath
end tell

####フォルダだけのリスト
tell application "Finder"
  set listAliasContents to every folder of contents of aliasDownloadsFolderPath
end tell

|

[サイズ]フォルダのサイズ

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

###
tell application "Finder"
    set aliasDesktopPath to path to desktop folder from user domain as alias
end tell
###
tell application "System Events"
    set numFolderSize to size of aliasDesktopPath as number
end tell
log (numFolderSize / 1024) / 1000 as integer




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


tell application "Finder"
    set aliasDesktopPath to path to desktop folder from user domain as alias
end tell

set strDesktopPath to POSIX path of aliasDesktopPath as text

set strCommandText to "/usr/bin/du -sh \"" & strDesktopPath & "\"" as text

set strResponse to do shell script strCommandText

set AppleScript's text item delimiters to "\t"
set listDelim to every text item of strResponse
set AppleScript's text item delimiters to ""
set strReturn to item 1 of listDelim as text

log strReturn

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



###
tell application "Finder"
    set aliasDesktopPath to path to desktop folder from user domain as alias
end tell
###
tell application "Finder"
    set recordInfo to info for aliasDesktopPath
    set numFolderSize to size of recordInfo
end tell
log (numFolderSize / 1024) / 1000 as integer

|

[moveItemAtURL]フォルダの移動

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

property refNSString : a reference to refMe's NSString

property refNSURL : a reference to refMe's NSURL

property refNSWorkspace : a reference to refMe's NSWorkspace


set objFileManager to refMe's NSFileManager's defaultManager()


###################################

#####入力フォルダ

###################################

###ダイアログのデフォルト

set ocidUserDesktopPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))

set aliasDefaultLocation to ocidUserDesktopPath as alias

tell application "Finder"

  ##  set aliasDefaultLocation to container of (path to me) as alias

end tell

###ダイアログテキスト

set strPromptText to "入力フォルダを選んでください"

try

  set listResponse to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as list

on error

  log "エラーしました"

  return

end try

###エリアス

set aliasDirPath to (item 1 of listResponse) as alias

###UNIXパス

set strDirPath to POSIX path of aliasDirPath as text

###String

set ocidDirPath to refNSString's stringWithString:strDirPath

###NSURL

set ocidDirPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidDirPath isDirectory:true

###フォルダ名

set ocidFileName to ocidDirPathURL's lastPathComponent()




#####移動先

set strFilePath to "~/Creative Cloud Files"

set ocidFilePath to refNSString's stringWithString:strFilePath

set ocidFilePath to ocidFilePath's stringByStandardizingPath

set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:true)

set ocidFileDistPathURL to ocidFilePathURL's URLByAppendingPathComponent:ocidFileName


###移動する

set listResults to objFileManager's moveItemAtURL:ocidDirPathURL toURL:ocidFileDistPathURL |error|:(reference)


###Finderで開く

set ocidShardWorkspace to refNSWorkspace's sharedWorkspace()

ocidShardWorkspace's openURL:ocidFilePathURL




|

[NSURLIsUserImmutableKey]フォルダをロックする

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application

property refNSString : a reference to refMe's NSString

property refNSURL : a reference to refMe's NSURL


property refNSDate : a reference to refMe's NSDate

property refNSCalendar : a reference to refMe's NSCalendar

property refNSTimeZone : a reference to refMe's NSTimeZone

property refNSDateFormatter : a reference to refMe's NSDateFormatter



set objFileManager to refMe's NSFileManager's defaultManager()





set ocidDesktopDirPathURL to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))


set aliasDefaultLocation to ocidDesktopDirPathURL as alias

set strDefaultName to doGetDateNo("yyyyMMdd") as text

set strPromptText to "名前を決めてください" as text


set aliasPath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»

####パステキスト

set strFilePathText to POSIX path of aliasPath as text

###String

set ocidFilePath to (refNSString's stringWithString:strFilePathText)

###絶対パスで

set ocidFilePathString to ocidFilePath's stringByStandardizingPath

###NSURL

set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)




############################

###フォルダを作る

set listBoolMakeDir to objFileManager's createDirectoryAtURL:(ocidFilePathURL) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)


set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue

set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue


#####ロックする

ocidFilePathURL's setResourceValue:ocidTrue forKey:(refMe's NSURLIsUserImmutableKey) |error|:(reference)


####ロックを解除する

ocidFilePathURL's setResourceValue:ocidFalse forKey:(refMe's NSURLIsUserImmutableKey) |error|:(reference)




###################################

#####日付

###################################

to doGetDateNo(strDateFormat)

  ####日付情報の取得

  set ocidDate to refNSDate's |date|()

  ###日付のフォーマットを定義

  set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()

  ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")

  ocidNSDateFormatter's setDateFormat:strDateFormat

  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate

  set strDateAndTime to ocidDateAndTime as text

  return strDateAndTime

end doGetDateNo



###################################

#####パーミッション 8進→10進

###################################

to doOct2Dem(argOctNo)

  set strOctalText to argOctNo as text

  set num3Line to first item of strOctalText as number

  set num2Line to 2nd item of strOctalText as number

  set num1Line to last item of strOctalText as number

  set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1)

  return numDecimal

end doOct2Dem


|

[Folder]フォルダを開く

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

# com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions



property refMe : a reference to current application

property refNSWorkspace : a reference to refMe's NSWorkspace


property refNSURL : a reference to refMe's NSURL


####ワークスペースを定義

set ocidShardWorkspace to refNSWorkspace's sharedWorkspace()

log ocidShardWorkspace

log className of ocidShardWorkspace as text


###パスを定義

set ocidDirPath to "/System/Library/CoreServices/Finder.app/Contents/Applications"

###NSURL

set ocidDirPathURL to refNSURL's fileURLWithPath:ocidDirPath

log ocidDirPathURL's absoluteString() as text

log className() of ocidDirPathURL as text

###Finderで開く

ocidShardWorkspace's openURL:ocidDirPathURL



|

[Folder]新しいフォルダを作る

#!/usr/bin/env osascript

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

#

#

#

#

#                       com.cocolog-nifty.quicktimer.icefloe

----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

##自分環境がos12なので2.8にしているだけです

use AppleScript version "2.8"

use framework "Foundation"

use scripting additions


property refMe : a reference to current application

property refNSString : a reference to refMe's NSString

property refNSURL : a reference to refMe's NSURL


property refNSDate : a reference to refMe's NSDate

property refNSCalendar : a reference to refMe's NSCalendar

property refNSTimeZone : a reference to refMe's NSTimeZone

property refNSDateFormatter : a reference to refMe's NSDateFormatter



set objFileManager to refMe's NSFileManager's defaultManager()





set ocidDesktopDirPathURL to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))


set aliasDefaultLocation to ocidDesktopDirPathURL as alias

set strDefaultName to doGetDateNo("yyyyMMdd") as text

set strPromptText to "名前を決めてください" as text


set aliasPath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»

####パステキスト

set strFilePathText to POSIX path of aliasPath as text

###String

set ocidFilePath to (refNSString's stringWithString:strFilePathText)

###絶対パスで

set ocidFilePathString to ocidFilePath's stringByStandardizingPath

###NSURL

set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidFilePathString isDirectory:true)


############################

#####属性を指定しておく

set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0

set numUID to user ID of (system info) as number

###所有者ID

ocidAttrDict's setValue:numUID forKey:(refMe's NSFileOwnerAccountID)

###グループID

ocidAttrDict's setValue:80 forKey:(refMe's NSFileGroupOwnerAccountID)

#####NSFileGroupOwnerAccountID

(* ゲストのGID

80-->admin

20-->staff

201-->_guest

99-->_unknown

-2-->nobody

*)

####パーミッション

set numPermissionDem to doOct2Dem(777) as integer

log numPermissionDem

ocidAttrDict's setValue:numPermissionDem forKey:(refMe's NSFilePosixPermissions)

###作るフォルダの属性

(*

###主要なモード NSFilePosixPermissions

777-->511

775-->509

770-->504

755-->493

750-->488

700-->448

555-->365

333-->219

*)

ocidAttrDict's setValue:(refMe's NSFileProtectionNone) forKey:(refMe's NSFileProtectionKey)

ocidAttrDict's setValue:0 forKey:(refMe's NSFileAppendOnly)


############################

###フォルダを作る

set listBoolMakeDir to objFileManager's createDirectoryAtURL:(ocidFilePathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)


############################

####ラベルを指定する

(*

0:ラベル無し

1:グレー

2:グリーン

3:パープル

4:ブルー

5:イエロー

6:レッド

7:オレンジ

*)

ocidFilePathURL's setResourceValue:7 forKey:(refMe's NSURLLabelNumberKey) |error|:(reference)

############################

####コメントの追加

tell application "Finder" to set comment of item aliasPath to strDefaultName






###################################

#####日付

###################################

to doGetDateNo(strDateFormat)

  ####日付情報の取得

  set ocidDate to refNSDate's |date|()

  ###日付のフォーマットを定義

  set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()

  ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")

  ocidNSDateFormatter's setDateFormat:strDateFormat

  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate

  set strDateAndTime to ocidDateAndTime as text

  return strDateAndTime

end doGetDateNo



###################################

#####パーミッション 8進→10進

###################################

to doOct2Dem(argOctNo)

  set strOctalText to argOctNo as text

  set num3Line to first item of strOctalText as number

  set num2Line to 2nd item of strOctalText as number

  set num1Line to last item of strOctalText as number

  set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1)

  return numDecimal

end doOct2Dem


|

[List]選択中フォルダ内のリスト

シンプルにパスが必要な『ダケ』ならFinderで
色々条件あるならcontentsOfDirectoryAtURL
NSOpenPanelはアプリケーションに書き出さないと利用できない



tell application "Finder"

-->ファインダで選択中の項目の戻り値は『リスト』
set listSelection to selection as list
-->リストに格納されている個数を数えて
set numCntListItem to (count of listSelection) as number
-->リストの項目が1つなら
if numCntListItem = 1 then
set strKind to kind of item 1 of listSelection as Unicode text
-->選択中の項目が『フォルダ』なら
if strKind is "フォルダ" then
set listContents to every item of (entire contents of (item 1 of listSelection)) as list
else
return "フォルダ以外を選択時には実行しない"
end if
else
return "複数選択時には実行しない"
end if

end tell



#!/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
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

set objFileManager to refMe's NSFileManager's defaultManager()


tell application "Finder"
-->ファインダで選択中の項目の戻り値は『リスト』
set listSelection to selection as list
end tell

-->リストに格納されている個数を数えて
set numCntListItem to (count of listSelection) as number
-->リストの項目が1つなら
if numCntListItem = 1 then
set strKind to kind of item 1 of listSelection as Unicode text
-->選択中の項目が『フォルダ』なら
if strKind is "フォルダ" then
set aliasFolderPath to item 1 of listSelection as alias
set strFolderPath to POSIX path of aliasFolderPath as text
set ocidFolderPath to refNSString's stringWithString:strFolderPath
set ocidFolderPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidFolderPath
set listContentsAndError to objFileManager's contentsOfDirectoryAtURL:ocidFolderPathURL includingPropertiesForKeys:(missing value) options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)

(*##NSURLResourceKey
フォルダ関連のキーは2つ
NSURLIsDirectoryKey
NSURLParentDirectoryURLKey
*)
(* ##NSDirectoryEnumerationOptions
フォルダ関連のオプションは3つ
NSDirectoryEnumerationSkipsSubdirectoryDescendants:0
NSDirectoryEnumerationSkipsPackageDescendants:1
NSDirectoryEnumerationSkipsHiddenFiles:2
*)

-->戻り値 MutableArray (*__NSArrayM*)
log item 1 of listContentsAndError as list
-->エラーの場合の戻り値エラーがなければmissing valueが戻り値
log item 2 of listContentsAndError

else
return "フォルダ以外を選択時には実行しない"
end if
else
return "複数選択時には実行しない"
end if



#!/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
property refNSString : a reference to refMe's NSString
set objFileManager to refMe's NSFileManager's defaultManager()


tell application "Finder"
-->ファインダで選択中の項目の戻り値は『リスト』
set listSelection to selection as list
end tell

-->リストに格納されている個数を数えて
set numCntListItem to (count of listSelection) as number
-->リストの項目が1つなら
if numCntListItem = 1 then
set strKind to kind of item 1 of listSelection as Unicode text
-->選択中の項目が『フォルダ』なら
if strKind is "フォルダ" then
set aliasFolderPath to item 1 of listSelection as alias
set strFolderPath to POSIX path of aliasFolderPath as text
set ocidFolderPath to refNSString's stringWithString:strFolderPath

set listContentsAndError to objFileManager's contentsOfDirectoryAtPath:ocidFolderPath |error|:(reference)
-->戻り値 MutableArray (*__NSArrayM*)
log item 1 of listContentsAndError as list
-->エラーの場合の戻り値エラーがなければmissing valueが戻り値
log item 2 of listContentsAndError

else
return "フォルダ以外を選択時には実行しない"
end if
else
return "複数選択時には実行しない"
end if

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom