Jordan De Sotle Jordan De Sotle - 3 months ago 24
AppleScript Question

Applescript dialog list from text file and adding lines to list

I can/t get my code to work when I try to make a file that contains names. I am trying to be able to add names to a file called FirstNames.txt but it wont show up as a list like I wanted it to. I kept on getting errors like "End of file error" or "Can't make type file" and then the directory "into type file." It was frustrating so I finally made it run. When I look at the contents of the list, it just says a random number each time like 1739 or 1741. It seem to be going up by twos. I am a fairly new coder when it come to applescript and hope someone can get my script to do what I want it to do. I am not worried about the file directories but I just want it to work. Any help is appreciated. Also I made comments in the code to explain my thinking.

set myDirectory to path to desktop as text
set myFile to myDirectory & "FirstName.txt" -- Navigates to the FirstName.txt file
display dialog myFile
try
set fileContents to open for access file myFile with write permission
set myList to every paragraph of (read fileContents)

set myChoice to (choose from list myList with prompt "Choose an existing name in database or click Create New" default items "None" OK button name {"Select"} cancel button name {"Create New"})
if myChoice is false then
set newline to the text returned of (display dialog "What would you like your name to be?" default answer "- - - New Name - - -" buttons {"Ok"} default button 1)
write return & newline to fileContents starting at eof
else
set userName to myChoice
display dialog "Ok. Hello " & userName & "!"
end if
close access fileContents
on error e
display dialog e
try
close access file myFile -- Closes the access in case it is already open
end try
end try

Answer

The main issue is that you're listing the file descriptor (a number) rather than the actual contents of the file.

Replace

choose from list fileContents

with

choose from list myList

But anyway I recommend to use also the built-in write command to write the text back. Further I added a more reliable error handling.

Consider that the script doesn't work if the file FirstName.txt doesn't exist yet or is empty.

tell application "Finder" to set myDirectory to container of (path to me) as text
set myFile to myDirectory & "FirstName.txt" -- Navigates to the FirstName.txt file

try
    set fileContents to open for access file myFile with write permission
    set myList to every paragraph of (read fileContents)

    set myChoice to (choose from list myList with prompt "Choose an existing name in database or click Create New" default items "None" OK button name {"Select"} cancel button name {"Create New"})
    if myChoice is false then
        set newline to the text returned of (display dialog "What would you like your name to be?" default answer "- - - New Name - - -" buttons {"Ok"} default button 1)
        write return & newline to fileContents starting at eof
    else
        set userName to myChoice
        display dialog "Ok. Hello " & userName & "!"
    end if
    close access fileContents
on error e
    display dialog e
    try
        close access file myFile -- Closes the access in case it is already open
    end try
end try