Finder上で選択中の画像をアンチエイリアスありで50%にリサイズするAppleScriptです。
よく使う拡大/縮小倍率のScriptを用意してmacOS標準装備のScript Menuに入れておき、Menuから呼び出して使っています。
AppleScript名:Finder上で選択中の画像をアンチエイリアスありで50%にリサイズ |
— Created 2014-02-21 Shane Stanley — Modified 2018-06-01 Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSPredicate : a reference to current application’s NSPredicate property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey set zoomPercent to 0.5 tell application "Finder" set inFiles to selection as alias list end tell –指定のAlias listのうち画像のみ抽出 set filRes1 to filterAliasListByUTI(inFiles, "public.image") of me if filRes1 = {} then return –選択中のファイルのうちの1つから親フォルダを求め、出力先ファイルパスを組み立てる set outPathTarg to POSIX path of (first item of filRes1) set pathString to current application’s NSString’s stringWithString:outPathTarg set newPath to (pathString’s stringByDeletingLastPathComponent()) as string repeat with i in filRes1 set outPOSIXpath to newPath & "/" & (current application’s NSUUID’s UUID()’s UUIDString()) as string set fRes1 to resizeImage(i, outPOSIXpath, zoomPercent) of me end repeat on resizeImage(imagePath, newPath, aZoom as real) –ファイルパスの拡張子を取得する set pathString to current application’s NSString’s stringWithString:imagePath set fileExt to (pathString’s pathExtension()) as string –画像ファイルをNSImageに読み込み set theImage to current application’s NSImage’s alloc()’s initWithContentsOfFile:imagePath — load the file as an NSImage — calculate required sizes set theSize to (theImage’s |size|()) as record set oldWidth to width of theSize set oldHeight to height of theSize set theWidth to oldWidth * aZoom set theHeight to oldHeight * aZoom set newImage to current application’s NSImage’s alloc()’s initWithSize:(current application’s NSMakeSize(theWidth, theHeight)) newImage’s lockFocus() theImage’s drawInRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:theHeight}} fromRect:(current application’s NSZeroRect) operation:(current application’s NSCompositeSourceOver) fraction:1.0 newImage’s unlockFocus() set theData to newImage’s TIFFRepresentation() set newRep to current application’s NSBitmapImageRep’s imageRepWithData:theData –元画像のファイルファイルフォーマットに合わせて同じフォーマットに if fileExt is in {"png", "PNG"} then set theData to (newRep’s representationUsingType:(current application’s NSPNGFileType) |properties|:{NSImageGamma:1.0}) else if fileExt is in {"jpg", "jpeg"} then set theData to (newRep’s representationUsingType:(current application’s NSJPEGFileType) |properties|:{NSImageCompressionFactor:1.0}) else if fileExt is in {"tif", "tiff"} then set theData to (newRep’s representationUsingType:(current application’s NSTIFFFileType) |properties|:(missing value)) else if fileExt is in {"bmp"} then set theData to (newRep’s representationUsingType:(current application’s NSBMPFileType) |properties|:(missing value)) end if set newPath to newPath & "." & fileExt return (theData’s writeToFile:newPath atomically:true) end resizeImage –Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す on filterAliasListByUTI(aList, targUTI) set newList to {} repeat with i in aList set j to POSIX path of i set tmpUTI to my retUTIfromPath(j) set utiRes to my filterUTIList({tmpUTI}, targUTI) if utiRes is not equal to {} then set the end of newList to j end if end repeat return newList end filterAliasListByUTI –指定のPOSIX pathのファイルのUTIを求める on retUTIfromPath(aPOSIXPath) set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value) if theResult = true then return theValue as string else return theResult end if end retUTIfromPath –UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList |
More from my site
(Visited 41 times, 1 visits today)