書式つきテキスト(NSAttributedString)にコンピュータのアイコン画像を添付し、RTFDとしてファイル保存するAppleScriptです。
RTFDはRTFに対して画像などの要素を追加したApple独自の拡張仕様です。書式つきテキストはRTFとしてファイルに書き込んだり、逆にRTFから読み込んで書式つきテキスト化することも可能ですが、画像を添付したらRTFDとして保存する必要があります。
AppleScript名:書式つきテキストを組み立てて、画像を追加し、RTFDとして保存 v2 |
— Created 2017-09-25 by Takaaki Naganoya — Modified 2017-09-27 by Shane Stanley — Modified 2024-11-25 by Takaaki Naganoya — 2017-2024 Piyomaru Software use AppleScript use scripting additions use framework "Foundation" use framework "AppKit" property NSFont : a reference to current application’s NSFont property |NSURL| : a reference to current application’s |NSURL| property NSColor : a reference to current application’s NSColor property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSDictionary : a reference to current application’s NSDictionary property NSTextAttachment : a reference to current application’s NSTextAttachment property NSAttributedString : a reference to current application’s NSAttributedString property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSFontAttributeName : a reference to current application’s NSFontAttributeName property NSKernAttributeName : a reference to current application’s NSKernAttributeName property NSImageNameComputer : a reference to current application’s NSImageNameComputer property NSMutableParagraphStyle : a reference to current application’s NSMutableParagraphStyle property NSLigatureAttributeName : a reference to current application’s NSLigatureAttributeName property NSMutableAttributedString : a reference to current application’s NSMutableAttributedString property NSRTFDTextDocumentType : a reference to current application’s NSRTFDTextDocumentType property NSUnderlineStyleAttributeName : a reference to current application’s NSUnderlineStyleAttributeName property NSDocumentTypeDocumentAttribute : a reference to current application’s NSDocumentTypeDocumentAttribute property NSParagraphStyleAttributeName : a reference to current application’s NSParagraphStyleAttributeName property NSForegroundColorAttributeName : a reference to current application’s NSForegroundColorAttributeName set aFile to POSIX path of (choose file name with prompt "保存するファイル名を入力(拡張子.rtfdつきで)") set aURL to |NSURL|’s fileURLWithPath:aFile –Attributed Stringを作成 set aFontSize to 48 set aString to "My Mac" set anAssrStr to makeRTFfromParameters(aString, "HelveticaNeue-Light", aFontSize, -2, (aFontSize * 1.5)) of me –添付画像を作成 set anAttach to NSTextAttachment’s alloc()’s init() set anImage to NSImage’s imageNamed:(NSImageNameComputer) set bImage to my resizeNSImage:anImage toSize:64 anAttach’s setImage:bImage –添付画像をAttributed Stringに添付 set iconString to NSAttributedString’s attributedStringWithAttachment:anAttach anAssrStr’s insertAttributedString:iconString atIndex:0 –冒頭に添付 –添付画像つきAttributed StringをRTFDとしてファイル保存 set aStrRange to current application’s NSMakeRange(0, anAssrStr’s |length|()) set attrDict1 to NSDictionary’s dictionaryWithObject:NSRTFDTextDocumentType forKey:NSDocumentTypeDocumentAttribute set {aFileWrapper, anError} to anAssrStr’s fileWrapperFromRange:aStrRange documentAttributes:(attrDict1) |error|:(reference) if anError is not equal to missing value then error set {fRes, fError} to aFileWrapper’s writeToURL:aURL options:(current application’s NSFileWrapperWritingAtomic) originalContentsURL:(missing value) |error|:(reference) –書式つきテキストを組み立てる on makeRTFfromParameters(aStr as string, fontName as string, aFontSize as real, aKerning as real, aLineSpacing as real) –Font set aVal1 to NSFont’s fontWithName:fontName |size|:aFontSize set aKey1 to (NSFontAttributeName) –Color set aVal2 to NSColor’s blackColor() set aKey2 to (NSForegroundColorAttributeName) –Kerning set aVal3 to aKerning set akey3 to (NSKernAttributeName) –Underline set aVal4 to 0 set akey4 to (NSUnderlineStyleAttributeName) –Ligature set aVal5 to 2 –all ligature ON set akey5 to (NSLigatureAttributeName) –縦書き指定–Japanese tategaki setting (Up to down, right to left direction drawing) set aVal6 to false set aKey6 to (current application’s NSVerticalGlyphFormAttributeName) –Paragraph space set aParagraphStyle to NSMutableParagraphStyle’s alloc()’s init() aParagraphStyle’s setMinimumLineHeight:(aLineSpacing) aParagraphStyle’s setMaximumLineHeight:(aLineSpacing) set akey7 to (NSParagraphStyleAttributeName) set keyList to {aKey1, aKey2, akey3, akey4, akey5, aKey6, akey7} set valList to {aVal1, aVal2, aVal3, aVal4, aVal5, aVal6, aParagraphStyle} set attrsDictionary to NSMutableDictionary’s dictionaryWithObjects:valList forKeys:keyList set attrStr to NSMutableAttributedString’s alloc()’s initWithString:aStr attributes:attrsDictionary return attrStr end makeRTFfromParameters on resizeNSImage:aImg toSize:aSize if (aImg’s isValid()) as boolean = false then error "Invalid NSImage" set tmpView to current application’s NSImageView’s alloc()’s initWithFrame:(current application’s NSMakeRect(0, 0, aSize, aSize)) tmpView’s setImageScaling:(current application’s NSImageScaleProportionallyUpOrDown) tmpView’s setImage:aImg set newRect to tmpView’s frame() set newRep to tmpView’s bitmapImageRepForCachingDisplayInRect:newRect tmpView’s cacheDisplayInRect:newRect toBitmapImageRep:newRep set newData to newRep’s TIFFRepresentation() –representationUsingType:(current application’s NSPNGFileType) |properties|:(missing value) return current application’s NSImage’s alloc()’s initWithData:newData end resizeNSImage:toSize: |