Folder

フォルダ内のファイルの情報をHTMLで出力する 少し修正

20241017112357_1652x3212

ダウンロード - makefilelist2html.zip




AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# 別名で保存から アプリケーションで保存でドロップレットになります
005# v2 一部のメディアのピクセルサイズの取得に対応
006# com.cocolog-nifty.quicktimer.icefloe
007----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use framework "UniformTypeIdentifiers"
012use framework "AVFoundation"
013use framework "CoreMedia"
014use scripting additions
015property refMe : a reference to current application
016
017
018###Wクリックで起動した場合
019on run
020  ###ダイアログを前面に出す
021  tell current application
022    set strName to name as text
023  end tell
024  ####スクリプトメニューから実行したら
025  if strName is "osascript" then
026    tell application "Finder" to activate
027  else
028    tell current application to activate
029  end if
030  ##デフォルト=デスクトップ
031  set appFileManager to refMe's NSFileManager's defaultManager()
032  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
033  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
034  set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
035  set strPromptText to "フォルダをえらんでください"
036  set strMesText to "フォルダをえらんでください"
037  try
038    set aliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias
039  on error
040    log "エラーしました"
041    return "エラーしました"
042  end try
043  open aliasDirPath
044end run
045
046
047on open aliasDirPath
048  ##パス
049  set aliasDirPath to aliasDirPath as alias
050  set strDirPath to (POSIX path of aliasDirPath) as text
051  set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
052  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
053  set ocidDirPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath)
054  set strDirPathURL to ocidDirPathURL's absoluteString() as text
055  ##フォルダ判定
056  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error| :(reference))
057  set boolIsDir to (item 2 of listBoole) as boolean
058  if boolIsDir is false then
059    return "フォルダ以外です処理を終了します"
060  end if
061  ##フォルダ名→保存先HTMLパス
062  set ocidDirName to ocidDirPathURL's lastPathComponent()
063  set ocidSaveFileName to refMe's NSMutableString's alloc()'s initWithCapacity:0
064  ocidSaveFileName's appendString:("_ファイルリスト")
065  ocidSaveFileName's appendString:(ocidDirName)
066  set ocidBaseFilePathURL to ocidDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)
067  set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("html")
068  
069  ##ファイルの各種プロパティを取得
070  set appFileManager to refMe's NSFileManager's defaultManager()
071  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
072  set ocidPropertieArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
073  ocidPropertieArray's addObject:(refMe's NSURLPathKey)
074  ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey)
075  ocidPropertieArray's addObject:(refMe's NSURLCreationDateKey)
076  ocidPropertieArray's addObject:(refMe's NSURLContentModificationDateKey)
077  ocidPropertieArray's addObject:(refMe's NSURLNameKey)
078  ocidPropertieArray's addObject:(refMe's NSURLContentTypeKey)
079  ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey)
080  ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey)
081  ########################################
082  ##コンテンツの収集 A 第一階層のみの場合
083  ########################################
084  (*
085  set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) |error| :(reference))
086  set ocidFilePathURLArray to item 1 of listResponse
087  #パスリストをファイル名でソート並び替え
088  set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:("localizedStandardCompare:")
089  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
090  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
091  *)
092  ########################################
093  ##コンテンツの収集 B 最下層までの場合
094  ########################################
095  set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference)
096  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
097  set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
098  ocidFilePathURLAllArray's addObjectsFromArray:(ocidEmuFileURLArray)
099  #
100  set ocidFilePathURLArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
101  repeat with itemFilePathURL in ocidFilePathURLAllArray
102    set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error| :(reference))
103    set boolIsRegularFileKey to item 2 of listResult
104    if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
105      ####リストにする
106      (ocidFilePathURLArray's addObject:(itemFilePathURL))
107    end if
108  end repeat
109  #パスリストをファイル名でソート並び替え absoluteString localizedStandardCompare
110  set ocidDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:(true) selector:"localizedStandardCompare:")
111  set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
112  set ocidSortedArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray)
113  
114  ##############################
115  # XML 生成開始
116  ##############################
117  #XML初期化
118  set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
119  ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
120  # DTD付与
121  set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
122  ocidDTD's setName:("html")
123  ocidXMLDoc's setDTD:(ocidDTD)
124  # XML主要部分を生成
125  set ocidRootElement to doMakeRootElement()
126  #ボディエレメント
127  set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
128  #ヘッダー
129  set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header")
130  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header")
131  ocidHeaderElement's addAttribute:(ocidAddNode)
132  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header")
133  ocidHeaderElement's addAttribute:(ocidAddNode)
134  ocidBodyElement's addChild:(ocidHeaderElement)
135  #アーティクル
136  set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article")
137  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article")
138  ocidArticleElement's addAttribute:(ocidAddNode)
139  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article")
140  ocidArticleElement's addAttribute:(ocidAddNode)
141  ocidBodyElement's addChild:(ocidArticleElement)
142  #フッター
143  set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer")
144  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer")
145  ocidFooterElement's addAttribute:(ocidAddNode)
146  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer")
147  ocidFooterElement's addAttribute:(ocidAddNode)
148  #リンク付与(不要なら削除可)
149  set ocidAElement to refMe's NSXMLElement's elementWithName:("a")
150  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
151  (ocidAElement's addAttribute:(ocidAddNode))
152  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
153  (ocidAElement's addAttribute:(ocidAddNode))
154  set strContents to ("AppleScriptで生成しました") as text
155  (ocidAElement's setStringValue:(strContents))
156  ocidFooterElement's addChild:(ocidAElement)
157  ocidBodyElement's addChild:(ocidFooterElement)
158  ##############################
159  # TABLE コンテンツ部分生成開始
160  ##############################
161  #テーブル部生成開始
162  set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
163  #【caption】
164  set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
165  ocidCaptionElement's setStringValue:("【ファイルリスト】: 行番号に※表示はCMYKデータ(PDFを除く)")
166  ocidTableElement's addChild:(ocidCaptionElement)
167  #【colgroup】
168  set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
169  #テーブルのタイトル部
170  set listColName to {"行番号", "ファイル名", "サイズ", "Pxサイズ", "種類", "作成日", "修正日"} as list
171  #タイトル部の数だけ繰り返し
172  repeat with itemColName in listColName
173    #【col】col生成
174    set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
175    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
176    (ocidAddElement's addAttribute:(ocidAddNode))
177    (ocidColgroupElement's addChild:(ocidAddElement))
178  end repeat
179  #テーブルエレメントに追加
180  ocidTableElement's addChild:(ocidColgroupElement)
181  #【thead】
182  set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
183  #TR
184  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
185  #タイトル部の数だけ繰り返し
186  repeat with itemColName in listColName
187    #ここはTDではなくてTHを利用
188    set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
189    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
190    (ocidAddElement's addAttribute:(ocidAddNode))
191    #値を入れる
192    (ocidAddElement's setStringValue:(itemColName))
193    #TH→TRにセット
194    (ocidTrElement's addChild:(ocidAddElement))
195  end repeat
196  #TRをTHEADにセット
197  ocidTheadElement's addChild:(ocidTrElement)
198  #THEADをテーブルにセット
199  ocidTableElement's addChild:(ocidTheadElement)
200  #【tbody】
201  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
202  #行番号
203  set numCntLineNo to 1 as integer
204  #合計ファイルサイズ用
205  set numAllFileSize to 0 as integer
206  #ファイルのパスの数だけ繰り返し
207  repeat with itemFilePathURL in ocidSortedArray
208    ##UTIを求めて
209    set listResourceValue to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference))
210    set ocidContentType to (item 2 of listResourceValue)
211    set strUTI to (ocidContentType's identifier) as text
212    #########
213    set strUTILIST to "public.png,public.jpeg,com.adobe.photoshop-image,public.tiff,com.apple.icns" as text
214    if strUTILIST contains strUTI then
215      log "CMYKチェック対象ファイル"
216      set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(itemFilePathURL))
217      set ocidImageRepArray to ocidReadImage's representations()
218      set ocidImageRep to ocidImageRepArray's firstObject()
219      #画像サイズと解像度
220      set numPxW to ocidImageRep's pixelsWide()
221      set numPxH to ocidImageRep's pixelsHigh()
222      set ocidPtSize to ocidReadImage's |size|()
223      set numPtW to ocidPtSize's width()
224      set numPtH to ocidPtSize's height()
225      #
226      #計算に必要な値
227      set ocidDecPt to (refMe's NSDecimalNumber's alloc()'s initWithString:("72"))
228      set ocidDecIn to (refMe's NSDecimalNumber's alloc()'s initWithString:("25.4"))
229      #
230      set ocidDecWpx to (refMe's NSDecimalNumber's alloc()'s initWithString:(numPxW as text))
231      set ocidDecHpx to (refMe's NSDecimalNumber's alloc()'s initWithString:(numPxH as text))
232      set ocidDecWpt to (refMe's NSDecimalNumber's alloc()'s initWithString:(numPtW as text))
233      set ocidDecHpt to (refMe's NSDecimalNumber's alloc()'s initWithString:(numPtH as text))
234      set ocidResolutioPxW to (ocidDecWpx's decimalNumberByDividingBy:(ocidDecWpt))
235      set ocidResolutinW to (ocidResolutioPxW's decimalNumberByMultiplyingBy:(ocidDecPt))
236      set ocidResolutioPxH to (ocidDecHpx's decimalNumberByDividingBy:(ocidDecHpt))
237      set ocidResolutinH to (ocidResolutioPxH's decimalNumberByMultiplyingBy:(ocidDecPt))
238      
239      #小数点以下2位で四捨五入
240      set appFormatter to refMe's NSNumberFormatter's alloc()'s init()
241      (appFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundUp))
242      (appFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
243      (appFormatter's setMaximumFractionDigits:(2))
244      #解像度は桁揃えしておく
245      set ocidResolutionW to (appFormatter's stringFromNumber:(ocidResolutinW))
246      set ocidResolutionH to (appFormatter's stringFromNumber:(ocidResolutinH))
247      #HTML表示用解像度
248      set strSetResValueA to ((numPxW as text) & " x " & (ocidDecHpx as text) & "") as text
249      set strSetResValueB to ("(" & ocidResolutionW & " x " & ocidResolutionH & " ppi)") as text
250      #######
251      set ocidColorSpace to ocidImageRep's colorSpace()
252      #シンプルにコンポーネント番号で判定する場合
253      # 1 = GS 3=RGB 4=CMYK
254      set numColorCompNo to ocidColorSpace's numberOfColorComponents as integer
255      #colorSpaceModelで判定したい場合
256      # set ocidColorModel to ocidColorSpace's colorSpaceModel()
257      # if ocidColorModel = (refMe's NSColorSpaceModelCMYK) then
258      log numColorCompNo
259      if numColorCompNo = 4 then
260        log "CMYYKデータです"
261        set ocidReadImage to (missing value)
262        set ocidImageRep to (missing value)
263        set boolCMYK to true as boolean
264      else
265        set boolCMYK to false as boolean
266      end if
267    else
268      set strSetResValueA to ("") as text
269      set strSetResValueB to ("") as text
270      set boolCMYK to false as boolean
271    end if
272    set strUTILIST to "public.movie,public.mpeg-4" as text
273    if strUTILIST contains strUTI then
274      set ocidReadAsset to (refMe's AVAsset's assetWithURL:(itemFilePathURL))
275      set ocidReadAssetTrackArray to ocidReadAsset's tracks()
276      set ocidTrack to (ocidReadAssetTrackArray's firstObject())
277      if ocidTrack = (missing value) then
278        set strSetResValueA to ("") as text
279        set strSetResValueB to ("") as text
280      else
281        set recordAssetTrackNaturalSize to ocidTrack's naturalSize()
282        set numTrackWidth to (width of recordAssetTrackNaturalSize) as integer
283        set numrackHeight to (height of recordAssetTrackNaturalSize) as integer
284        set strTrackWidth to (numTrackWidth) as text
285        set strTrackHeight to (numrackHeight) as text
286        set strSetResValueA to (strTrackWidth & " x " & strTrackHeight & "") as text
287        set strSetResValueB to ("") as text
288      end if
289    end if
290    
291    ##
292    #ファイルのリソースを取得
293    set listResponse to (itemFilePathURL's resourceValuesForKeys:(ocidPropertieArray) |error| :(reference))
294    set ocidValueDict to (item 1 of listResponse)
295    #TRの開始
296    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
297    #【行番号】をTHでセット
298    set strZeroSupp to ("00") as text
299    set strZeroSupp to ("00" & numCntLineNo) as text
300    set strLineNO to (text -3 through -1 of strZeroSupp) as text
301    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
302    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
303    (ocidThElement's addAttribute:(ocidAddNode))
304    if boolCMYK is true then
305      set strSetLineNO to (strLineNO & "*") as text
306    else if boolCMYK is false then
307      set strSetLineNO to (strLineNO) as text
308    end if
309    (ocidThElement's setStringValue:(strSetLineNO))
310    (ocidTrElement's addChild:(ocidThElement))
311    #【ファイル名】をTDでセット
312    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLNameKey))
313    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
314    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイル名"))
315    (ocidTdElement's addAttribute:(ocidAddNode))
316    ####
317    set itemFilePath to itemFilePathURL's |path|
318    set ocidRange to (itemFilePath's rangeOfString:(ocidDirPath))
319    set numLength to (|length| of ocidRange) + 1 as integer
320    set ocidRlativePath to (itemFilePath's substringFromIndex:(numLength))
321    
322    #↑のTDの内容=ファイル名にリンクを付与
323    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
324    set strHref to ("./" & ocidRlativePath) as text
325    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strHref))
326    (ocidAElement's addAttribute:(ocidAddNode))
327    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
328    (ocidAElement's addAttribute:(ocidAddNode))
329    set strHref to (ocidRlativePath) as text
330    (ocidAElement's setStringValue:(strHref))
331    #リンクをTDにセット
332    (ocidTdElement's addChild:(ocidAElement))
333    #TDをTRにセット
334    (ocidTrElement's addChild:(ocidTdElement))
335    #【ファイルサイズ】TD
336    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLFileSizeKey))
337    #ファイルサイズの合計に加算
338    set strValue to ocidValue as text
339    set numValue to strValue as integer
340    set numAllFileSize to (numAllFileSize + numValue)
341    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
342    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ファイルサイズ"))
343    (ocidTdElement's addAttribute:(ocidAddNode))
344    #単位による分岐
345    set numValue to ocidValue's intValue() as number
346    set numFileSize to numValue as integer
347    #
348    set numByteUnits to 1000 as integer
349    # バイト単位 10.5以前やWindowsターゲットの場合は1024に
350    
351    if (numFileSize) < numByteUnits then
352      log "b:バイト単位"
353      set strFileSize to doRound2Dec(numFileSize)
354      set strValue to (strFileSize & " b") as text
355    else if numFileSize < (numByteUnits * numByteUnits) then
356      log "kb:キロバイト単位"
357      set strFileSize to doRound2Dec(numFileSize / numByteUnits)
358      set strValue to (strFileSize & "kb") as text
359    else if numFileSize < (numByteUnits * numByteUnits * numByteUnits) then
360      log "mb:メガバイト単位"
361      set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits))
362      set strValue to (strFileSize & " mb") as text
363    else
364      log "gb:ギガバイト単位"
365      set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits * numByteUnits))
366      set strValue to (strFileSize & " gb") as text
367    end if
368    (ocidTdElement's setStringValue:(strValue))
369    (ocidTrElement's addChild:(ocidTdElement))
370    #【種類】TDでセット
371    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
372    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
373    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("解像度"))
374    (ocidTdElement's addAttribute:(ocidAddNode))
375    # set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
376    # (ocidTdElement's addAttribute:(ocidAddNode))
377    #
378    set ocidPElement to (refMe's NSXMLElement's elementWithName:("p"))
379    (ocidPElement's setStringValue:(strSetResValueA))
380    set ocidSmallElement to (refMe's NSXMLElement's elementWithName:("small"))
381    (ocidSmallElement's setStringValue:(strSetResValueB))
382    (ocidTdElement's addChild:(ocidPElement))
383    (ocidTdElement's addChild:(ocidSmallElement))
384    
385    (ocidTrElement's addChild:(ocidTdElement))
386    
387    
388    
389    #【種類】TDでセット
390    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentTypeKey))'s localizedDescription()
391    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
392    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("種類"))
393    (ocidTdElement's addAttribute:(ocidAddNode))
394    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("kind_string"))
395    (ocidTdElement's addAttribute:(ocidAddNode))
396    (ocidTdElement's setStringValue:(ocidValue))
397    (ocidTrElement's addChild:(ocidTdElement))
398    #【作成日】TDでセット
399    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLCreationDateKey))
400    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
401    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("作成"))
402    (ocidTdElement's addAttribute:(ocidAddNode))
403    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
404    (ocidTdElement's addAttribute:(ocidAddNode))
405    set strDate to (ocidValue as date) as text
406    (ocidTdElement's setStringValue:(strDate))
407    (ocidTrElement's addChild:(ocidTdElement))
408    #【修正日】TDでセット
409    set ocidValue to (ocidValueDict's valueForKey:(refMe's NSURLContentModificationDateKey))
410    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
411    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("修正日"))
412    (ocidTdElement's addAttribute:(ocidAddNode))
413    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("class") stringValue:("date_string"))
414    (ocidTdElement's addAttribute:(ocidAddNode))
415    set strDate to (ocidValue as date) as text
416    (ocidTdElement's setStringValue:(strDate))
417    (ocidTrElement's addChild:(ocidTdElement))
418    #出来上がったTRをTBODYにセット
419    (ocidTbodyElement's addChild:(ocidTrElement))
420    #項目番号のカウントアップ
421    set numCntLineNo to (numCntLineNo + 1) as integer
422  end repeat
423  #TBODYをテーブルにセット
424  ocidTableElement's addChild:(ocidTbodyElement)
425  #【tfoot】 TRで
426  set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot")
427  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
428  #項目数を取得して
429  set numCntCol to (count of listColName) as integer
430  #colspan指定して1行でセット
431  set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
432  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
433  (ocidThElement's addAttribute:(ocidAddNode))
434  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text))
435  (ocidThElement's addAttribute:(ocidAddNode))
436  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
437  (ocidThElement's addAttribute:(ocidAddNode))
438  #
439  set numByteUnits to 1000 as integer
440  # バイト単位 10.5以前やWindowsターゲットの場合は1024に
441  if numAllFileSize < numByteUnits then
442    log "b:バイト単位"
443    set strFileSize to doRound2Dec(numAllFileSize)
444    set strValue to "フォルダ合計:" & strFileSize & " b" as text
445    
446  else if numAllFileSize < (numByteUnits * numByteUnits) then
447    log "kb:キロバイト単位"
448    set strFileSize to doRound2Dec(numAllFileSize / numByteUnits)
449    set strValue to "フォルダ合計:" & strFileSize & " kb" as text
450    
451  else if numAllFileSize < (numByteUnits * numByteUnits * numByteUnits) then
452    log "mb:メガバイト単位"
453    set strFileSize to doRound2Dec(numAllFileSize / (numByteUnits * numByteUnits))
454    set strValue to "フォルダ合計:" & strFileSize & " mb" as text
455  else
456    log "gb:ギガバイト単位"
457    set strFileSize to doRound2Dec(numAllFileSize / (numByteUnits * numByteUnits * numByteUnits))
458    set strValue to "フォルダ合計:" & strFileSize & " gb" as text
459  end if
460  
461  
462  
463  if (numAllFileSize) < 102400 then
464    set numValue to (numAllFileSize / 1024) as integer
465    set strValue to "フォルダ合計:" & numValue & " kb" as text
466  else if (numAllFileSize) > (10240 * 100000) then
467    set numValue to (numAllFileSize / (10240 * 100000))
468    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
469    (ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
470    (ocidFormatter's setMinimumFractionDigits:(2))
471    (ocidFormatter's setMaximumFractionDigits:(2))
472    set strValue to (ocidFormatter's stringFromNumber:(numValue)) as text
473    set strValue to "フォルダ合計:" & strValue & " gb" as text
474  else
475    set numValue to (numAllFileSize / 1024000) as integer
476    set strValue to "フォルダ合計:" & numValue & " mb" as text
477  end if
478  ocidThElement's setStringValue:(strValue)
479  
480  #THをTRにセットして
481  ocidTrElement's addChild:(ocidThElement)
482  #TRをTFOOTにセット
483  ocidTfootElement's addChild:(ocidTrElement)
484  #TFOOTをテーブルにセット
485  ocidTableElement's addChild:(ocidTfootElement)
486  # 出来上がったテーブルをArticleエレメントにセット
487  ocidArticleElement's addChild:(ocidTableElement)
488  #
489  ocidRootElement's addChild:(ocidBodyElement)
490  ##############################
491  # TABLE
492  ##############################
493  #ROOTエレメントをXMLにセット
494  ocidXMLDoc's setRootElement:(ocidRootElement)
495  #読み取りやすい表示
496  set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
497  #保存
498  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)
499  set ocidSaveFilePath to ocidSaveFilePathURL's |path|
500  set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
501  set ocidContainerDirPath to ocidContainerDirPathURL's |path|
502  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
503  set boolDone to appSharedWorkspace's selectFile:(ocidSaveFilePath) inFileViewerRootedAtPath:(ocidContainerDirPath)
504  set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
505  if (boolDone as boolean) is false then
506    #ファイルを開く
507    set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
508    tell application "Finder"
509      open file aliasSaveFilePath
510    end tell
511  end if
512  ##処理終了
513  return "処理終了"
514end open
515
516##############################
517# 小数点以下2桁処理
518##############################
519
520to doRound2Dec(argNumber)
521  set strDecNo to ((round (argNumber * 100)) / 100) as text
522  return strDecNo
523end doRound2Dec
524##############################
525# 基本的なHTMLの構造
526##############################
527to doMakeRootElement()
528  #
529  set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
530  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
531  ocidRootElement's addAttribute:(ocidAddNode)
532  #
533  set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
534  #
535  set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
536  ocidAddElement's setStringValue:("ファイル一覧")
537  ocidHeadElement's addChild:(ocidAddElement)
538  # http-equiv
539  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
540  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type")
541  ocidAddElement's addAttribute:(ocidAddNode)
542  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8")
543  ocidAddElement's addAttribute:(ocidAddNode)
544  ocidHeadElement's addChild:(ocidAddElement)
545  #
546  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
547  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Style-Type")
548  ocidAddElement's addAttribute:(ocidAddNode)
549  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
550  ocidAddElement's addAttribute:(ocidAddNode)
551  ocidHeadElement's addChild:(ocidAddElement)
552  #
553  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
554  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Script-Type")
555  ocidAddElement's addAttribute:(ocidAddNode)
556  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
557  ocidAddElement's addAttribute:(ocidAddNode)
558  ocidHeadElement's addChild:(ocidAddElement)
559  #
560  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
561  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport")
562  ocidAddElement's addAttribute:(ocidAddNode)
563  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720")
564  ocidAddElement's addAttribute:(ocidAddNode)
565  ocidHeadElement's addChild:(ocidAddElement)
566  #
567  set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
568  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 p { margin-block-end: 2px; margin-block-start: 2px;} 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 } }")
569  ocidHeadElement's addChild:(ocidAddElement)
570  #
571  ocidRootElement's addChild:(ocidHeadElement)
572  #
573  return ocidRootElement
574end doMakeRootElement
AppleScriptで生成しました

|

フォルダ内のファイルを比較 差異があるファイルにはラベルを付与


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# ドロップレット用で作っています
005# 比較したい2つのフォルダをドロップするのが前提
006# 対象のファイルに 差分はオレンジ 相違はグレーのラベルを付与します
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014property refMe : a reference to current application
015
016####################################
017#【A】ダブルクリックかスクリプトエディタから実行
018on run
019  ##デスクトップ
020  set appFileManager to refMe's NSFileManager's defaultManager()
021  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
022  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
023  set aliasDesktopDirPath to ocidDesktopDirPathURL's absoluteURL() as alias
024  ###ダイアログを前面に出す
025  tell current application
026    set strName to name as text
027  end tell
028  if strName is "osascript" then
029    tell application "Finder" to activate
030  else
031    tell current application to activate
032  end if
033  set strMesText to ("フォルダを『2つ』選んでください") as text
034  set strPromptText to ("フォルダを『2つ』選んでください") as text
035  try
036    set listAliasDirPath to (choose folder strMesText with prompt strPromptText default location aliasDesktopDirPath with multiple selections allowed, invisibles and showing package contents) as list
037  on error
038    display alert "エラーが発生しました" message "エラーが発生しましたログを確認ください"
039    return "エラーしました"
040  end try
041  #比較用なので2つ以上ならNG
042  set numCntPath to (count of listAliasDirPath) as integer
043  #パスが2つなら
044  if numCntPath = 2 then
045    #サブルーチンに渡す
046    set boolDone to doAction(listAliasDirPath)
047  else
048    set boolDone to false as boolean
049  end if
050  #サブルーチンの戻り値でエラーチェック
051  if boolDone is false then
052    display alert "エラーが発生しました" message "エラーが発生しましたログを確認ください"
053  end if
054end run
055
056####################################
057#【B】ドロップで起動した場合
058on open dropAliasPath
059  set numCntPath to (count of dropAliasPath) as integer
060  #パスが2つなら
061  if numCntPath = 2 then
062    #サブルーチンに渡す
063    set boolDone to doAction(dropAliasPath)
064  else
065    set boolDone to false as boolean
066  end if
067  
068end open
069
070####################################
071#【C】RUNから渡されるサブ
072to doAction(argListPath)
073  #相違のあったファイルのパスの戻り値用テキスト
074  set ocidOutPutString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
075  #ALIAS
076  set aliasPath01 to (item 1 of argListPath) as alias
077  set aliasPath02 to (item 2 of argListPath) as alias
078  #PATH
079  set strPath01 to (POSIX path of aliasPath01) as text
080  set strPath02 to (POSIX path of aliasPath02) as text
081  ################
082  #チェック1 Only in
083  set strCommandText to ("/bin/zsh -c '/usr/bin/diff -rq \"" & strPath01 & "\" \"" & strPath02 & "\"' | grep \"Only in\" | grep -v \"DS_Store\"") as text
084  log strCommandText
085  set strResponse to (do shell script strCommandText) as text
086  #出力用のテキストにパスを追加
087  (ocidOutPutString's appendString:(strResponse))
088  (ocidOutPutString's appendString:("\r"))
089  #改行でリストに
090  set strDelim to AppleScript's text item delimiters
091  set AppleScript's text item delimiters to "\r"
092  set listLineText to every text item of strResponse
093  set AppleScript's text item delimiters to strDelim
094  repeat with itemLineText in listLineText
095    #Only inを取る
096    set strDelim to AppleScript's text item delimiters
097    set AppleScript's text item delimiters to "Only in "
098    set listOnlyPath to every text item of itemLineText
099    set AppleScript's text item delimiters to strDelim
100    set strTmpPath to (item 2 of listOnlyPath) as text
101    #コロンをパスに
102    set strDelim to AppleScript's text item delimiters
103    set AppleScript's text item delimiters to ": "
104    set listDiffPath to every text item of strTmpPath
105    set AppleScript's text item delimiters to strDelim
106    #パスにする
107    set strDiffFilePath to ((item 1 of listDiffPath) & "/" & (item 2 of listDiffPath)) as text
108    #ラベルを指定するサブへ渡す
109    log doLabelIndexOnlyIn(strDiffFilePath)
110  end repeat
111  ################
112  #チェック2 differ
113  set strCommandText to ("/bin/zsh -c '/usr/bin/diff -rq \"" & strPath01 & "\" \"" & strPath02 & "\"' | grep \"differ\" | grep -v \"DS_Store\"") as text
114  log strCommandText
115  set strResponse to (do shell script strCommandText) as text
116  #出力用のテキストにパスを追加
117  (ocidOutPutString's appendString:(strResponse))
118  (ocidOutPutString's appendString:("\r"))
119  #改行でリストに
120  set strDelim to AppleScript's text item delimiters
121  set AppleScript's text item delimiters to "\r"
122  set listLineText to every text item of strResponse
123  set AppleScript's text item delimiters to strDelim
124  repeat with itemLineText in listLineText
125    #Filesを取る
126    set strDelim to AppleScript's text item delimiters
127    set AppleScript's text item delimiters to "Files "
128    set listFilesPath to every text item of itemLineText
129    set AppleScript's text item delimiters to strDelim
130    set strTmpFPath to (item 2 of listFilesPath) as text
131    #differを取る
132    set strDelim to AppleScript's text item delimiters
133    set AppleScript's text item delimiters to " differ"
134    set listDiffer to every text item of strTmpFPath
135    set AppleScript's text item delimiters to strDelim
136    set strTmpDPath to (item 1 of listDiffer) as text
137    #andを区切りにしてリストにする
138    set strDelim to AppleScript's text item delimiters
139    set AppleScript's text item delimiters to " and "
140    set listAndPath to every text item of strTmpDPath
141    set AppleScript's text item delimiters to strDelim
142    log doLabelIndexDiffer(item 1 of listAndPath)
143    log doLabelIndexDiffer(item 2 of listAndPath)
144  end repeat
145  ################
146  #ダイアログに渡す
147  log doOpenDialogue(ocidOutPutString)
148  return true
149end doAction
150
151#####################
152#戻り値用ダイアログ
153to doOpenDialogue(argText)
154  tell current application
155    set strName to name as text
156  end tell
157  if strName is "osascript" then
158    tell application "Finder"
159      activate
160    end tell
161  else
162    tell current application
163      activate
164    end tell
165  end if
166  try
167    set aliasIconPath to (POSIX file "/System/Applications/Utilities/Terminal.app/Contents/Resources/Terminal.icns") as alias
168    set strAnsertText to (argText) as text
169    set strMes to "コマンド戻り値です"
170    set recordResult to (display dialog strMes with title "戻り値です" default answer strAnsertText buttons {"クリップボードにコピー", "終了", "ファイルにする"} default button "終了" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
171  on error
172    return "エラーしました"
173  end try
174  if (gave up of recordResult) is true then
175    return "時間切れです"
176  end if
177  ###############
178  #自分自身を再実行
179  if button returned of recordResult is "再実行" then
180    tell application "Finder"
181      set aliasPathToMe to (path to me) as alias
182    end tell
183    run script aliasPathToMe with parameters "再実行"
184  end if
185  ###############
186  #保存する
187  if button returned of recordResult is "ファイルにする" then
188    #保存先はtemp
189    set appFileManager to refMe's NSFileManager's defaultManager()
190    set ocidTempDirURL to appFileManager's temporaryDirectory()
191    set ocidUUID to refMe's NSUUID's alloc()'s init()
192    set ocidUUIDString to ocidUUID's UUIDString
193    set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
194    #フォルダを作っておいて
195    set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
196    ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
197    set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
198    if (item 1 of listDone) is true then
199      log "正常処理"
200    else if (item 2 of listDone) ≠ (missing value) then
201      set strErrorNO to (item 2 of listDone)'s code() as text
202      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
203      refMe's NSLog("■:" & strErrorNO & strErrorMes)
204      return "エラーしました" & strErrorNO & strErrorMes
205    end if
206    #保存先ファイルパス
207    set ocidBaseFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(false)
208    set ocidSaveFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("txt")
209    #保存
210    set listDone to argText's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
211    if (item 1 of listDone) is true then
212      log "正常処理"
213    else if (item 2 of listDone) ≠ (missing value) then
214      set strErrorNO to (item 2 of listDone)'s code() as text
215      set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
216      refMe's NSLog("■:" & strErrorNO & strErrorMes)
217      return "エラーしました" & strErrorNO & strErrorMes
218    end if
219    #開く
220    set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
221    set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL)
222    
223  end if
224  
225  ########
226  #値のコピー
227  if button returned of recordResult is "クリップボードにコピー" then
228    try
229      set strText to text returned of recordResult as text
230      ####ペーストボード宣言
231      set appPasteboard to refMe's NSPasteboard's generalPasteboard()
232      set ocidText to (refMe's NSString's stringWithString:(strText))
233      appPasteboard's clearContents()
234      appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
235    on error
236      tell application "Finder"
237        set the clipboard to strText as text
238      end tell
239    end try
240  end if
241  
242end doOpenDialogue
243
244
245
246##############################
247#ラベル グレーを付与する
248to doLabelIndexDiffer(argStrFilePath)
249  set strFilePath to argStrFilePath as text
250  set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
251  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
252  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
253  # Only inはオレンジ
254  set ocidLabelNo to refMe's NSNumber's numberWithInteger:(1)
255  set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference))
256  if (item 1 of listDone) is true then
257    # log "正常処理"
258  else if (item 2 of listDone) ≠ (missing value) then
259    set strErrorNO to (item 2 of listDone)'s code() as text
260    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
261    refMe's NSLog("■:" & strErrorNO & strErrorMes)
262    log "エラーしました" & strErrorNO & strErrorMes
263    return false
264  end if
265  return true
266end doLabelIndexDiffer
267
268##############################
269#ラベル オレンジを付与する
270to doLabelIndexOnlyIn(argStrFilePath)
271  set strFilePath to argStrFilePath as text
272  set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
273  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
274  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
275  # Only inはオレンジ
276  set ocidLabelNo to refMe's NSNumber's numberWithInteger:(7)
277  set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference))
278  if (item 1 of listDone) is true then
279    # log "正常処理"
280  else if (item 2 of listDone) ≠ (missing value) then
281    set strErrorNO to (item 2 of listDone)'s code() as text
282    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
283    refMe's NSLog("■:" & strErrorNO & strErrorMes)
284    log "エラーしました" & strErrorNO & strErrorMes
285    return false
286  end if
287  return true
288end doLabelIndexOnlyIn
AppleScriptで生成しました

|

ファイル名でフォルダを作って、その中に入れる(WEBLOCファイル対応)


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004#-->ドロップレットアプリケーションに書き出すとフォルダでも出来ます
005#v3 重複チェックを入れた
006#v4 WEBLOC等でOpenしてしまうので
007#OPENを利用しないドロップレットに変更した
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009use AppleScript version "2.8"
010use framework "Foundation"
011use framework "AppKit"
012use scripting additions
013
014property refMe : a reference to current application
015
016
017####################################
018#ダブルクリックかスクリプトエディタから実行
019on run
020  ##デスクトップ
021  set appFileManager to refMe's NSFileManager's defaultManager()
022  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
023  set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
024  set aliasDesktopDirPath to ocidDesktopDirPathURL's absoluteURL() as alias
025  ###ダイアログを前面に出す
026  tell current application
027    set strName to name as text
028  end tell
029  if strName is "osascript" then
030    tell application "Finder" to activate
031  else
032    tell current application to activate
033  end if
034  set strMes to ("ファイルを選んでください") as text
035  set strPrompt to ("ファイルを選んでください") as text
036  set listUTI to {"public.item"} as list
037  set listAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with multiple selections allowed and showing package contents without invisibles) as list
038  #サブルーチンに渡す
039  set boolDone to doAction(listAliasFilePath)
040  #サブルーチンの戻り値でエラーチェック
041  if boolDone is false then
042    display alert "エラーが発生しました" message "エラーが発生しましたログを確認ください"
043  end if
044end run
045
046####################################
047#ドロップレットでドロップした場合
048on open listAliasPath
049  #サブルーチンに渡す
050  set boolDone to doAction(listAliasPath)
051  #サブルーチンの戻り値でエラーチェック
052  if boolDone is false then
053    display alert "エラーが発生しました" message "エラーが発生しましたログを確認ください"
054  end if
055end open
056
057
058
059on doAction(argListAliasFilePath)
060  ##ファイルマネージャ初期化
061  set appFileManager to refMe's NSFileManager's defaultManager()
062  ##繰り返しのはじまり
063  repeat with itemAliasFilePath in argListAliasFilePath
064    set aliasFilePath to itemAliasFilePath as alias
065    set strFilePath to (POSIX path of aliasFilePath) as text
066    set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
067    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
068    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
069    ##拡張子
070    set ocidExtensionName to ocidFilePathURL's pathExtension()
071    ##フォルダを作るディレクトリ
072    set ocidConteinerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
073    ##ファイル名(拡張子あり)
074    set ocidFileName to ocidFilePathURL's lastPathComponent()
075    ##拡張子をとって
076    set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
077    #カンマを置換する→これがフォルダ名
078    #拡張子が2重に指定してあるケース対応でカンマをアンダースコアに置換する
079    set ocidDirName to (ocidBaseFileName's stringByReplacingOccurrencesOfString:(".") withString:("_"))
080    #作成するフォルダパス
081    set ocidMakeDirPathURL to (ocidConteinerDirPathURL's URLByAppendingPathComponent:(ocidDirName) isDirectory:(true))
082    ##フォルダパス+ファイル名で移動先
083    set ocidMoveFilePathURL to (ocidMakeDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false))
084    ##作ろうとしているディレクトリがすでに無いか?確認
085    set ocidMakeDirPath to ocidMakeDirPathURL's |path|()
086    set boolDirExists to (appFileManager's fileExistsAtPath:(ocidMakeDirPath) isDirectory:(true))
087    if boolDirExists = true then
088      log "フォルダがすでにある"
089    else if boolDirExists = false then
090      log "フォルダを作る"
091      set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
092      # 777-->511 755-->493 700-->448 766-->502
093      (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
094      set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidMakeDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference))
095    end if
096    ##重複チェック
097    set ocidDistFilePath to doChkExists(ocidMoveFilePathURL)
098    if ocidDistFilePath is false then
099      display alert "エラーが発生しました" message "重複チェックでエラーになりました"
100      return "重複チェックでエラーになりました"
101    end if
102    set ocidDistFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDistFilePath) isDirectory:false)
103    if ocidMoveFilePathURL = ocidMoveFilePathURL then
104      log "置換します"
105      #今あるファイルをゴミ箱に
106      set listResult to (appFileManager's trashItemAtURL:(ocidDistFilePathURL) resultingItemURL:(ocidDistFilePathURL) |error| :(reference))
107      #移動
108      set ListDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error| :(reference))
109    else
110      #移動
111      set ListDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error| :(reference))
112    end if
113    set boolDone to (item 1 of ListDone) as boolean
114    if boolDone is false then
115      log "移動に失敗しました"
116      return false
117    end if
118  end repeat
119  return true
120end doAction
121
122
123
124
125####################################
126#上書きチェック
127# ocid file path = NSPathStore を返します
128####################################
129to doChkExists(argFilePath)
130  log (className() of argFilePath) as text
131  if (class of argFilePath) is text then
132    log "テキストファイルパス"
133    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
134    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
135  else if (class of argFilePath) is alias then
136    log "エリアスファイルパス"
137    set strArgFilePath to (POSIX path of argFilePath) as text
138    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
139    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
140  else if (class of argFilePath) is «class furl» then
141    log "エリアスfurlファイルパス"
142    set aliasFilePath to argFilePath as alias
143    set strArgFilePath to (POSIX path of argFilePath) as text
144    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
145    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
146  else if (className() of argFilePath as text) contains "NSCFString" then
147    log "NSStringファイルパス"
148    set ocidArgFilePath to argFilePath's stringByStandardizingPath()
149  else if (className() of argFilePath as text) contains "NSPathStore" then
150    log "NSPathStore2ファイルパス"
151    set ocidArgFilePath to argFilePath
152  else if (className() of argFilePath as text) contains "NSURL" then
153    log "NSURLファイルパス"
154    set ocidArgFilePath to argFilePath's |path|
155  end if
156  ####
157  set appFileManager to refMe's NSFileManager's defaultManager()
158  set boolExists to appFileManager's fileExistsAtPath:(ocidArgFilePath) isDirectory:(false)
159  #
160  if boolExists = true then
161    ##ダイアログを前面に
162    set strName to (name of current application) as text
163    if strName is "osascript" then
164      tell application "Finder" to activate
165    else
166      tell current application to activate
167    end if
168    set strMes to "同名のファイルがすでにあります\n上書きします?" as text
169    try
170      set objResponse to (display alert strMes buttons {"上書きする", "処理を中止する", "ファイル名を変更"} default button "上書きする" cancel button "処理を中止する" as informational giving up after 20)
171    on error
172      log "処理を中止しました"
173      return false
174    end try
175    if true is equal to (gave up of objResponse) then
176      log "時間切れですやりなおしてください"
177      return false
178    end if
179    if "上書きする" is equal to (button returned of objResponse) then
180      log "上書き保存します"
181      set ocidReturnFilePath to ocidArgFilePath
182    else if "ファイル名を変更" is equal to (button returned of objResponse) then
183      log "ファイル名を変更"
184      set ocidContainerDirFilePath to ocidArgFilePath's stringByDeletingLastPathComponent()
185      set strFileName to ocidArgFilePath's lastPathComponent() as text
186      set aliasContainerDirPath to (POSIX file (ocidContainerDirFilePath as text)) as alias
187      ##
188      set strPromptText to "名前を決めてください" as text
189      set strMesText to "名前を決めてください" as text
190      ###ファイル名 ダイアログ
191      set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strFileName with prompt strPromptText) as «class furl»
192      set strFilePath to (POSIX path of aliasFilePath) as text
193      set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
194      set ocidReturnFilePath to ocidFilePathStr's stringByStandardizingPath()
195    else if "処理を中止する" is equal to (button returned of objResponse) then
196      return false
197    else
198      return false
199    end if
200  else if boolExists = false then
201    log "そのままファイル生成"
202    set ocidReturnFilePath to ocidArgFilePath
203  end if
204  return ocidReturnFilePath
205  
206end doChkExists
AppleScriptで生成しました

|

フォルダ名で検索 ドロップレット


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002
003(*
004ドロップしたフォルダの名称で
005検索WINDOWを開き
006同名のフォルダがある場合はラベルを塗る
007何に使うのか?全くわからない…笑
008*)
009###Wクリックで開いたら
010on run
011  tell application "Finder"
012    set aliasDefaultLocation to (path to desktop folder from user domain) as alias
013  end tell
014  set strMes to ("フォルダを選んでください") as text
015  set strPrompt to ("フォルダを選んでください") as text
016  set listAliasFolderPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with multiple selections allowed without invisibles and showing package contents) as list
017  open (listAliasFolderPath)
018end run
019
020###アプリケーションに保存してドロップされたら
021on open listAliasFolderPath
022  repeat with itemAliasDirPath in listAliasFolderPath
023    ##パス
024    set aliasDirPath to itemAliasDirPath as alias
025    set recordFileInfo to info for aliasDirPath
026    ##フォルダの場合だけ処理する
027    if (folder of recordFileInfo) is true then
028      log "フォルダの処理"
029      #フォルダの名前を
030      set strDirName to (name of recordFileInfo) as text
031      #クリップボードに格納して
032      tell application "Finder"
033        tell folder aliasDirPath
034          set strDirName to name as string
035          set the clipboard to strDirName as text
036        end tell
037      end tell
038      #検索フォームにペースト
039      tell application "Finder"
040        make new Finder window
041        activate
042        tell front Finder window
043          tell application "System Events" to keystroke "f" using {command down}
044          delay 1
045          tell application "System Events" to keystroke "v" using {command down}
046          delay 1
047          tell application "System Events" to keystroke return
048        end tell
049        delay 2
050        tell front Finder window
051          ##同名のものがあればラベル黄色
052          ##なければラベルを無色にする
053          set numCntItem to (count of every item) as integer
054          if numCntItem ≤ 1 then
055            set label index of aliasDirPath to 0
056          else
057            set label index of aliasDirPath to 3
058          end if
059          close
060        end tell
061      end tell
062    else
063      log "ファイルは処理しない"
064    end if
065  end repeat
066end open
067
AppleScriptで生成しました

|

フォルダのロック解除


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012
013#############################
014###ダイアログを前面に出す
015set strName to (name of current application) as text
016if strName is "osascript" then
017  tell application "Finder" to activate
018else
019  tell current application to activate
020end if
021#デフォルトロケーション
022set appFileManager to refMe's NSFileManager's defaultManager()
023set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
025set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
026#ダイアログ フォルダ選択
027set strMes to "フォルダを選んでください" as text
028set strPrompt to "フォルダを選択してください" as text
029try
030  set listAliasFolderPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with multiple selections allowed without invisibles and showing package contents)
031on error
032  log "エラーしました"
033  return "エラーしました"
034end try
035
036repeat with objAliasFolderPath in listAliasFolderPath
037  #パス
038  set aliasFolderPath to objAliasFolderPath as alias
039  set strDirPath to (POSIX path of aliasFolderPath) as text
040  set ocidDirPathStr to (refMe's NSString's stringWithString:(strDirPath))
041  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
042  set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:false)
043  ###処理実行 ロック解除
044  set listDone to doDirUnLock(ocidDirPathURL)
045  ###処理実行 ロック
046  # set listDone to doDirLock(ocidDirPathURL)
047  #結果
048  if (item 2 of listDone) ≠ (missing value) then
049    set strDirPath to (ocidDirPathURL's |path|()) as text
050    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
051    #
052    set strMes to ("【エラー】\n" & strDirPath & "\n" & strErrorMes) as text
053    try
054      set recordResponse to display alert strMes buttons {"続行", "中止"} default button "続行" cancel button "中止" as informational giving up after 10
055    on error
056      log "キャンセルしました"
057      exit repeat
058    end try
059    if true is equal to (gave up of recordResponse) then
060      log "時間切れですやりなおしてください"
061      exit repeat
062      error number -128
063    end if
064  else if (item 1 of listDone) is true then
065    log "正常終了 : " & (ocidDirPathURL's |path|()) as text
066  end if
067  
068end repeat
069
070
071
072############################
073#フォルダのロックを解除
074############################
075
076to doDirUnLock(argDirURL)
077  #URLをパスにして
078  set ocidDirPath to argDirURL's |path|()
079  #ロック設定 false
080  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
081  ocidAttrDict's setValue:(false) forKey:(refMe's NSFileImmutable)
082  #設定する
083  set appFileManager to refMe's NSFileManager's defaultManager()
084  set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error| :(reference)
085  #結果を戻す
086  return listDone
087end doDirUnLock
088
089
090############################
091#フォルダのロック
092############################
093
094to doDirLock(argDirURL)
095  #URLをパスにして
096  set ocidDirPath to argDirURL's |path|()
097  #ロック設定 true
098  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
099  ocidAttrDict's setValue:(true) forKey:(refMe's NSFileImmutable)
100  #設定する
101  set appFileManager to refMe's NSFileManager's defaultManager()
102  set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error| :(reference)
103  #結果を戻す
104  return listDone
105end doDirLock
AppleScriptで生成しました

|

フォルダのロック


あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012
013#############################
014###ダイアログを前面に出す
015set strName to (name of current application) as text
016if strName is "osascript" then
017  tell application "Finder" to activate
018else
019  tell current application to activate
020end if
021#デフォルトロケーション
022set appFileManager to refMe's NSFileManager's defaultManager()
023set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
025set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
026#ダイアログ フォルダ選択
027set strMes to "フォルダを選んでください" as text
028set strPrompt to "フォルダを選択してください" as text
029try
030  set listAliasFolderPath to (choose folder strMes with prompt strPrompt default location aliasDefaultLocation with multiple selections allowed without invisibles and showing package contents)
031on error
032  log "エラーしました"
033  return "エラーしました"
034end try
035
036repeat with objAliasFolderPath in listAliasFolderPath
037  #パス
038  set aliasFolderPath to objAliasFolderPath as alias
039  set strDirPath to (POSIX path of aliasFolderPath) as text
040  set ocidDirPathStr to (refMe's NSString's stringWithString:(strDirPath))
041  set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
042  set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:false)
043  ###処理実行 ロック解除
044  #   set listDone to doDirUnLock(ocidDirPathURL)
045  ###処理実行 ロック
046  set listDone to doDirLock(ocidDirPathURL)
047  #結果
048  if (item 2 of listDone) ≠ (missing value) then
049    set strDirPath to (ocidDirPathURL's |path|()) as text
050    set strErrorMes to (item 2 of listDone)'s localizedDescription() as text
051    #
052    set strMes to ("【エラー】\n" & strDirPath & "\n" & strErrorMes) as text
053    try
054      set recordResponse to display alert strMes buttons {"続行", "中止"} default button "続行" cancel button "中止" as informational giving up after 10
055    on error
056      log "キャンセルしました"
057      exit repeat
058    end try
059    if true is equal to (gave up of recordResponse) then
060      log "時間切れですやりなおしてください"
061      exit repeat
062      error number -128
063    end if
064  else if (item 1 of listDone) is true then
065    log "正常終了 : " & (ocidDirPathURL's |path|()) as text
066  end if
067  
068end repeat
069
070
071
072############################
073#フォルダのロックを解除
074############################
075
076to doDirUnLock(argDirURL)
077  #URLをパスにして
078  set ocidDirPath to argDirURL's |path|()
079  #ロック設定 false
080  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
081  ocidAttrDict's setValue:(false) forKey:(refMe's NSFileImmutable)
082  #設定する
083  set appFileManager to refMe's NSFileManager's defaultManager()
084  set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error| :(reference)
085  #結果を戻す
086  return listDone
087end doDirUnLock
088
089
090############################
091#フォルダのロック
092############################
093
094to doDirLock(argDirURL)
095  #URLをパスにして
096  set ocidDirPath to argDirURL's |path|()
097  #ロック設定 true
098  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
099  ocidAttrDict's setValue:(true) forKey:(refMe's NSFileImmutable)
100  #設定する
101  set appFileManager to refMe's NSFileManager's defaultManager()
102  set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidDirPath) |error| :(reference)
103  #結果を戻す
104  return listDone
105end doDirLock
AppleScriptで生成しました

|

[リネーム] フォルダに内包されているファイルのファイルサイズを付与した名前にする(修正)

3GBを超えるとエラーになるのを修正した
AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004com.cocolog-nifty.quicktimer.icefloe
005*)
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use framework "Foundation"
009use framework "AppKit"
010use scripting additions
011
012property refMe : a reference to current application
013
014#【設定項目】基本名と日付部を区切る文字
015property strDemText : "@" as text
016
017###Wクリックで起動した場合
018on run
019  set aliasDefaultLocation to (path to desktop from user domain) as alias
020  set strPromptText to "フォルダをえらんでください"
021  set strMesText to "フォルダをえらんでください"
022  try
023    set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list
024  on error
025    log "エラーしました"
026    return "エラーしました"
027  end try
028  open listFolderPath
029end run
030
031###ドロップで起動した場合
032on open listFolderPath
033  ####################################
034  ####フォルタ以外は処理しない
035  repeat with itemFolderPath in listFolderPath
036    #パス
037    set aliasFolderPath to itemFolderPath as alias
038    set strDirPath to (POSIX path of aliasFolderPath) as text
039    set ocidDirPathStr to (refMe's NSString's stringWithString:(strDirPath))
040    set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
041    set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath))
042    #フォルダ名
043    set ocidDirName to ocidDirPathURL's lastPathComponent()
044    #コンテナ
045    set ocidContainerDirPathURL to ocidDirPathURL's URLByDeletingLastPathComponent()
046    #コンテンツの収集の設定
047    #オプション:不可視ファイルを除外
048    set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
049    #プロパティ:ファイルサイズ
050    set ocidPropertieArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0))
051    (ocidPropertieArray's addObject:(refMe's NSURLIsRegularFileKey))
052    (ocidPropertieArray's addObject:(refMe's NSURLFileSizeKey))
053    (ocidPropertieArray's addObject:(refMe's NSURLFileAllocatedSizeKey))
054    #コンテンツの収集実行
055    set appFileManager to refMe's NSFileManager's defaultManager()
056    set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieArray) options:(ocidOption) errorHandler:(reference))
057    #コンテンツをリストに
058    set ocidEmuFileURLArray to ocidEmuDict's allObjects()
059    ########
060    #【1】ディレクトリを除外して単純に『ファイルだけ』のリストにする
061    set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
062    # set numTotalFileSize to 0 as integer
063    set ocidTotalFileSize to refMe's NSDecimalNumber's zero()
064    #ファイルサイズを収集して加算していく
065    repeat with itemFilePathURL in ocidEmuFileURLArray
066      set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error| :(reference))
067      set boolIsRegularFileKey to item 2 of listResult
068      if boolIsRegularFileKey is (refMe's NSNumber's numberWithBool:true) then
069        set listResult to (itemFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileSizeKey) |error| :(reference))
070        set strFileSize to (item 2 of listResult) as text
071        set ocidFileSize to (refMe's NSDecimalNumber's alloc()'s initWithString:(strFileSize))
072        set ocidTotalFileSize to (ocidTotalFileSize's decimalNumberByAdding:(ocidFileSize))
073      end if
074    end repeat
075    # バイト単位 10.5以前やWindowsターゲットの場合は1024に
076    set ocidByteUnits to (refMe's NSDecimalNumber's alloc()'s initWithString:("1000"))
077    #バイト計算の値 1000x1000 1000x1000x1000 1000x1000x1000x1000
078    set ocidKBUnits to (ocidByteUnits's decimalNumberByMultiplyingBy:(ocidByteUnits))
079    set ocidMBUnits to (ocidKBUnits's decimalNumberByMultiplyingBy:(ocidByteUnits))
080    set ocidGBUnits to (ocidMBUnits's decimalNumberByMultiplyingBy:(ocidByteUnits))
081    #比較を行う
082    set ocidBCompare to (ocidTotalFileSize's compare:(ocidByteUnits))
083    set ocidKBCompare to (ocidTotalFileSize's compare:(ocidKBUnits))
084    set ocidMBCompare to (ocidTotalFileSize's compare:(ocidMBUnits))
085    set ocidGBCompare to (ocidTotalFileSize's compare:(ocidGBUnits))
086    #ファイルサイズ四捨五入用のフォーマット
087    set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
088    (ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundUp))
089    (ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle))
090    (ocidFormatter's setUsesGroupingSeparator:(false))
091    #ファイルサイズ格納用の可変テキスト
092    set ocidFileSizeStr to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
093    
094    #比較開始
095    if ocidBCompare = (refMe's NSOrderedAscending) then
096      log "B比較前方が小さい B単位"
097      (ocidFileSizeStr's setString:(ocidTotalFileSize's stringValue()))
098      (ocidFileSizeStr's appendString:("B"))
099      
100    else if ocidKBCompare = (refMe's NSOrderedAscending) then
101      log "KB比較前方が小さい KB単位"
102      set ocidTotalFileSize to (ocidTotalFileSize's decimalNumberByDividingBy:(ocidByteUnits))
103      (ocidFormatter's setMaximumFractionDigits:(0))
104      (ocidFileSizeStr's setString:(ocidFormatter's stringFromNumber:((ocidTotalFileSize))))
105      (ocidFileSizeStr's appendString:("KB"))
106      
107    else if ocidMBCompare = (refMe's NSOrderedAscending) then
108      log "MB比較前方が小さい MB単位"
109      set ocidTotalFileSize to (ocidTotalFileSize's decimalNumberByDividingBy:(ocidKBUnits))
110      (ocidFormatter's setMaximumFractionDigits:(1))
111      (ocidFileSizeStr's setString:(ocidFormatter's stringFromNumber:(ocidTotalFileSize)))
112      (ocidFileSizeStr's appendString:("MB"))
113      
114    else if ocidGBCompare = (refMe's NSOrderedSame) then
115      log "GB比較同数GB GB単位"
116      set ocidTotalFileSize to (ocidTotalFileSize's decimalNumberByDividingBy:(ocidMBUnits))
117      (ocidFormatter's setMaximumFractionDigits:(2))
118      (ocidFileSizeStr's setString:(ocidFormatter's stringFromNumber:(ocidTotalFileSize)))
119      (ocidFileSizeStr's appendString:("GB"))
120      
121    else if ocidGBCompare = (refMe's NSOrderedAscending) then
122      log "GB比較後方が小さいGB GB単位"
123      set ocidTotalFileSize to (ocidTotalFileSize's decimalNumberByDividingBy:(ocidMBUnits))
124      (ocidFormatter's setMaximumFractionDigits:(2))
125      (ocidFileSizeStr's setString:(ocidFormatter's stringFromNumber:(ocidTotalFileSize)))
126      (ocidFileSizeStr's appendString:("GB"))
127    end if
128    
129    if (ocidDirName as text) contains strDemText then
130      ###区切り文字でリストにして
131      set ocidDirNameArray to (ocidDirName's componentsSeparatedByString:(strDemText))
132      ###最後のアイテムを削除(旧日時)
133      ocidDirNameArray's removeLastObject()
134      ###テキストに戻す
135      set strDirName to (ocidDirNameArray's componentsJoinedByString:("")) as text
136    end if
137    #フォルダ名
138    set strDirName to (strDirName & "@" & ocidFileSizeStr) as text
139    #リネームURL
140    set ocidMoveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strDirName))
141    #リネーム
142    set listDone to (appFileManager's moveItemAtURL:(ocidDirPathURL) toURL:(ocidMoveDirPathURL) |error| :(reference))
143    if (item 2 of listDone) ≠ (missing value) then
144      log (item 2 of listDone)'s localizedDescription() as text
145      return "リネームに失敗しました"
146    end if
147    
148  end repeat
149end open
150
151
AppleScriptで生成しました

|

ファイル名のフォルダを作って中に入れる(拡張子が複数ある場合の処理を変更)


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

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

property refMe : a reference to current application

on run
  ##デスクトップ
  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
  ###ダイアログを前面に出す
  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 strMes to ("ファイルを選んでください") as text
  set strPrompt to ("ファイルを選んでください") as text
  set listUTI to {"public.data"} as list
  set listAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with multiple selections allowed and showing package contents without invisibles) as list
open listAliasFilePath
end run


on open listAliasFilePath
  ##ファイルマネージャ初期化
  set appFileManager to refMe's NSFileManager's defaultManager()
  ##繰り返しのはじまり
  repeat with itemAliasFilePath in listAliasFilePath
    set aliasFilePath to itemAliasFilePath as alias
    set strFilePath to (POSIX path of aliasFilePath) as text
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
    ##拡張子
    set ocidExtensionName to ocidFilePathURL's pathExtension()
    ##フォルダを作るディレクトリ
    set ocidConteinerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
    ##ファイル名(拡張子あり)
    set ocidFileName to ocidFilePathURL's lastPathComponent()
    ##拡張子をとって
    set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
    #カンマを置換する→これがフォルダ名
    #拡張子が2重に指定してあるケース対応でカンマをアンダースコアに置換する
    set ocidDirName to (ocidBaseFileName's stringByReplacingOccurrencesOfString:(".") withString:("_"))
    #作成するフォルダパス
    set ocidMakeDirPathURL to (ocidConteinerDirPathURL's URLByAppendingPathComponent:(ocidDirName) isDirectory:(true))
    ##フォルダパス+ファイル名で移動先
    set ocidMoveFilePathURL to (ocidMakeDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false))
    ##作ろうとしているディレクトリがすでに無いか?確認
    set ocidMakeDirPath to ocidMakeDirPathURL's |path|()
    set boolDirExists to (appFileManager's fileExistsAtPath:(ocidMakeDirPath) isDirectory:(true))
    if boolDirExists = true then
log "フォルダがすでにある"
    else if boolDirExists = false then
log "フォルダを作る"
      set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
      # 777-->511 755-->493 700-->448 766-->502
(ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
      set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidMakeDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
    end if
    ##重複チェック
    set ocidDistFilePath to doChkExists(ocidMoveFilePathURL)
    set ocidDistFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDistFilePath) isDirectory:false)
    if ocidMoveFilePathURL = ocidMoveFilePathURL then
log "置換します"
      #今あるファイルをゴミ箱に
      set listResult to (appFileManager's trashItemAtURL:(ocidDistFilePathURL) resultingItemURL:(ocidDistFilePathURL) |error|:(reference))
      #移動
      set ListDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error|:(reference))
    else
      #移動
      set ListDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error|:(reference))
    end if
    set boolDone to (item 1 of ListDone) as boolean
    if boolDone is false then
return "移動に失敗しました"
    end if
  end repeat
end open




####################################
#上書きチェック
# ocid file path = NSPathStore を返します
####################################
to doChkExists(argFilePath)
log (className() of argFilePath) as text
  if (class of argFilePath) is text then
log "テキストファイルパス"
    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
  else if (class of argFilePath) is alias then
log "エリアスファイルパス"
    set strArgFilePath to (POSIX path of argFilePath) as text
    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
  else if (class of argFilePath) is «class furl» then
log "エリアスfurlファイルパス"
    set aliasFilePath to argFilePath as alias
    set strArgFilePath to (POSIX path of argFilePath) as text
    set ocidFilePathStr to refMe's NSString's stringWithString:(argFilePath)
    set ocidArgFilePath to ocidFilePathStr's stringByStandardizingPath()
  else if (className() of argFilePath as text) contains "NSCFString" then
log "NSStringファイルパス"
    set ocidArgFilePath to argFilePath's stringByStandardizingPath()
  else if (className() of argFilePath as text) contains "NSPathStore" then
log "NSPathStore2ファイルパス"
    set ocidArgFilePath to argFilePath
  else if (className() of argFilePath as text) contains "NSURL" then
log "NSURLファイルパス"
    set ocidArgFilePath to argFilePath's |path|
  end if
  ####
  set appFileManager to refMe's NSFileManager's defaultManager()
  set boolExists to appFileManager's fileExistsAtPath:(ocidArgFilePath) isDirectory:(false)
  #
  if boolExists = true then
    ##ダイアログを前面に
    set strName to (name of current application) as text
    if strName is "osascript" then
      tell application "Finder" to activate
    else
      tell current application to activate
    end if
    set strMes to "同名のファイルがすでにあります\n上書きします?" as text
    try
      set objResponse to (display alert strMes buttons {"上書きする", "処理を中止する", "ファイル名を変更"} default button "上書きする" cancel button "処理を中止する" as informational giving up after 20)
    on error
log "処理を中止しました"
return "処理を中止しました"
      error number -128
    end try
    if true is equal to (gave up of objResponse) then
log "時間切れですやりなおしてください"
return "時間切れですやりなおしてください"
      error number -128
    end if
    if "上書きする" is equal to (button returned of objResponse) then
log "上書き保存します"
      set ocidReturnFilePath to ocidArgFilePath
    else if "ファイル名を変更" is equal to (button returned of objResponse) then
log "ファイル名を変更"
      set ocidContainerDirFilePath to ocidArgFilePath's stringByDeletingLastPathComponent()
      set strFileName to ocidArgFilePath's lastPathComponent() as text
      set aliasContainerDirPath to (POSIX file (ocidContainerDirFilePath as text)) as alias
      ##
      set strPromptText to "名前を決めてください" as text
      set strMesText to "名前を決めてください" as text
      ###ファイル名 ダイアログ
      set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strFileName with prompt strPromptText) as «class furl»
      set strFilePath to (POSIX path of aliasFilePath) as text
      set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
      set ocidReturnFilePath to ocidFilePathStr's stringByStandardizingPath()
    else if "処理を中止する" is equal to (button returned of objResponse) then
return "処理を中止しました"
    else
return "エラーしました"
      error number -128
    end if
  else if boolExists = false then
log "そのままファイル生成"
    set ocidReturnFilePath to ocidArgFilePath
  end if
return ocidReturnFilePath
  
end doChkExists

|

フォルダ内一覧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初期化