Including emoticons in an .ics file

Hi All,

I would like to include an :airplane: symbol in the Summary line of an .ics file I construct.

I am able to manually add any emoticon to an event I create in Calendar so I know that this is possible.

However, when I use AppleScript to generate an .ics file with an emoticon in the Summary line the output only generates a “?” symbol.

Perhaps I need to specify the emoticon as a Unicode text number?

Any help would be appreciated.

Cheers,

Kevin


property thePath : ""
property fRef : ""

set mySummary to "Brisbane to Hong Kong"
set myStartDate to "20180216T004500"
set myEndDate to "20180216T071000"
set myLocation to "A350G"
set myIcon to "✈"

set thePath to (path to documents folder as Unicode text) & "TempRoster.ics"

icsFileHeader(thePath) of me
writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
icsFileFooter(thePath) of me


on icsFileHeader(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		set eof fRef to 0
		write "BEGIN:VCALENDAR
X-WR-TIMEZONE:Floating
METHOD:PUBLISH
" to fRef
	end try
	close access fRef
end icsFileHeader

on writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
	set fRef to (open for access file thePath with write permission)
	try
		write "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" to fRef starting at eof
		set theUID to theUID + 1
	end try
	close access fRef
end writeEventToFile

on icsFileFooter(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		write "END:VCALENDAR" to fRef starting at eof
	end try
	close access fRef
end icsFileFooter

--Result is..

--BEGIN:VCALENDAR
--X-WR-TIMEZONE:Floating
--METHOD:PUBLISH
--BEGIN:VEVENT
--SUMMARY:?? Brisbane to Hong Kong
--LOCATION:A350G
--DTSTART:20180216T004500
--DTEND:20180216T071000
--END:VEVENT
--END:VCALENDAR

Model: MacBook Pro (13-inch, 2016, Four Thunderbolt 3 Ports)
AppleScript: Version 2.10 (194)
Browser: Safari 604.5.6
Operating System: Mac OS X (10.13 Developer Beta 3)

The write command writes using MacRoman (or locale equivalent) decoding by default. You need to add “as «class utf8»” to the write statements.

Hi Shane,

Thanks for the prompt response


Tried this but get an error message in Script Debugger of;

get <> “Begin:VEVENT\n”
no such object (e.g. specifier asked for the 3rd, but there are only 2) (errAENoSuchObject:-1728)

property thePath : ""
property fRef : ""

set mySummary to "Brisbane to Hong Kong"
set myStartDate to "20180216T004500"
set myEndDate to "20180216T071000"
set myLocation to "A350G"
set myIcon to "✈"

set thePath to (path to documents folder as Unicode text) & "TempRoster.ics"

icsFileHeader(thePath) of me
writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
icsFileFooter(thePath) of me


on icsFileHeader(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		set eof fRef to 0
		write "BEGIN:VCALENDAR
X-WR-TIMEZONE:Floating
METHOD:PUBLISH
" to fRef
	end try
	close access fRef
end icsFileHeader

on writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
	set fRef to (open for access file thePath with write permission)
	try
		write as «class utf8» "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" to fRef starting at eof
		set theUID to theUID + 1
	end try
	close access fRef
end writeEventToFile

on icsFileFooter(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		write "END:VCALENDAR" to fRef starting at eof
	end try
	close access fRef
end icsFileFooter

--Result is..

--BEGIN:VCALENDAR
--X-WR-TIMEZONE:Floating
--METHOD:PUBLISH
--END:VCALENDAR

Hi Kevin.

‘as «class utf8»’ is a labelled parameter of the ‘write’ command, the ‘as’ being the label. So it should go somewhere after the text, which is the direct parameter. eg.

		write "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" as «class utf8» to fRef starting at eof

Or:

		write "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" to fRef as «class utf8» starting at eof

You’ve added it in the wrong place – you can’t put it between the command and the direct parameter. It’s good practice to keep your encoding consistent when writing files, too. So CX156 would be:

property thePath : ""
property fRef : ""

set mySummary to "Brisbane to Hong Kong"
set myStartDate to "20180216T004500"
set myEndDate to "20180216T071000"
set myLocation to "A350G"
set myIcon to "✈"

set thePath to (path to documents folder as Unicode text) & "TempRoster.ics"

icsFileHeader(thePath) of me
writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
icsFileFooter(thePath) of me


on icsFileHeader(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		set eof fRef to 0
		write "BEGIN:VCALENDAR
X-WR-TIMEZONE:Floating
METHOD:PUBLISH
" as «class utf8» to fRef
	end try
	close access fRef
end icsFileHeader

on writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
	set fRef to (open for access file thePath with write permission)
	try
		write "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" as «class utf8» to fRef starting at eof
		set theUID to theUID + 1
	end try
	close access fRef
end writeEventToFile

on icsFileFooter(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		write "END:VCALENDAR" to fRef starting at eof
	end try
	close access fRef
end icsFileFooter

In fact, I’d be inclined to rework it so there’s a single write. It would be more efficient, and with UTF it’s generally a bit safer.

Thank you both Shane and Nigel,

This works perfectly. (Interesting that you determined the correct flight number Shane).

I have to separate the writes because I have multiple events that get written in a repeat loop, this is just a snippet of a much larger script.

This forum continues to save me time and teach me more about scripting.

Thanks again,

Kev

Once the file’s existence has been established with your icsFileHeader handler, the other two don’t need to open and close access to it explicity. Provided that the initial access with write permission has been closed, you can just write to the file using the file specifier and the necessary access will be handled automatically. This is a bit more efficient than using ‘open for access’ and ‘close access’ for every single write to it.

on icsFileHeader(thePath)
	set fRef to (open for access file thePath with write permission)
	try
		set eof fRef to 0
		write "BEGIN:VCALENDAR
X-WR-TIMEZONE:Floating
METHOD:PUBLISH
" as «class utf8» to fRef
	end try
	close access fRef
end icsFileHeader

on writeEventToFile(mySummary, myStartDate, myEndDate, myLocation, myIcon)
	write "BEGIN:VEVENT
" & "SUMMARY:" & myIcon & " " & mySummary & "
LOCATION:" & myLocation & "
DTSTART:" & myStartDate & "
DTEND:" & myEndDate & "
END:VEVENT
" as «class utf8» to file thePath starting at eof
	--set theUID to theUID + 1
end writeEventToFile

on icsFileFooter(thePath)
	write "END:VCALENDAR" as «class utf8» to file thePath starting at eof
end icsFileFooter

I see Nigel answered while I was having dinner :slight_smile:

:cool: Hope it was a smooth flight.