Thursday, November 23, 2017

#1 2003-03-26 12:23:39 pm

T.J. Mahaffey
From:: Arkansas, USA
Registered: 2002-11-20
Posts: 238

Cooking with Data Types - Part 2: Coercion

The Corleone family, Scarface, the Sopranos. They all have one interesting skill in common: the power of persuasion, also known as 'coercion'. In this edition of unScripted, I'm going to expand on our previous lesson in data types and show you how to take one bit of data in its original form and make it an offer it can't refuse by coercing it to another type of data.

The fact that the word 'as' does all the dirty work for us in the dark underworld of coercion is as pleasingly powerful as a garlic-rich canolli. This one simple word lets an AppleScripter bend just about any poor value to his or her will.

Let's look at a couple of examples. In the illustration below, I've set up a variable containing a date. If you'll remember, the result pane is going to show us the result of the *last command* in our script. In this case, the result is a value of the date data type. We know this because of its structure: the word 'date' followed by a special string in a very specific format including month, day, year, etc.



Now, in the second figure, I'm coercing this myDate value into a string. Here's where you should be putting down that plate of spaghetti and picking up your note-taking pen. Notice that all I have done here is added a second line of code in which I created a second variable called myDate_string which now contains not an object that AppleScript recognizes as a date, but a series of plain characters which happen to *look* like a date. Your next question is, "But why, Don Corleone, would I want to do that?" To which I would reply, "Soon, my friend, this power of coercion could all be yours." To put things in more AppleScript terms, let's look at the example below.


In the third example above, I've decided that I need, for some arbitrary reason, to get the first character of today's date. Instinctively, I might achieve this as illustrated. However, if you recreate the code in your own script editor, you'll find that an error is thrown as in the next screenshot:


Here's where coercion comes in to save the proverbial day. We can't 'get character 1 of myDate' because a date object doesn't have such a thing. Liken this to looking for wings on a pig. Pigs just don't have wings. However, we can give this pig wings with the magic of coercion. In the next illustration, we're adding a mid-line of code in which myDate is coerced to a new variable called myDate_string. Now we can get 'character 1 of myDate_string' because myDate_string is, in fact, a string which has characters. The pig now has wings. (There is something silly about the fact that I seem to have jumped from The Godfather to The Island of Dr. Moreau, another movie starring Marlon Brando. However, if this pig analogy has worked for you, I won't dwell on the strangeness of it all. *grin*)


In this next example, we'll work with some numbers and see how coercion can be employed to further our metaphorical extortion. The figure below sets up our variable 'a' with a value of 32.


But maybe our ultimate need is to work with, say, stock quotes, so we need our numbers in decimal form. If you'll remember from your early days as a wee little gangster, a 'real number' is merely any number expressed as a decimal. In the next figure below, we've added a second line of code to coerce our original 'a' variable from its value of '32' (an integer) to its real number equivalent '32.0'. Same number, but now in 'real' form instead of 'integer' form. The next screenshot illustrates this.


To expand upon the use of numbers, it is important to note that AppleScript is also intelligent enough to understand some things for itself, without the help of coercion. In the next screenshot, I've set up two variables. The first is technically a string ("32") because it is enclosed in quotes, the second is an obvious integer (14). AppleScript's intelligence shows through when we try to add these two variables together. In this case, AS recognizes that our variable 'x' is a number without us having to coerce it to an integer first.


In addition, you should understand that some coercions just won't work. For instance, you can't coerce a real number like '44.3' to an integer because an integer is not expressed in decimal form and AppleScript doesn't know what to do with the leftover '.3' value. An attempt to do this in your code will result in an error similar to the one below:


Ok, in our last example, we'll take a ride out to the docks (trunk seat and cement boots optional) and show you how we might bully the list data type. In the illustration below, I have set up a list of words called someList. In our second line of code, I've set up another variable finalString based on someList this time coercing the original list to a string, resulting in all of someList's items expressed as a single string. Ah, the power of as.


That does it for this edition of unScripted. I hope these examples will help you understand the coercion racket and reveal to you how this very special AppleScript operator can help you move to the top of your very own AppleScript crime family.

Be well and I'll see you next month! T.J.


Filed under: data, Mahaffey, Types, Coercion



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)