20260514

[AppleScript]画像ファイルのリネーム(ベースファイル名を親フォルダ名に)OBJC版

[AppleScript]画像ファイルのリネーム(ベースファイル名を親フォルダ名に)

NOTE記事一覧ですnote.com
 

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

OC画像ファイルのリネーム解像度入り.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006
007画像ファイルを収集
008URLにして
009パス毎でソート
010
011フォルダ名(親フォルダ名)をベースファイル名にするパターン
012
013重複回避の連番
014親フォルダ名
015縦横pxサイズ
016解像度
017拡張子 でファイル名にしていきます
018
019OBJC記法
020NSimageでptサイズNSImageRepでpxサイズを取得して解像度を求める
021
022
023com.cocolog-nifty.quicktimer.icefloe *)
024----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
025use AppleScript version "2.8"
026use framework "Foundation"
027use framework "AppKit"
028use framework "UniformTypeIdentifiers"
029use scripting additions
030
031property refMe : a reference to current application
032
033########################
034# RUN=Wクリックの始まり
035#on run {listAliasFilePath}
036
037
038(*
039      ########################
040   #ダイアログ
041   *)
042on run
043   set strMsg to ("画像ファイルを 選択") as text
044   set strPrompt to ("画像ファイルを を選択してください" & return & "複数選択可") as text
045   set aliasDesktopDirPath to (path to desktop from user domain) as alias
046   set listUTI to {"public.image"} as list
047   set listUTI to {"public.item"} as list
048   try
049      tell application "SystemUIServer"
050         activate
051         set listAliasFilePath to (choose file strMsg with prompt strPrompt default location aliasDesktopDirPath of type listUTI with invisibles and multiple selections allowed without showing package contents) as list
052      end tell
053   on error strErrMes number numErrNo
054      log strErrMes & numErrNo
055      return false
056   end try
057   if listAliasFilePath is {} then
058      return false
059   end if
060   
061   
062   
063   
064   set boolDone to (open listAliasFilePath)
065   return boolDone
066end run
067
068########################
069# OPEN =ドロップの始まり
070on open listAliasFilePath
071   
072   #処理対象のUTIリスト=このUTIのファイルだけを次工程に回す
073   set ocidImageUTType to (refMe's UTType's typeWithIdentifier:("public.image"))
074   
075   #次工程に回すリスト
076   set ocidFilePathURLArrayM to refMe's NSMutableArray's alloc()'s init()
077   
078   #OPENに渡されたエイリアスのリストを順番に処理
079   repeat with itemAliasFilePath in listAliasFilePath
080      #エイリアス確定
081      set aliasItemFilePath to itemAliasFilePath as alias
082      #NSURLに
083      set strItemFilePath to (POSIX path of aliasItemFilePath) as text
084      set ocidItemFilePathStr to (refMe's NSString's stringWithString:(strItemFilePath))
085      set ocidItemFilePath to ocidItemFilePathStr's stringByStandardizingPath()
086      set ocidItemFilePath to ocidItemFilePath's stringByExpandingTildeInPath()
087      set ocidItemFilePathURL to (refMe's NSURL's fileURLWithPath:(ocidItemFilePath) isDirectory:(false))
088      #コンテンツのタイプを取得して
089      set listResponse to (ocidItemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
090      set ocidUTType to (second item of listResponse)
091      #含まれていれば次工程に回す
092      if (ocidUTType's conformsToType:(ocidImageUTType)) is true then
093         (ocidFilePathURLArrayM's addObject:(ocidItemFilePathURL))
094      end if
095      
096   end repeat
097   #パス基準でソート NSURLはSELFではソートできない
098   set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(yes) selector:("localizedStandardCompare:")
099   set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
100   set ocidSortedArray to ocidFilePathURLArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
101   #本処理のファイル名変更
102   set boolDone to doCngFileName(ocidSortedArray)
103   return true
104end open
105
106
107########################
108#
109to doCngFileName(ocidSortedArray)
110   #渡されたArrayのアイテム数
111   set numCntArray to ocidSortedArray's |count|()
112   #Arrayのアイテムの数だけリピート
113   repeat with iteNO from 0 to (numCntArray - 1) by 1
114      #カウンターのゼロサプレス
115      set strZeroPadd to ("000" & (iteNO + 1) & "") as text
116      set strItemNO to (text -3 through -1 of strZeroPadd) as text
117      #NAURLをArrayから取り出して
118      set ocidFilePathURL to (ocidSortedArray's objectAtIndex:(iteNO))
119      #親フォルダパス
120      set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
121      #親フォルダ名
122      set ocidDirName to ocidContainerDirPathURL's lastPathComponent()
123      #拡張子
124      set ocidExtensionName to ocidFilePathURL's pathExtension()
125      #ファイル名
126      set ocidFileName to ocidFilePathURL's lastPathComponent()
127      #ファイル名の文字をデコードNFD
128      set ocidFileName to ocidFileName's decomposedStringWithCanonicalMapping()
129      #元ファイルのベースファイル名
130      set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
131      #NSImageRepはptサイズ pxサイズ両方持っています
132      set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL))
133      set ocidSizePt to ocidImageRep's |size|()
134      set numWpt to ocidSizePt's width()
135      set numHpt to ocidSizePt's height()
136      set numWpx to ocidImageRep's pixelsWide()
137      set numHpx to ocidImageRep's pixelsHigh()
138      #画像解像度(小数点以下丸め)
139      set numPPI to ((numWpx / numWpt) * 72) as integer
140      ###########
141      #変更語のファイル名
142      set strAddSize to ("" & numWpx & "x" & numHpx & "@" & numPPI & "") as text
143      set strSetFileName to ("" & strItemNO & "-" & ocidDirName & "-" & strAddSize & "." & ocidExtensionName & "") as text
144      #変更後のファイルパス
145      set ocidDistFilePathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strSetFileName) isDirectory:(false))
146      ##########
147      #MOVEでリネーム
148      set appFileManager to refMe's NSFileManager's defaultManager()
149      set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error|:(reference))
150      
151   end repeat
152   
153   return (first item of listDone) as boolean
154   
155end doCngFileName
AppleScriptで生成しました