Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript Turning list in .txt file into Choose From List

I am working on an applescript that will take a list of names in a .txt file and turn it into a list that the user can choose from in a "Choose from list" box.


While you can put a variable into the list brackets ( ex.: {variable_name, variable_name2}) and then assign a string to each variable, so that variable_name can stand for "2", "3", "4", and that will make 4 separate items in the dialog box. Example:


set variable_name to {"2", "3", "4"}

choose from list variable_name --

It displays 3 items to click in the choose from list box


But when I try:


set variable_name to (read file TextFile.txt)

choose from list variable_name


It displays 1 item to click in the choose from list box.


When I write to the .txt file, I'm using this command:


my WriteLog("\"2\", \"3\"")

on WriteLog(the_text)

set this_story to the_text

set this_file to (((path to desktop folder) as text) & "TextFile.txt")

my write_to_file(this_story, this_file, true)

end WriteLog

on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)

try

set the target_file to the target_file as text

set the open_target_file to ¬

open for access file target_file with write permission

if append_data is false then ¬

set eof of the open_target_file to 0

write this_data to the open_target_file starting at eof

close access the open_target_file

return true

on error

try

close access file target_file

end try

return false

end try

end write_to_file

MacBook Pro (13-inch Mid 2012), OS X Mavericks (10.9.5)

Posted on Mar 24, 2015 1:05 PM

Reply
Question marked as Best reply

Posted on Mar 24, 2015 1:38 PM

It displays 1 item to click in the choose from list box.


Sure, because when you read the file, you get one single text object containing all the file data.


You need to coerce it into a list that represents the individual items available to select. The easiest way is to ask AppleScript to parse the paragraphs of the text file:


set variable_name to (paragraphs of (readfilemyTextFile))


Now AppleScript will read the file as a single text object, then break it down into a list based on the paragraph delimiters, effectively ending up with one list item per line in the file, which sounds like what you want.

4 replies
Question marked as Best reply

Mar 24, 2015 1:38 PM in response to tc8213

It displays 1 item to click in the choose from list box.


Sure, because when you read the file, you get one single text object containing all the file data.


You need to coerce it into a list that represents the individual items available to select. The easiest way is to ask AppleScript to parse the paragraphs of the text file:


set variable_name to (paragraphs of (readfilemyTextFile))


Now AppleScript will read the file as a single text object, then break it down into a list based on the paragraph delimiters, effectively ending up with one list item per line in the file, which sounds like what you want.

Mar 24, 2015 6:58 PM in response to Camelot

That did it! Thank you! I'll just make sure that the names are each on a separate line. Appreciate the quick help!



Now one other question you might be able to help with (if not don't worry):


When I use this line: my WriteLog("Müller"), notice that it has a German umlaut. I have textedit set to open the file with UTF-8 and all. But it sends me this error when I try to look at the text file:


The document “MY STORY” could not be opened. Text encoding Unicode (UTF-8) isn’t applicable. The file may have been saved using a different text encoding, or it may not be a text file.


I thought UTF-8 could handle special characters?

Mar 24, 2015 9:00 PM in response to tc8213

I thought UTF-8 could handle special characters?

UTF-8 can, but it isn't a UTF-8 file, hence the problem.


A quick peek in Terminal.app shows:


$ file ~/Desktop/TextFile.txt

TextFile.txt: Non-ISO extended-ASCII text, with no line terminators


So AppleScript is writing the file as extended ASCII, not UTF-8.


The simplest solution is to have TextEdit.app auto-detect the file type, rather than forcing UTF-8 (I honestly don't understand why the option exists at all in TextEdit preferences...), otherwise you can try writing the UTF-8 BOM to the file to provide a hint, as well as write the data as UTF-8:



write (this_data as «class utf8») to the open_target_filestarting ateof

Applescript Turning list in .txt file into Choose From List

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.