Part of Nobumi Iyanaga's website. n-iyanag@ppp.bekkoame.ne.jp. 7/10/05.

logo picture

ASUnicodeDialogs

v. 0.7.1 Sun, Jul 10, 2005
Nobumi Iyanaga

Introduction

ASUnicodeDialogs is a special application that I made for my own needs. I thought perhaps some other people might have similar needs, so I wanted to distribute it as a freeware program -- without guarantee of any kind.

My needs are:
1. a "display dialog" command in which one can enter a Unicode string;

displaydialog_image picture

2. a "choose from list" command which can display items of Unicode string.

choosefromlist picture

As you see, "display dialog" and "choose from list" are commands of AppleScript. I use currently Mac OS 10.3.8, with US English as the primary language, but I need Japanese or Chinese or some Unicode characters in my daily work. With the native AppleScript "display dialog", it is possible to display Unicode strings, but it is impossible -- as far as I know -- to enter Unicode string as "answer". And the native AppleScript "choose from list" allows only Mac Roman string to be displayed. This is why I wrote this little application. This is the first AppleScript Studio application that I wrote.

NEW (July 10th 2005)

The day after the day I uploaded the first "public" version of this page, April 29th 2005, Apple released a new version of OS X, 10.4 (Tiger). The upgraded AppleScript bundled with Tiger included a new "display dialog" command which now accepts input of Unicode text, and a new "choose from list" command which can display Unicode text. So, for users of Tiger, my ASUnicodeDialogs becomes totally useless. -- On the other hand, I just realized that the application that I uploaded was defective, and could not launch on machines other than mine (it was built with ZeroLink checked...).

In these conditions, I may have decided to remove all this page, with all its software. But considering that there may be people who are still using pre-Tiger version of OS X, I thought that ASUnicodeDialogs may still be useful for them; and I might take this opportunity to give to the public a new example, and also an example of the use of the new "display dialog" and "choose from list" commands included in Tiger.

So, this new version (0.7.1 -- the first version was 0.7) includes:

  1. Newly built ASUnicodeDialogs (without the ZeroLink checked) which should work in pre-Tiger versions of OS X (say, from OS 10.2x to 10.3x) [there is no change in the application at all...] -- this replaces the non-working ASUnicodeDialogs which was on this page;
  2. A new example AppleScript application of ASUnicodeDialogs: it is similar to "UnicodeGrep.app that I wrote for Jedit X (see below), but it works in combination with "TextWrangler" rather than with Jedit X: it is named "UnicodeGrep_TW.app" (I discovered that TextWrangler is much faster and more robust than Jedit X, at least for opening files and selecting lines);
  3. Finally, an example application of the new "display dialog" and "choose from list" commands included in Tiger. It works just like "UnicodeGrep_TW.app" in combination with TextWrangler, but uses only the native AppleScript commands "display dialog" and "choose from list". It is named "Unix_grepAS_TW.app". It will work only on Tiger (and later) OS, but it is certainly more robust than my poor application...

Contents of the package:

ASUnicodeDialogs.app	
an application, that you will put anywhere you want, but preferably in your Applications/Utilities folder -- NEWLY built without ZeroLink checked....

ASUnicodeDialogs_handlers.scpt		
an AppleScript script file containing two AppleScript handlers:
	- ASUnicodeDisplayDialog
and
	- ASUnicodeChooseFromList

UnicodeGrep.app	
an AppleScript application which uses these two handlers. This is an example of the use of these "new commands".

And NEW:
  • UnicodeGrep_TW.app	
    same as UnicodeGrep.app, but works in combination with TextWrangler (see above)
  • Unix_grepAS_TW.app	
    same as UnicodeGrep_TW.app, but doesn't need ASUnicodeDialogs.app; it works only on Tiger and later OS (see above).

How to use:

ASUnicodeDialogs is almost a "faceless" application: when you launch it on the Finder, you will see nothing. It must be used as a "helper application", from within AppleScript scripts.

The best way to understand how it works is to try the example script included in ASUnicodeDialogs_handlers.scpt. Open this script in Script Editor; the example script is commented out with "(*" and "*)". Please remove these signs. Then compile it (press the "Enter" key) and try to run the script (press "Command + R"):

If it is the first time you run the script, you will see an application choosing dialog asking to locate ASUnicodeDialogs. You will find it in the list; please select it, and press "OK" button. The application ASUnicodeDialogs will launch and becomes active.

