Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Add custom units to the Unit Converter widget System 10.4
Tiger only hintHere's how to add a new unit to the Unit Conversion widget. In our example, we will add the unit "Cup (US)" to the "Volume" section of conversions. From the command line, do this:
cd /Library/Widgets/Unit Converter.wdgt
Next, make a backup copy of the Conversions.js file:
sudo cp Conversions.js Conversions_backup.js
Now edit the Conversions.js file with your favorite editor (I use vim launched via sudo).

Inside the file, search for the Volume array declaration. It looks like this:

var Volume = [
    {name:'Cubic Feet',         toBase:linearForm(28.316846592),        fromBase:invLinForm(28.316846592)},
    {name:'Gallon (Imperial)',  toBase:linearForm(4.54609),             fromBase:invLinForm(4.54609)},
    {name:'Gallon (US)',        toBase:linearForm(3.785411784),         fromBase:invLinForm(3.785411784)},
    {name:'Quart (US)',         toBase:linearForm(0.94635294600000),    fromBase:invLinForm(0.94635294600000)},
    {name:'Pint (US)',          toBase:invLinForm(2.11337641886519),    fromBase:linearForm(2.11337641886519)},
    {name:'Fluid Ounce (US)',   toBase:invLinForm(33.81402270184300),   fromBase:linearForm(33.81402270184300)},
    {name:'Dram (US)',          toBase:invLinForm(270.51218161474401),  fromBase:linearForm(270.51218161474401)},
    {name:'Cubic Meter',        toBase:linearForm(1000.0),              fromBase:invLinForm(1000.0)},
    {name:'Liter',              toBase:linearForm(1.0),                 fromBase:invLinForm(1.0)}
];

Add your own custom entry for the Cup unit. The format is simple. The first value is the name, the second value is the conversion from the new unit to the base unit (in the case of Volume, the base unit is Liter, the last entry in the list). Use the invLinForm option for conversion through multiplication. The last value is the reverse conversion. If you used invLinForm for the second, you would use linearForm for the third, and vice versa.

In our case, since the Cup is half the size of a Pint, we can double the conversion factor of Pint:

    {name:'Cup (US)',          toBase:invLinForm(4.22675283773038),    fromBase:linearForm(4.22675283773038)},

Save the file. Relaunch the unit converter widget and enjoy your custom unit!

Some things to note:

  • The actual Conversions.js file uses tabs, but the text above doesn't.
  • If you are adding a unit near the end of the array, make sure to remember to get the line terminating commas correct; all lines should have a terminating comma except for the last one.
  • If you want to add unicode characters, use the standard unicode escape uHHHH inside of the string. This will be compatible with whatever editor you use.
  • If you really want to be thorough, you need to add translations to all of the *.lproj/localizedStrings.js files. This is left as an exercise for the user.
[robg adds: I haven't tested this one.]
    •    
  • Currently 1.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[11,992 views]  

10.4: Add custom units to the Unit Converter widget | 8 comments | Create New Account
Click here to return to the '10.4: Add custom units to the Unit Converter widget' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Find more conversion values with Google
Authored by: renderhead on May 25, '05 11:57:30AM
Google is an invaluable tool for finding more conversion values to add to your table. To find the value for "Tablespoon (US)", for example, go to Google.com and type:

1 liter in tablespoons

and submit. You'll get the handy results:

1 liter = 67.6280451 US tablespoons

which you can use as your invLinForm() value. It works for just about any units you can think of. For fun, figure out and add a conversion to and from "Stones" in the "Weight" category!

[ Reply to This | # ]

Find more conversion values with Google
Authored by: PeteVerdon on May 25, '05 03:13:45PM
Alternatively, use the command-line "units" program, which has been around for donkey's years and is included in Panther and Tiger (and probably earlier).

[pete@Al pete]$ units
500 units, 54 prefixes
You have: 20 litres
You want: pints
        * 42.267528
        / 0.023658824
You have: 2 parsecs
You want: miles
        * 3.8347023e+13
        / 2.6077643e-14
You have: ^D
[pete@Al pete]$
Pete

[ Reply to This | # ]
Find more conversion values with Google
Authored by: dubmaster on May 27, '05 04:18:39PM

yeah, only hick with "units" is that it doesn't do temperature. No fahrenheit to celsius or vice versa.


---
-- Dub



[ Reply to This | # ]
Find more conversion values with Google
Authored by: PeteVerdon on May 28, '05 04:03:36AM

I believe at least some versions of it now do. I don't know if the one that comes with OS X does.

Pete



[ Reply to This | # ]
Find more conversion values with Google
Authored by: ben42 on Jun 12, '05 11:30:21AM

I tried this method with tablespoons and teaspoons, and it worked great, except that when I converted 1 pint to teaspoons I got 95.9999 instead of 96. Not enough decimal places in the Google definition (the units program in Terminal has even less). So I looked around and found another site with one more decimal place: http://www.onlineconversion.com/volume.htm. That one did the trick.



[ Reply to This | # ]
10.4: Add custom units to the Unit Converter widget
Authored by: boredzo on May 25, '05 03:03:54PM
shouldn't the escape be \uHHHH?

[ Reply to This | # ]
10.4: Add custom units to the Unit Converter widget
Authored by: boredzo on May 25, '05 03:08:34PM
ahh... the importance of reading the red text.
\uHHHH
is the correct escape.

[ Reply to This | # ]
Find more conversion values with Google
Authored by: blueaudio on Jan 24, '07 09:06:18AM
This link will let you convert temperatures for lots of types, including Fahrenheit, Celsius, kelvin, Newton, Delisle, Rankine, Reaumur, and Romer. Google conversion doesn't seem to handle temperatures yet.

[ Reply to This | # ]