Need help with AppleScript for languages and dictionaries

  • Thread starter Richard Ronnback
  • Start date
R

Richard Ronnback

I need to script changing the applied language to text in thousands of
rtf-files, using MS Word 2004, but I have run into two Apple Script problems,
that I am sure are very basic but I just don't get it.

Ideally I would like the user to be able to select language from a list and
then apply the chosen language to all text in the document. My two problems
with this are:

1) How do I via AppleScript retrieve a list of names of available
(installed) languages from the Word application itself? I could of course
hardwire the list, but would really prefer to build it dynamically from the
actual languages

2) If I a present a list of names (strings) to the user (via the choose from
list command), how can I get MS Word to accept the string returned as a valid
language, in other words, how do I go from a string to the enumeration for
the language ID that MS Word require?

Any help is much appreciated
 
P

Paul Berkowitz

I need to script changing the applied language to text in thousands of
rtf-files, using MS Word 2004, but I have run into two Apple Script problems,
that I am sure are very basic but I just don't get it.

Ideally I would like the user to be able to select language from a list and
then apply the chosen language to all text in the document. My two problems
with this are:

1) How do I via AppleScript retrieve a list of names of available
(installed) languages from the Word application itself? I could of course
hardwire the list, but would really prefer to build it dynamically from the
actual languages

2) If I a present a list of names (strings) to the user (via the choose from
list command), how can I get MS Word to accept the string returned as a valid
language, in other words, how do I go from a string to the enumeration for
the language ID that MS Word require?

Any help is much appreciated

Sorry, Richard, I sent you here instead of just answering on the AppleScript
list (which somehow I thought was the Entourage list!) so I'll answer here
as well as there. Someone else might have more to add:



You can use the long list of 'language ID' enumerations that are found in
various classes. Probably the most useful place is 'template', since you can
always get the 'attached template' of any document - usually that will be
Normal template. If you're starting off from a new document, you might want
to just assume the Normal template. As far as I know, there are no
differences as to which languages are available by AppleScript (or VBA) in
the various localized versions of Word. I might be wrong - I'll ask. But I
think that the only language IDs available by AppleScript are the ones you
see as enumerations on the Word AS dictionary. You can coerce each one 'as
string' to make your list, or - better - use the 'retrieve language' command
so you can present the list in the user's local language. I see where
'choose form list' still uses strings, not Unicode , even in Tiger (shame!).
so maybe that won't work, but

name local of (retrieve language (language ID of normal template))

gets me "English (US)" here. Using 'name' instead of 'name local' might be
a better idea to avoid the Unicode problem of 'choose from list'.

name of (retrieve language danish)
--> "Dansk"

name local of (retrieve language danish)
--> "Danish"

Use whichever you prefer. If the language is not installed (if that's
possible), you'd get an error. So to build your list - just in case some
languages really are not available locally - you could do something like:


tell application "Microsoft Word"
set languageList to {}
try
set end of languageList to name local of (retrieve language danish)
end try
try
set end of languageList to name local of (retrieve language german)
end try
-- etc. through every language ID

set chosenLanguage to item 1 of (choose from list languageList)

end tell


For your second question, I think it would be too risky to try to coerce the
string back to the language ID via 'run script', even using the 'name'
string rather than the 'name local'. Instead I think you just need to have a
separate list of every enumeration, and get the same list item as the
selected string is found to be from the languageList. A sort of lookup (you
could do it via a list of records too.) E.g.




tell application "Microsoft Word"
set languageList to {}
set languageIDList to {}
try
set end of languageList to name local of (retrieve language danish)
set end of languageIDList to danish -- will omit if previous line
errors
end try

try
set end of languageList to name local of (retrieve language german)
set end of languageIDList to german
end try
--etc, through every language ID

set chosenLanguage to item 1 of (choose from list languageList)
set theIndex to my CollectUniqueItemIndex(languageList, chosenLanguage)
set chosenLanguageID to item theIndex of languageIDList
end tell

to CollectUniqueItemIndex(theList, theItem)

set theIndex to 0
repeat with i from 1 to (count theList)
set aListMember to item i of theList
if aListMember = theItem then
set theIndex to i
exit repeat
end if
end repeat

return theIndex

end CollectUniqueItemIndex



--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
P

Paul Berkowitz

For your second question, I think it would be too risky to try to coerce
the string back to the language ID via 'run script', even using the 'name'
string rather than the 'name local'.


If you wanted to do it, with no risk but less attractive user interface,
you'd simply make up the list of available languages with the lower-case
enumerations 'as string':

tell application "Microsoft Word"
set languageList to {}
try
set end of languageList to danish as string
end try
try
set end of languageList to german as string
end try
--etc

set chosenLanguage to item 1 of (choose from list languageList)
set chosenLaguageID to run script chosenLanguage -- gets the enumeration
back
end tell

Note that if you somehow needed to get the enumerations 'as string' out of
Word tell block, say writing to a text file or something, you first need to
preface all such manipulations with:

run script "tell application \"Microsoft Word\"
end tell"

...one of those strange peculiarities of AppleScript. Otherwise the exported
constants appear as their «raw codes» instead of the English language
constants you expect.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top