« [attributesOfItemAtPath]属性の取得 | トップページ | [プレビュー]ファイル名順に開く »

[Contacts]選択中のアドレスのQRコードを作成します

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "CoreImage"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
set objFileManager to objMe's NSFileManager's defaultManager()

tell application "Contacts"

set listOfSelection to selection as list
set numCntSelection to (count of listOfSelection) as number

if numCntSelection > 1 then
set strErrorMes to "選択カード(人)は1つにしてください"
return strErrorMes
end if

set strCardID to item 1 of listOfSelection
tell strCardID
(*
last name
suffix
title
company
first name
image
nickname
organization
maiden name
name
home page
job title
modification date
phonetic middle name
department
middle name
birth date
phonetic last name
id
*)
set strFirstName to first name
set strLastName to last name
set strName to name as text
set strVcard to vcard as text
end tell
end tell

if strName is missing value then
set strName to (strLastName & strFirstName) as text
end if

######################
##set strVcard to doReplace(strVcard, "PRODID:-//Apple Inc.//macOS 12.3.1//EN\r\n", "") as text
##set strVcard to doReplace(strVcard, "\r\n\r\n", "\r\n") as text
######################
set strQRvcard to "" as text

set listEveryParagraph to every paragraph of strVcard
##################
#Vcardの中に『写真』データがあるとエラーになるので
#不要なデータを行を削除するようにしています
####Vcard整形
repeat with strParagraph in listEveryParagraph
set strParagraph to strParagraph as text
if strParagraph starts with "PRODID" then
set strParagraph to ""
else if strParagraph starts with " " then
set strParagraph to ""
else if strParagraph starts with "PHOTO" then
set strParagraph to ""
else if strParagraph starts with "X-ALTBDAY" then
set strParagraph to ""
####このelseIFを増やせば非表示項目を増やせます
else
set strQRvcard to strQRvcard & strParagraph & "\r\n" as text
end if
end repeat
set strVcard to doReplace(strQRvcard, "\r\n\r\n", "\r\n") as text

set strText to strVcard as text