Then you will see a dialog asking your name: you will enter your name, or any other characters (perhaps Unicode characters or Japanese or Chinese characters...); you will press the "OK" button (or simply, press the "Return" key);

Then you will see a "list choosing dialog", with three items; the second and the third items are selected by default. Please press the "OK" button.

Now, the front application switches to Script Editor, and you will see the result in the Result window (for example): {{"your_name", "OK", 0}, {{2, 3}, "OK"}}.

After this experiment, please examine the comments in the script file ASUnicodeDialogs_handlers.scpt:

These comments list and explain the different parameters of the two handlers:
1. ASUnicodeDisplayDialog(the_prompt, default_answer, the_buttons, default_button, with_icon, giving_up_after, if_quit)

-- prompt: a string
-- default_answer: a string (required)
-- the_buttons: a list of three strings; or an empty list ({})
-- default_button: an integer, 1, 2 or 3; or 0 ("buttons" must be given)
-- the parameter "with_icon" works only for "0"; for "1" and "2", AppleScript Studio's "display dialog" displays only a generic icon for applications.
-- giving_up_after: an integer, 1 or bigger; or 0
--if_quit: 0 or 1

-- result: a list of three items: {user's_answer, button_pressed, gave_up_or_not}

So, ASUnicodeDisplayDialog takes seven parameters. Except the last one, they are almost the same as the parameters of the native AppleScript display dialog, which are:

	display dialog  plain text  -- the text to display in dialog box
	[default answer  plain text]  -- the default editable text	
	[buttons  a list of plain text]  -- a list of up to three button names
	[default button  number or string]  -- the name or number of the default button
	[with icon  number or string]  -- the name or ID of the icon to display...
	[with icon  stop/note/caution]  -- ...or one of these system icons
	[giving up after  integer]  -- number of seconds to wait before automatically dismissing dialog
	Result:   dialog reply  -- a record containing the button clicked and text entered (if any)

There are however some differences. The most important difference is that there is NO optional parameter for ASUnicodeDisplayDialog: all the seven parameters are always required.

Another difference is that ASUnicodeDisplayDialog displays a dialog which prompts always an answer of the user. Since the native AppleScript display dialog is already able to display Unicode characters, the only "raison d'être" of ASUnicodeDisplayDialog is that it accepts answers in Unicode characters.

Other than that, you will notice that the parameter "with_icon" accepts only 0 [zero] or an empty string ("") -- the default being "". If you set it to 0, it will display the icon for "STOP"; but other parameters (1 for "NOTE", and 2 for "CAUTION") don't work in AppleScript Studio's display dialog.

If you set the parameter "default_buton" which must be an integer, 1, 2 or 3, you MUST set the name of the three buttons with the parameter "the_buttons". 1 will highlight the left button; 2 will highlight the middle button and 3 will highlight the right button.

The parameter "the_buttons" must be either an empty list ({}) or a list of three short strings, for example {"Left", "Center", "Right"}.

The parameter "giving_up_after" accepts an integer, 0 or 1 or a bigger number.

If you set the parameter "if_quit" to 1, the application will quit after having returned the result.

The result will be a list of three items: the user's answer, the name of the button pressed, and gave up or not (1 or 0).

Here is an example:

set the_res to ASUnicodeDisplayDialog("Show me your name", "", {}, 0, "", 0, 0)


2. ASUnicodeChooseFromList(choice_list, prompt_string, default_items, OKButton_name, cancelButton_name, multiSelection_allowed, emptySelection_allowed,return_string, if_quit)

-- choice_list: a list of items or a list with one item which is a file path in the form "file://" plus POSIX path
	-- the file must be a text file in UTF-8 with line endings in cr or lf
	-- you must know the number of lines in the file...
-- prompt_string: a short string of less than 50 characters
--default_items: a list of integer(s) corresponding to the number(s) of items to be selected by default
-- OKButton_name: a short string or ""
-- cancelButton_name: a short string or ""
-- multiSelection_allowed: 0 or 1
-- emptySelection_allowed: 0 or 1
--return_string: 0 or 1 (default is 0) [if it is 1, the result will be returned as the selected row(s) string]
-- if_quit: 0 or 1

-- result: a list of two items: {selected_row(s)_number(s)_or_string(s), button_pressed}

As in the case of ASUnicodeDisplayDialog, the parameters are similar to those of the native AppleScript "choose from list":

	choose from list  a list of plain text  -- a list of strings to display 
		(an empty list if no selection)
	[with prompt  plain text]  -- the prompt to appear at the top of the list selection dialog
	[default items  a list of plain text]  -- list of strings to initially select
	[OK button name  plain text]  -- the name of the OK button
	[cancel button name  plain text]  -- the name of the Cancel button
	[multiple selections allowed  boolean]  -- Allow multiple items to be selected?
	[empty selection allowed  boolean]  -- Can the user make no selection and then choose OK?
	Result:   a list of plain text  -- the list of strings chosen

As in the case of ASUnicodeDisplayDialog, all the nine parameters are required always. There are some differences and improvements compared to the native AppleScript "choose from list".

- choice_list: this can be either a list of strings (Unicode strings) or a list of only one item, beginning with "file://" plus a POSIX file path of a text file (it must be a UTF-8 file with lf or cr as line endings). The latter option may be useful if you want to use a temporary file to store the list to be displayed. If you use this option with the parameter return_string set to 1, you can get the result as the chosen string (or a list of chosen strings); if the parameter return_string is set to 0, then the result will be the number of the line that has been chosen (or a list of numbers of lines chosen); in that case, you should know the number of lines in the file.

- prompt_string: a short string of less than 50 characters to be displayed at the top of the list.

- default_items: a list of integer(s) corresponding to the item number(s) of the item(s) to be selected initially, or an empty list ({}). If there are more than one item to be selected, the parameter "multiSelection_allowed" must be set to 1.

- OKButton_name: a short string to be displayed as the label of the OK button. If it is an empty string (""), the OK button will be labeled "OK".

- cancelButton_name: short string to be displayed as the label of the Cancel button. If it is an empty string (""), the Cancel button will be labeled "Cancel".

- multiSelection_allowed: 0 or 1, the default being 0. If you set several items to be selected initially with the parameter "default_items", this parameter must be set to 1.

- emptySelection_allowed: 0 or 1, the default being 0 (i.e. the empty selection is not allowed). - return_string: 0 or 1, the default being 0. If this parameter is set to 0, the return value (the result) will be a list of the chosen item(s) number(s), and the pressed button name, for example {1, "OK"}, or {{2, 3}, "OK"}, or {"", "Cancel"}. If it is set to 1, the first item of the result list will be a string, or a list of strings, of the chosen item(s). - if_quit: 0 or 1, the default being 0. If this parameter is set to 0, ASUnicodeDialogs will continue to run; if it is et to 1, it will quit after the execution of the script.

Here is an example:

set choose_from_list_res to ASUnicodeChooseFromList({"file:\///Volumes/NIVol3/cbeta/normal/T01/0004_001.txt"}, "Choose from these items...:", {2, 3}, "", "", 1, 0, 0, 0)

How all this works

What is above should be sufficient to use this application. If you are interested to know how this works, here is some more information.

To see visually how it works, you will launch ASUnicodeDialogs, and open a new script in Script Editor, and run the following script:

	tell application "ASUnicodeDialogs"
		activate
		set visible of window "win1" to true
	end tell

When you run this script, you will see a new window in ASUnicodeDialogs; it is divided into two vertically, the left side entitled "display dialog" and the right side entitled "choose from list". In each side, there are a number of text fields, labeled "Prompt", "default Answer", etc., and below these text fields, there is a button, labeled, the left side one, "Display Dialog", and the right side one, "Choose from list". Each side is divided also horizontally: on the upper side, you will find the text fields for different parameters sent to the application; and on the lower side, you will find the text fields which will display the results.

win1_image picture

What you see in the text fields when the window appears initially are the default values of the parameters for the two commands. Try to press the two buttons, one after the other, to see how the "display dialog" window and the list window will be displayed.

You can try to enter different values in the parameter text fields, and press the buttons, and you will see the results in the result fields. To try choose from list, you will first change the value of the text field "Delimiter char:" from "return" to a comma (","), and will enter your list in the text field "Item List", delimited by a comma (on this point, see below). All the changes that you will make will not be saved, so you can play with different values; when you will quit the application once, and relaunch it, the window will not be displayed, and all will work normally.

Now, with a new script in Script Editor, you can try to run, for example, the following script:

	tell application "ASUnicodeDialogs"
		activate
		tell window "win1"
			set the contents of text field "dialogPrompt" to "What is your name?"
		end tell
	end tell

There is also the command:

	tell application "ASUnicodeDialogs"
		activate
		tell window "win1"
			tell button "displayDialogButton" to perform action
		end tell
	end tell

which works as if the button "Display Dialog" were pressed.

Now, the first three lines of the script "ASUnicodeDialogs_handlers.scpt", which are:

	set app_path to (path to application "ASUnicodeDialogs" as string)
	set interface_script_path to app_path & 
		"Contents:Resources:ASUnicodeDialogs_interface.scpt"

set ASUnicodeDialog to load script file interface_script_path

mean that there is an AppleScript script in the Resources folder, inside the application bundle of ASUnicodeDialogs, named "ASUnicodeDialogs_interface.scpt" which contains in fact the two handlers:

	ASUnicodeDisplayDialog

and
	ASUnicodeChooseFromList.

Opening the application bundle with a "Control click" on the Finder, you can examine this script. You will find there all the AppleScript names for the text fields of the window "win1" -- except the text field labeled "Delimiter Char:" in the right side of the window. This "parameter" is the only one which is not controled from the "interface" script. This "parameter" is here mainly for testing the application: it is impossible to enter the return character (or the tab character) in text fields, so we cannot enter the return character in the field labeled "Item List:". To test the list table, we can enter in the text field "Delimiter char:" for example a comma (,), and delimit items in the text field "Item List:" with comma(s). You will be able to change the value of this "parameter" using the following script:

	tell application "ASUnicodeDialogs"
		activate
		tell window "win1"
			set the contents of text field "delimiterChar" to your_delimiter_char
		end tell
	end tell

But in fact, as we send normally directly a list of strings to the application (or a file path using the format "file://...."), changing the value of the delimiter character does not make any sense.

Here is the table of names of different objects in the window "win1":

Prompt dialogPrompt
Default Anser defaultAnswer
Button 1 dialogButton1
Button 2 dialogButton2
Button3 dialogButton3
With Icon dialogIcon
Default Button dialogDefaultButton
Giving up... givingUp
Display Dialog displayDialogButton
Text Returned dialogTextReturned
Button Returned dialogButtonReturned
Gave up gaveUpResult
Prompt listPrompt
Item List itemList (*)
Default Items listDefaultItems
OK Button Name listOKButtonName
Cancel Button Name listCancelButtonName
MultiSelection Allowed multiSelection
EmptySelection Allowed emptySelection
Delimiter Char delimiterChar (**)
Return string returnListString
Choose from list chooseFromList
Returned List Items returnedListItems
Buttonr returned listButtonReturned

(*) The text field "itemList" is used when we send a file path to the application; otherwise, it is not used from within scripts.
(**) The text field "delimiterChar" is never used from within scripts.

The example application UnicodeGrep.app

The use of UnicodeGrep is very simple. It supposes that you have Jedit X in your local disk(s). If you don't have it, you can download a demo version from:
<http://www.artman21.net/product/JeditX/>
(or, in English:
<http://www.artman21.net/product/JeditX/index_E.html>
).

You will launch UnicodeGrep; a dialog will ask you to enter the grep pattern that you want to use; you can enter there any Unicode string. Another dialog will ask you to locate a folder in which you would like to perform the grep search. Locate a folder full of UTF-8 text files, with the extension ".txt". If no result is returned, the script stops with the message: "Couldn't find any occurrence..."; otherwise, thesearch result will be displayed in a list; choose one of the rows, and press "OK". Jedit X will launch, and will open the chosen file, and select the word that was found. Please note that Jedit X may be VERY slow when the file is big; sometimes, it may perhaps crash...

Aknowledgment

The interface and the script for the command "ASUnicodeDisplayDialog" is taken almost entirely from the sample AppleScript Studio project named "Display Dialog" that comes with the Developer folder.

I have been helped by various kind people of the AppleScript Studio mailing list (<http://lists.apple.com/mailman/listinfo/applescript-studio>). I am most grateful especially to Philip Buckley and Matt Neuburg.

Download

Download the latest version of the package from here (349K to download).

Have fun!


Go to Research tools Home Page
Go to NI Home Page


Mail to Nobumi Iyanaga


frontierlogo picture

This page was last built with Frontier on a Macintosh on Sun, Jul 10, 2005 at 11:42:59 PM. Thanks for checking it out! Nobumi Iyanaga