• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Converting enums to strings and v.v.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Converting enums to strings and v.v.


  • Subject: Re: Converting enums to strings and v.v.
  • From: Christopher Nebel <email@hidden>
  • Date: Wed, 24 Sep 2003 00:08:34 -0700

On Sep 23, 2003, at 7:47 PM, Steve Mills wrote:

Recently I was writing a script that needed to present various enums to the user as their English dialect strings, then convert the strings back to enums. An example would be the color space property of a color spec; 4-char code 'RGB ' should display as "RGB space", etc.

I tried the most simple thing I could think of, which was a coercion:

(color space of theColor) as string
-->"RGB space"

This worked! Later that day, it stopped working. The next day it didn't work for a while, then suddenly it did. I asked the Script Debugger list about it and Mark said that it should not work. But it did. Anybody have any clues? ... Strangely, the terms true and false seem to be able to coerce to strings all the time.

The enumerator-to-string coercion, which was added back in AppleScript 1.4, was one of those features that people had requested for years, and we thought we had a terribly clever solution for, but has turned out to be a fairly bad idea in practice.

The difficulty, and probably why it only works sometimes for you, is that it relies on the normal unparsing routines that AppleScript uses to display source. This means that the terminology for that enumeration must have been loaded. The coercion will not force this to happen, *and running scripts does not load application terminology.* Only compiling scripts does that. (AppleScript built-in terminology always gets loaded, which is why true and false always work.) Once the terminology is loaded, it stays loaded, so it will keep working, at least for that process.

The practical upshot of this is that you have to compile (or display) a script that talks to the relevant application for the coercion to actually work. Calling "run script" is the usual solution -- note that on Mac OS X, the "run script" must happen in the same process as the script trying to do the coercion. (In Mac OS 9, it could be any application, since the terminology store was shared between processes.)

This is not to mention the inherent problems with the coercion even if it did work, like the fact that by definition it gives you the English term, which is not very useful if you're trying to write something for non-English users. In fact, most people wanted this because it seemed like the obvious way to convert month and weekday enumerators to strings, but there are much better ways to solve that problem.


--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

References: 
 >Converting enums to strings and v.v. (From: Steve Mills <email@hidden>)

  • Prev by Date: Re: Posting Form Data with UAS or iCAB
  • Next by Date: Re: Cannot set package folder bit?
  • Previous by thread: Converting enums to strings and v.v.
  • Next by thread: Posting Form Data with UAS or iCAB
  • Index(es):
    • Date
    • Thread