指定画像をbase64でエンコードしたのちに、デコードして画像に戻すAppleScriptです。
base64文字列からのデコードだけをテストしたかったのですが、テストのために本来は不要な画像のエンコード部分を付け足しています。
macOS 13.5上で動作確認していますが、OSのバージョンやAppleScriptのバージョンに依存はしないことでしょう。
AppleScript名:指定画像をbase64エンコード文字列に変換→デコード.scptd |
— – Created by: Takaaki Naganoya – Created on: 2023/07/29 — – Copyright © 2023 Piyomaru Software, All Rights Reserved — property NSData : a reference to current application’s NSData property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSPNGFileType : a reference to current application’s NSPNGFileType property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding property NSDataBase64EncodingEndLineWithLineFeed : a reference to current application’s NSDataBase64EncodingEndLineWithLineFeed use AppleScript version "2.8" — macOS 12 or later use framework "Foundation" use scripting additions set aFile to choose file of type {"public.image"} set aStr to base64StringFromImageFile(aFile) of me –> "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAKuWlDQ1BJQ0MgU……" set aImage to decodeImageFromBase64String(aStr) of me –> (NSImage) –Base64 Decode on decodeImageFromBase64String(aString) set restoreData to NSData’s alloc()’s initWithBase64EncodedString:aString options:0 set restoreImage to NSImage’s alloc()’s initWithData:restoreData return restoreImage end decodeImageFromBase64String –Base64 Encode on base64StringFromImageFile(aFile) set aPOSIX to POSIX path of aFile set anImage to NSImage’s alloc()’s initWithContentsOfFile:aPOSIX set imageRep to NSBitmapImageRep’s alloc()’s initWithData:(anImage’s TIFFRepresentation()) set aPNGdat to imageRep’s representationUsingType:(NSPNGFileType) |properties|:(missing value) set base64Str to aPNGdat’s base64EncodedDataWithOptions:(NSDataBase64EncodingEndLineWithLineFeed) set bStr to (NSString’s alloc()’s initWithData:base64Str encoding:(NSUTF8StringEncoding)) return bStr as string –or return NSString (delete as string) for speedy processing end base64StringFromImageFile |
More from my site
(Visited 45 times, 1 visits today)