###フォルダのパス
###ピクチャーフォルダのパス
set aliasDirPath to (path to pictures folder from user domain) as alias
set strDirPath to POSIX path of aliasDirPath as text
####フォルダ名
set strFolderName to "QRcode" as text
set strDirPath to ("" & strDirPath & strFolderName & "") as text
###NSStringテキスト
set ocidDirPath to objNSString's stringWithString:strDirPath
###フォルダを作る
###途中のフォルダも作る-->true
set boolMakeNewFolder to (objFileManager's createDirectoryAtPath:ocidDirPath withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))

####日付フォーマット
set prefDateFormat to "yyyyMMddHHmmss"
set ocidFormatter to objMe's NSDateFormatter's alloc()'s init()
ocidFormatter's setLocale:(objMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidFormatter's setDateFormat:(prefDateFormat as string)
set strDateAndTime to (ocidFormatter's stringFromDate:(current date)) as text
#####日付をファイル名にする
##set strSaveFilePath to strDirPath & "/" & strDateAndTime & ".png"
set strSaveFilePath to strDirPath & "/" & strName & ".png"

################################################################
set strInputText to strText as text
####テキストをNSString
set ocidInputString to objNSString's stringWithString:strInputText
####テキストをUTF8
set ocidUtf8InputString to ocidInputString's dataUsingEncoding:(objMe's NSUTF8StringEncoding)
####CIQRCodeGenerator初期化
set ocidQRcodeImage to objMe's CIFilter's filterWithName:"CIQRCodeGenerator"
ocidQRcodeImage's setDefaults()
###テキスト設定
ocidQRcodeImage's setValue:ocidUtf8InputString forKey:"inputMessage"
###読み取り誤差値設定L, M, Q, H
ocidQRcodeImage's setValue:"Q" forKey:"inputCorrectionLevel"
###QRコード本体のイメージ
set ocidCIImage to ocidQRcodeImage's outputImage()
################################################################


-->ここで生成されるのはQRのセルが1x1pxの最小サイズ
###QRコードの縦横取得
set ocidCIImageDimension to ocidCIImage's extent()
set ocidCIImageWidth to (item 1 of item 2 of ocidCIImageDimension) as integer
set ocidCIImageHight to (item 2 of item 2 of ocidCIImageDimension) as integer
###最終的に出力したいpxサイズ
set numScaleMax to 580
###整数で拡大しないとアレなのでの値のニアなサイズになります
set numWidth to ((numScaleMax / ocidCIImageWidth) div 1) as integer
set numHight to ((numScaleMax / ocidCIImageHight) div 1) as integer
###↑サイズの拡大縮小する場合はここで値を調整すれば良い
####変換スケール作成-->拡大
set recordScalse to objMe's CGAffineTransform's CGAffineTransformMakeScale(numWidth, numHight)
##変換スケールを適応(元のサイズに元のサイズのスケール適応しても意味ないけど
set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:recordScalse
#######元のセルが1x1pxの最小サイズで出したいときはここで処理
##set ocidCIImageScaled to ocidCIImage
###イメージデータを展開
set ocidNSCIImageRep to objMe's NSCIImageRep's imageRepWithCIImage:ocidCIImageScaled
###出力用のイメージの初期化
set ocidNSImageScaled to objMe's NSImage's alloc()'s initWithSize:(ocidNSCIImageRep's |size|())
###イメージデータを合成
ocidNSImageScaled's addRepresentation:ocidNSCIImageRep
###出来上がったデータはOS_dispatch_data
set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation()
####NSBitmapImageRep
set ocidNSBitmapImageRep to objMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData
#################################################################
###quiet zone用に画像をパディングする
set numPadWidth to ((ocidCIImageWidth * numWidth) + (numWidth * 6)) as integer
set numPadHight to ((ocidCIImageHight * numHight) + (numHight * 6)) as integer
###左右に3セル分づつ余白 quiet zoneを足す
####まずは元のQRコードのサイズに6セルサイズ分足したサイズの画像を作って
set ocidNSBitmapImagePadRep to (objMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numPadWidth pixelsHigh:numPadHight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(objMe's NSCalibratedRGBColorSpace) bitmapFormat:(objMe's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
###初期化
objMe's NSGraphicsContext's saveGraphicsState()
###ビットマップイメージ
(objMe's NSGraphicsContext's setCurrentContext:(objMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidNSBitmapImagePadRep))
###塗り色を『白』に指定して
objMe's NSColor's whiteColor()'s |set|()
##画像にする
objMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numPadWidth, height:numPadHight}})
###出来上がった画像にQRバーコードを左右3セル分ずらした位置にCompositeSourceOverする
ocidNSBitmapImageRep's drawInRect:{origin:{x:(numWidth * 3), y:(numHight * 3)}, |size|:{width:numPadWidth, Hight:numPadHight}} fromRect:{origin:{x:0, y:0}, |size|:{width:numPadWidth, height:numPadHight}} operation:(objMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
####画像作成終了
objMe's NSGraphicsContext's restoreGraphicsState()

#################################################################

####JPEG用の圧縮プロパティ
##set ocidNSSingleEntryDictionary to objMe's NSDictionary's dictionaryWithObject:1 forKey:(objMe's NSImageCompressionFactor)
####PNG用の圧縮プロパティ
set ocidNSSingleEntryDictionary to objMe's NSDictionary's dictionaryWithObject:true forKey:(objMe's NSImageInterlaced)

#####出力イメージへ変換
set ocidNSInlineData to (ocidNSBitmapImagePadRep's representationUsingType:(objMe's NSBitmapImageFileTypePNG) |properties|:ocidNSSingleEntryDictionary)
(*
NSBitmapImageFileTypeJPEG
NSBitmapImageFileTypePNG
NSBitmapImageFileTypeGIF
NSBitmapImageFileTypeBMP
NSBitmapImageFileTypeTIFF
NSBitmapImageFileTypeJPEG2000
*)
######インラインデータをファイルに書き出し
set boolMakeQrCode to (ocidNSInlineData's writeToFile:strSaveFilePath atomically:true)

set aliasDirPath to POSIX file strDirPath as alias

tell application "Finder"
select aliasDirPath
end tell
tell application "Preview"
launch
open aliasDirPath
end tell
return true





to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

|

« [attributesOfItemAtPath]属性の取得 | トップページ | [プレビュー]ファイル名順に開く »

AppleScript Contacts」カテゴリの記事

AppleScript NSImage」カテゴリの記事