use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableDictionary : a reference to refMe's NSMutableDictionary
property refNSURL : a reference to refMe's NSURL
property refNSBitmapImageRep : a reference to refMe's NSBitmapImageRep
set objFileManager to refMe's NSFileManager's defaultManager()
tell application "Calendar" to launch
tell application "Calendar" to activate
repeat
tell application "Calendar"
set boolFrontMost to frontmost
end tell
if boolFrontMost is true then
tell application "Calendar"
activate
reload calendars
delay 0.5
end tell
exit repeat
end if
delay 1
end repeat
tell application "Calendar"
activate
tell application "System Events"
keystroke "r" using command down
end tell
delay 0.5
end tell
set strPlistFilePath to "~/Library/Preferences/com.apple.iCal.plist"
set ocidRelativePath to refNSString's stringWithString:strPlistFilePath
set ocidPlistFullPath to ocidRelativePath's stringByStandardizingPath
set ocidPlistPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidPlistFullPath
set listReadPlistData to refNSMutableDictionary's dictionaryWithContentsOfURL:ocidPlistPathURL |error|:(reference)
set ocidPlistDict to item 1 of listReadPlistData
set strSelectedListUUID to (ocidPlistDict's valueForKey:"last selected calendar list item") as text
set ocdiSelectedEventsArray to ((ocidPlistDict's valueForKey:"SelectedEvents")'s valueForKey:"iCal")
if (count of ocdiSelectedEventsArray) = 0 then
return "イベントを選択してから実行してください"
error "イベントを選択してから実行してください" number -200
else if (count of ocdiSelectedEventsArray) ≥ 2 then
return "QRコードは1回に1コのイベント選択でお願いします"
error "QRコードは1回に1コのイベント選択でお願いします" number -201
end if
set strSelectedEventUUID to item 1 of ocdiSelectedEventsArray as text
tell application "Calendar"
activate
tell calendar id (strSelectedListUUID)
get name
get color
get writable
get description
tell event id (strSelectedEventUUID)
show
get recurrence
get stamp date
get excluded dates
get status
get uid
set theDTSTART to ""
set theDTSTART to (get start date)
set theDTEND to ""
set theDTEND to (get end date)
set theAllDay to (get allday event)
if theAllDay is "missing value" then
set theAllDay to false as boolean
end if
set theSummary to (get summary) as text
if theSummary is "missing value" then
set theSummary to theDTSTART as text
end if
set theDescription to (get description) as text
if theDescription is "missing value" then
set theDescription to "" as text
end if
set theSequence to (get sequence)
if theSequence is "missing value" then
set theSequence to "1" as text
end if
set theLocation to (get location) as text
if theLocation is "missing value" then
set theLocation to "" as text
end if
set theURL to (get url)
if theURL is "missing value" then
set theURL to "https://www.icloud.com/calendar/" as text
end if
end tell
end tell
end tell
if theAllDay is false then
set theDTSTART to ("DTSTART:" & getEventDateTime(theDTSTART))
set theDTEND to ("DTEND:" & getEventDateTime(theDTEND))
else
set theDTSTART to ("DTSTART;VALUE=DATE:" & getEventDate(theDTSTART))
set theDTEND to ("DTEND;VALUE=DATE:" & getEventDate(theDTEND))
end if
set theSequence to ("SEQUENCE:" & theSequence) as text
set theLocation to ("LOCATION:" & theLocation) as text
set theSummary to ("SUMMARY:" & theSummary) as text
if theDescription contains "FaceTime" then
set theDescription to doReplace(theDescription, "\r\n", "")
log theDescription
set AppleScript's text item delimiters to "\n"
set listDescription to every text item of theDescription as list
set AppleScript's text item delimiters to ""
set theURL to item 3 of listDescription as text
set theURL to doReplace(theURL, " ", "")
set theDescription to ("DESCRIPTION:Facetimeミーティング") as text
else
set theDescription to ("DESCRIPTION:" & theDescription) as text
end if
set theURL to ("URL;VALUE=URI:" & theURL) as text
set theALARM to "BEGIN:VALARM\nTRIGGER:-PT1H\nATTACH;VALUE=URI:Chord\nACTION:AUDIO\nEND:VALARM"
set strEventText to "BEGIN:VEVENT\n" & "\n" & theSummary & "\n" & theLocation & "\n" & theSequence & "\n" & theDTSTART & "\n" & theURL & "\n" & theDescription & "\n" & theDTEND & "\n" & theALARM & "\nEND:VEVENT" as text
set ocidEventString to refNSString's stringWithString:strEventText
set strSaveDirPath to "~/Pictures/QRcode"
set ocidSaveDirPath to refNSString's stringWithString:strSaveDirPath
set ocidSaveDirFullPath to ocidSaveDirPath's stringByStandardizingPath
set ocidSaveDirPathURL to refNSURL's alloc()'s initFileURLWithPath:ocidSaveDirFullPath
set boolMakeNewFolder to (objFileManager's createDirectoryAtURL:ocidSaveDirPathURL withIntermediateDirectories:true attributes:({NSFilePosixPermissions:511}) |error|:(reference))
set prefDateFormat to "yyyyMMddHHmmss"
set ocidFormatter to refMe's NSDateFormatter's alloc()'s init()
ocidFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidFormatter's setDateFormat:(prefDateFormat as string)
set strDateAndTime to (ocidFormatter's stringFromDate:(current date)) as text
set strSaveFileName to ("" & strDateAndTime & ".tif") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:strSaveFileName
set ocidUtf8InputString to ocidEventString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)
set ocidQRcodeImage to refMe's CIFilter's filterWithName:"CIQRCodeGenerator"
ocidQRcodeImage's setDefaults()
ocidQRcodeImage's setValue:ocidUtf8InputString forKey:"inputMessage"
ocidQRcodeImage's setValue:"Q" forKey:"inputCorrectionLevel"
set ocidCIImage to ocidQRcodeImage's outputImage()
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
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 refMe's CGAffineTransform's CGAffineTransformMakeScale(numWidth, numHight)
set ocidCIImageScaled to ocidCIImage's imageByApplyingTransform:recordScalse
set ocidNSCIImageRep to refMe's NSCIImageRep's imageRepWithCIImage:ocidCIImageScaled
set ocidNSImageScaled to refMe's NSImage's alloc()'s initWithSize:(ocidNSCIImageRep's |size|())
ocidNSImageScaled's addRepresentation:ocidNSCIImageRep
set ocidOsDispatchData to ocidNSImageScaled's TIFFRepresentation()
set ocidNSBitmapImageRep to refMe's NSBitmapImageRep's imageRepWithData:ocidOsDispatchData
set numPadWidth to ((ocidCIImageWidth * numWidth) + (numWidth * 6)) as integer
set numPadHight to ((ocidCIImageHight * numHight) + (numHight * 6)) as integer
set ocidColorSpaceName to refMe's NSCalibratedWhiteColorSpace
set ocidNSBitmapImageFileType to refMe's NSBitmapImageFileTypeTIFF
set ocidBitmapFormat to refMe's NSBitmapFormatAlphaNonpremultiplied
set ocidNSBitmapImagePadRep to (refNSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:numPadWidth pixelsHigh:numPadHight bitsPerSample:8 samplesPerPixel:1 hasAlpha:false isPlanar:false colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:0 bitsPerPixel:0)
log ocidNSBitmapImagePadRep
refMe's NSGraphicsContext's saveGraphicsState()
(refMe's NSGraphicsContext's setCurrentContext:(refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:ocidNSBitmapImagePadRep))
set ocidSetColor to refMe's NSColor's colorWithWhite:1.0 alpha:1.0
ocidSetColor's |set|()
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:numPadWidth, height:numPadHight}})
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:(refMe's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
refMe's NSGraphicsContext's restoreGraphicsState()
set ocidNSBitmapImageRepPropertyKey to refNSMutableDictionary's alloc()'s initWithCapacity:0
ocidNSBitmapImageRepPropertyKey's setObject:0 forKey:(refMe's NSImageCompressionFactor)
set ocidNSInlineData to (ocidNSBitmapImagePadRep's representationUsingType:(ocidNSBitmapImageFileType) |properties|:ocidNSBitmapImageRepPropertyKey)
set boolMakeQrCode to (ocidNSInlineData's writeToURL:ocidSaveFilePathURL atomically:true)
tell application "Finder"
select (ocidSaveFilePathURL as alias)
end tell
tell application "Preview"
launch
activate
open (ocidSaveFilePathURL as alias)
end tell
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
to getEventDate(str)
set theYear to year of (str) as number
set theMonth to month of (str) as number
if theMonth < 10 then
set theMonth to ("0" & theMonth) as text
else
set theMonth to theMonth as text
end if
set theDate to day of (str) as number
if theDate < 10 then
set theDate to ("0" & theDate) as text
else
set theDate to theDate as text
end if
return (theYear & theMonth & theDate) as text
end getEventDate
to getEventDateTime(str)
set theYear to year of (str) as number
set theMonth to month of (str) as number
if theMonth < 10 then
set theMonth to ("0" & theMonth) as text
else
set theMonth to theMonth as text
end if
set theDate to day of (str) as number
if theDate < 10 then
set theDate to ("0" & theDate) as text
else
set theDate to theDate as text
end if
set theTime to time of (str)
set theHours to theTime div hours
if theHours < 10 then
set theHours to ("0" & theHours) as text
else
set theHours to theHours as text
end if
set theMinutes to (theTime - (theHours) * hours) div minutes
if theMinutes < 10 then
set theMinutes to ("0" & theMinutes) as text
else
set theMinutes to theMinutes as text
end if
set theSeconds to theTime mod minutes
if theSeconds < 10 then
set theSeconds to ("0" & theSeconds) as text
else
set theSeconds to theSeconds as text
end if
return (theYear & theMonth & theDate & "T" & theHours & theMinutes & theSeconds) as text
end getEventDateTime