Applescript dictionaries....

Hi all,

How do you use dictionaries (key/value pairs) in Applescript?

Thanks and God Bless,

Ben.

not sure I understand the question do you want know how to use the dictionary or are you having trouble trying to make key value pairs ?

gave you show use what your script is doing or what your trying to accomplish ?

thanks
mm

In AppleScript a dictionary is the vocabulary of the terms and commands the programmer can use.

If you mean the dictionary of a xml or property list file,
you can access it both with the defaults command (for plist files) of the shell or
with the Property List Suite or XML Suite of System Events.
Consider that the System Events suites differ in Tiger and Leopard

Strangely enough, I just posted an example of using System Events to read a plist file, on the macosxhints fora ( :slight_smile: forums):

http://forums.macosxhints.com/showthread.php?t=88497 Check Post #7

Are you looking for records?

Hi guys,

I know this has been discussed in a previous post, but I’m trying to achieve localization through dictionaries to minimize my code. I know there are other methods, however, this method is necessary as per the software lead.

Here is what I was thinking:

set localeEnglish to {locale:en, display:"This will uninstall your Software"}

I would repeat this line of code for all the locales to minimize the following method:

on displayEnglish()
	display dialog "This will uninstall your Software. Do you want to continue?" buttons {"Yes", "No"} default button "Yes" with icon caution
	if button returned of result is "No" then error number -128
end displayEnglish

I want to replace the “display dialog” portion with the record I have placed, however, my first question is:

  1. How can I achieve this, yet still implement the buttons of the dialog box, and still return a result value to quit the program?

I would call the localization methods at the end of the program such as this:

Ignoring case
	set SystemLanguage to do shell script "defaults read .GlobalPreferences AppleLanguages | tr -d [:space:] | cut -c2-3"
	
	if SystemLanguage is equal to "en" then
		displayEnglish()
		CloseSoftware()
		Deleter()

I use “Ignoring Case” as I want this utility to work on case-sensitive Tiger/Leopard partitions as well.

  1. In my record (dictionary) element, I have set the locale:en tag, which I don’t use…should I use it here, instead of SystemLanguage is equal to “en”?

Basically, what’s the quickest, and most efficient way to get this all to work?

Thanks for your help and God Bless you guys! :slight_smile:

Ben.

I told you the most efficient and fastest (and most professionell) method, but you don’t want it. :confused:
I guess you need a bunch of if - then lines.


set SystemLanguage to do shell script "defaults read .GlobalPreferences AppleLanguages | tr -d [:space:] | cut -c2-3"

if SystemLanguage is equal to "en" then
	displayEnglish()
else if SystemLanguage is equal to "fr" then
	displayFrench()
else if SystemLanguage is equal to "jp" then
	displayJapanese()
else if SystemLanguage is equal to "de" then
	displayGerman()
end if
CloseSoftware()
Deleter()

You have to check the system language anyway with the do shell script line,
so the record key “locale” is actually not needed

or something like this:


property langAbbrev : {"en", "fr", "jp", "de"}
property displayLocale : {"This will.", "french text", "japansese text", "german text"}

set SystemLanguage to do shell script "defaults read .GlobalPreferences AppleLanguages | tr -d [:space:] | cut -c2-3"

repeat with i from 1 to count langAbbrev
	if SystemLanguage is item i of langAbbrev then
		set textToDisplay to item i of displayLocale
		exit repeat
	end if
end repeat
displayMessage(textToDisplay)
CloseSoftware()
Deleter()

H Stefan,

I can understand that you may be frustrated with my question, and while I do agree that the other way may be professional, I was adviced by the software lead to use this method. I like your second method…thanks a lot for you help. One question I have is the “textToDisplay” variable which is passed to the display method…I’m assuming that I use it for my display dialog, such as:

on Display(textToDisplay)
   display dialog textToDisplay buttons {"Yes", "No"} default button "Yes" with icon caution
   if button returned of result is "No" then error number -128
end Display

Thanks again! :smiley:

Exactly.

Consider, that the “Yes” and “No” buttons are not localized.

PS: I’m not frustrated at all.
As Apple gives everybody the tools to develop software for free,
unfortunately there are lots of middle-rate written programs.

Unlike books with a “bumpy” style, which won’t be sold very well,
you normally can’t see the code within the application :wink:

Thanks Stefan,

I’m assuming the “Yes” and “No” buttons will automatically localized with the OS language, is this correct? Also, with the “if/else” logic, I was able to put an “else” clause to default the language to English in case an unknown (unsupported) language is chosen by the user. With a property, I’m limited to my language selection; is there a way I can still keep my property settings and add a default to English if an unknown (unsupported) language is selected?

Thanks,

Ben.

No, it isn’t. The scripter must do the job.

Make an english preset. If no match is found, the script uses english


.
set SystemLanguage to do shell script "defaults read .GlobalPreferences AppleLanguages | tr -d [:space:] | cut -c2-3"

set textToDisplay to item 1 of displayLocale -- preset to english
repeat with i from 1 to count langAbbrev
.

Hey, thanks again, it worked! :slight_smile: I have a localization problem though…I’m trying to say “Удаление завершено”, which means “uninstall completed” in Russian, but the Applescript editor doesn’t recognize it in Tiger. Is there a pack that I can download for Tiger and Leopard, so that I can paste these localization strings in properly?

Tiger doesn’t recognize literal Unicode text. You have to use the raw data way

set a to «data utxt111122223333» as Unicode text

1111, 2222, 3333 represents a hex word (e.g. Ѫ = 046A)

Hmm…that’s strange, cuz other languages such as Norweigan, or Polish for example is read correctly. Only the Russian language is not read correctly. So, in order to get my string in Russian, I have to find out the raw data representation for that string? Is there a utility I could get for that purpose, as I have no idea on how to get that raw data representation for the Russian localization string that I want to use.

Thanks,

Ben.

You can retrieve the hex equivalent of Unicode characters with e.g.

¢ Character Palette (built in)
¢ Unicode Checker (freeware)
¢ PopChar (shareware)

Your system probably has MacRoman at the “default” AppleScript text encoding. Whatever the default encoding is, only text that can be written in that encoding may be used in the text of AppleScript code (including string literals). My guess is that the Russian characters you want are not representable in your system’s default encoding.

Try kai’s utxt generation script. Warning: I am pretty sure there is a bug in that code relating to UTF-16 surrogate pairs. If you do not need any code points above FFFF, then it will probably work well enough.

It looks very complicated for the short amount of time needed to localize one language, so I’ll probably just use English as a default for that particular language. Too bad there’s not an easier way to do this. Thanks for all your help guys and God bless you all! :slight_smile:

Ben.

I know I posted another post regarding this, but how do I create an icon for my script with no dependencies?