View Full Version : COMBOBOX populate from text file (cocoa applescript)

Jun 28, 2012, 03:08 PM

I am new to all this - I have been using an applescript in my workflow for a long time now and am trying to bring it to the next level thanks to xCode.

But I got stuck rather quickly: I cannot find a way to populate a simple combo box with values taken from a text file located on my desktop.

The idea is the following:

- Open application
- At launch, the application looks for values of the ComboBox located in a text file on my desktop

This is step one of my program (next step will be to use an Xcel chart as reference: column A would be linked to the combo box, and then some text fields will be linked to the same)

I have looked online but wasn't able to find any reference for this.

Any help would be greatly appreciated.

Red Menace
Jun 28, 2012, 04:55 PM
The class you are looking for is NSComboBox (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSComboBox_Class/Reference/Reference.html).

First, connect your combo box to an outlet property in your script, for example myComboBox. Then you refer to that outlet when using the various class methods. There are a few ways to do this (see the AppleScriptObjC Release Notes (https://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html)), for example:

tell myComboBox
addItemWithObjectValue_("testing") -- single item
addItemsWithObjectValues_({"one", "two", "three"}) -- multiple items
end tell

You will usually see the following form though, since it is closer to the Objective-C examples:
myComboBox's addItemWithObjectValue_("testing")
myComboBox's addItemsWithObjectValues_({"one", "two", "three"})

Just about any Cocoa reference or example (by Apple or others) will be in Objective-C, so there is a little bit of a learning curve if you are not familiar with reading it (or the documentation). There are a few introductory tutorials out there such as MacScripter's AppleScriptObjC in Xcode (http://macscripter.net/viewtopic.php?id=30274), but the the main reference so far (by far) is Shane Stanley's AppleScriptObjC Explored (http://www.macosxautomation.com/applescript/apps/) ebook.

Jun 28, 2012, 07:38 PM
Thank you Red Menace for taking interest in my case - your answer provides some insight - but my goal is a step further: I am sorry for not being clear enough.

In my case I want my application to do the following:
"when MyApplication launches, myApplication must look for the file on my desktop entitled "myfile.txt", take the list of words (each word is a paragraph) from this text file and make it the values of myComboBox"

There must be a way to do that – I feel like I am turning around the answer.
I have been looking at S. Stanley excellent book for a week but no clear method in sight.
I am a patient man - but this is driving me nuts! Xcode is a whole different world. Fun ---- but frustrating at times :) I'm sure you've all been there!

Thanks for any further insight!

Red Menace
Jun 28, 2012, 09:52 PM
Most of the regular AppleScript code should work without any change, but you might be missing a few "current application" references, since that is where the various Cocoa classes are defined. In the following example I am populating the combo box in the applicationWillFinishLaunching_ handler since that is called right before the application is initialized.

property comboBoxFile : ((path to desktop folder) as text) & "myFile.txt"
property myComboBox : missing value -- combo box outlet connected via IB Editor

on applicationWillFinishLaunching_(aNotification)
set comboItems to {} -- this will be a list of combo box items
repeat with anItem in paragraphs of current application's (read file comboBoxFile)
set anItem to anItem as text
if anItem is not "" then set the end of comboItems to anItem
# other error checking as needed
end repeat
tell myComboBox
setStringValue_("default item") -- put something in the text field if desired
# any other comboBox settings
end tell
end applicationWillFinishLaunching_

Jun 29, 2012, 01:36 PM
It worked!
Thank you so much!

This will allow me to move on a little bit.

Thank you again.