I’m farily new to AppleScript studio, so pardon the crudeness of my code. Anyway, I’ll get to the point. I have an external text file delimited by carriage returns:
Item1
Item2
Item3
Item4
etc…
I want my script to read the file, convert it to a list, and then add each item to the popup button.
Here’s what I have so far (I want it to do it upon loading the NIB, but I’m not sure how to do it yet. So I’m clicking a button atm.)
on clicked theObject
delete every menu item of menu of popup button "myButton" of window "myWindow"
set file_path to (((path to desktop) as string) & "test.txt") as file specification
set read_data to read file_path using delimiter return
-- Debug: display dialog read_data
repeat with i from 1 to number of items of read_data
set current_item to item i as string
make new menu item at end of menu of popup button "myButton" of window "myWindow" with properties {title:current_item, enabled:true}
end repeat
end clicked
Unfortunately, I keep getting the following error:
2003-11-24 04:36:14.262 Login[1204] *** -[NSCFArray length]: selector not recognized
Why, yes it did. Thanks. I didn’t think to use “length of.”
Quick couple questions, however:
To make it run on startup, so after the interface loads the popup button will be cleared and refreshed with the current list, would I attach the script to “awake from nib?” I’ll play around with it.
Secondly, why did using carriage return as the delimiter not work? As opposed to paragraph?
If you time to answer both, thanks, but mainly the first. I’ll check the FAQs or something, I’m sure it’s been asked a thousand times.
As discussed elsewhere on this site, using the paragraphs property of text is much easier than reading a file with a line ending delimiter. This is because your file could be a Mac, Unix, or DOS file each with different line ending delimiters. The paragraph property doesn’t care which delimiter, it just works. As to your other question, yes, the awake from nib handler would be a great place to load this popup initially. As to “length of” vs. “number of items of” there is not much difference except “length” is less characters to enter. I think the main problem with your code (at least as posted), was that in your repeat loop you set the current item to item i but didn’t specify item i of what.
on clicked theObject
�����delete every menu item of menu of popup button “myButton” of window “myWindow”
�����set file_path to (((path to desktop) as string) & “test.txt”) as file specification
�����set read_data to paragraphs of (read file_path)
�����repeat with i from 1 to (length of read_data)
����������set current_item to (item i of read_data) as string
����������make new menu item at end of menu of popup button “myButton” of window “myWindow” with properties {title:current_item, enabled:true}
�����end repeat
end clicked
Something like this is almost exactly what I need. I am trying to do something similar in that I want to build the popup button’s menu on the fly but instead of getting the menu item names from a text file I got a folder in the resources folder called “Alert Sounds” and I want my app to look in there in “on awake from nib” and build the menu from the names of all the files found in that folder.
Point being so that users of my app can add there own alert sounds by control clicking on the app and just dropping the sound files in the Alert Sounds folder which would then be available to select in the preferences window the next time the app is launched.
I’ve tried…
set AlertSoundsFolder to ((resource path of main bundle) & “/Alert Sounds”) as string
set soundNames to (list folder AlertSoundsFolder)
but with that I get a “Bad Name for file error (-37)”
So in the above code the lines that build the menu are good but I need to know what to replace the lines that read the file with.
set soundNames to (list folder ((path to me) as string) & “Contents:Resources:AlertSounds:”)
I think it was just the lack of the trailing “:” that was giving me a problem. Seems a reference to a path with out one is automatically treated as a file and with it treated as a folder.