How do you write a script to autofill from a database?

Hi,

I’ve searched around the MacScripter forum for an answer to this, and couldn’t find anything. Surely, this is a feature that people want to know how to accomplish.

How do you write a script to take information from a data source, and auto-fill the rest of the field in a form in an application?

Web browser URL bars have this feature, as do many applications. You type the first few letters, and the app fills out the rest of the field with a possible result.

This is called Clairvoyance, type ahead, and auto fill.

Any ideas?

Cheers,

Peter :slight_smile:

Greetings. From what I have found, there is no great way to make a text field auto-fill, for a few reasons. I won’t go into that now because there is an easy alternative with built-in auto-fill ability. You should use a combo box instead of a text field. Attributing values to it is slightly difficult if you haven’t used a combo box before, but it supports data sources and can handle many values natively, automatically, and without writing any extra code to perform the auto-fill feature.

First, I created a combo box named “autoFillCBox” in my project. YOU MUST remember to check the “Completes” checkbox in the combo box’s attributes or the auto-fill feature will not happen.

In my script I created a property named “scanFields”, a simple list of values I wanted to try to match in the auto-fill feature, and dropped it in the top of the script. You can also use a data source as you would for other objects like text views or outline views, depending on what suits your application best. I haven’t tried this yet, so don’t know what the exact verbage would be to accomplish this.

Then, I added the code which propagates the combo box with all of the values of the list when the application launches, attached to an ‘on activated’ event.

property scanFields : {"Alligator", "Allosaurus", "AliceInWonderland", "Atta-Boy", "AZisHOTinTheSummer"}

on activated theObject
	delete every combo box item of combo box "autoFillCBox" of window "mainWindow"
	repeat with theField in scanFields
		make new combo box item at the end of combo box items of combo box "autoFillCBox" of window "mainWindow" with data theField
	end repeat
end activated

The result will be a blank text field with a pulldown menu attached to it that contains a row for each of the records in the “scanFields” list. You can grab the disclosure triangle at any time to access the predefined list (if you so choose to make it available), or you can start typing and the field will fill in the rest. The most noticable difference between the combo box and the text field, is the presence of the disclosure triangle at the right of the text area. This might actually help you and your users, as they may want to pick from a selection of similarly spelled matches. Otherwise, you can set the number of items shown to one, essentially giving the user access to only the best/first match found. I looked around and didn’t find any mention of how to get rid of the disclosure triangle if you decided you didn’t want it there. You could get crazy :stuck_out_tongue: and use a custom image overlapping the end of the menu to essentially cover up the dTriangle. Just a suggestion, especially if you want it to look like a regular text field. Hopefully someone else will post a solution that can get rid of the triangle altogether.

Hope this helps…
j