Small problem with a handler using Applescript and Xcode

This works as a stand alone script:

loadList()

on loadList()
set delimitedList to {}
try
set myPath to ((path to home folder) & “Just A Folder:” as Unicode text)
set oldDelims to AppleScript’s text item delimiters – save their current state
set AppleScript’s text item delimiters to {“\n”} – declare new delimiters
set fRef to open for access file (myPath & “theList.txt”)
set theText to read fRef as «class utf8»
set delimitedList to every text item of theText
close access fRef
set AppleScript’s text item delimiters to oldDelims – restore them
on error
set AppleScript’s text item delimiters to oldDelims – restore them in case something went wrong
end try

display dialog item 1 of delimitedList
display dialog item 2 of delimitedList

end loadList

However, when I use the same code in an Xcode application, it fails with nothing in delimitedList, with is error:

Can’t get item 1 of {}. (error -1728)

Any help is appreciated.

Hi,

there is a clash with the file keyword. You have to tell the current application to perform the read operation
Actually you can replace your handler with this code, paragraphs of does the same as text item delimiters set to newline and open for access is not needed to read a file


on loadList()
	set myPath to (path to home folder as text) & "Just A Folder:theList.txt"
	try
		tell current application to set delimitedList to paragraphs of (read file myPath as «class utf8»)
		
		display dialog item 1 of delimitedList
		display dialog item 2 of delimitedList
	on error e
		display dialog e
	end try
end loadList

Thanks much, Stefan! I always appreciate your assistance.