A constant is a reserved word with a predefined value. AppleScript has defined a few constants to help us be more effective in our coding endeavors. We brushed on some of those when we discussed the date class. For example, we learned that the days constant has a value of 86400 (the number of seconds in day). I found very little information concerning constants aside from the AppleScript Language Guide and even there the info is sparse. Let see how we can use them in our daily tasks:
Group |
Contants |
Usage |
Arithmetics |
- pi
- days
- hours
- minutes
- seconds
- weeks
| Some of you might remember the formulas needed to calculate circle's properties:
set radius to 13 set Circle_Circumference to 2 * pi * radius —> 81.681408993335
set Circle_Area to pi * (radius ^ 2) / 2 —> 265.464579228338
set Circle_Volume to 4 / 3 * pi * (radius ^ 3) —> 9202.7720799157 As I mentioned when we talked about the date class, AppleScript included some constants to help us do arithmetic calculation with dates:
set Today to current date —> date "Friday, June 15, 2007 9:32:43 AM"
set Tomorrow to Today + 24 * hours —> date "Saturday, June 16, 2007 9:32:43 AM"
set NextWeek to Today + 7 * days —> date "Friday, June 22, 2007 9:32:43 AM"
|
Boolean | | Noting much to say here other than to state that the line between a Boolean variable and a Boolean constant can be quite blurry. The way I see it is that an expression can have a Boolean value (ex.: 3>4 -> false) but the words true and false are themselves constants. In any case since variable and constants are use in a similar manner this should not be a case for concerns. |
Considering / Ignoring |
- application responses
- case
- diacriticals
- expansion
- hyphens
- punctuation
- white space
| application responses: [Default: Considering] When you wish you script to carry on with the next line of code before a targeted application has completed the last command(s) you have sent to it you have to issue an ignoring application responses call. Note: This will also silence any error such application might returns.
set Final_Destination to path to desktop from user domain tell application "Finder" set QFolder to "Which folder would you like to copy to your desktop?" set ThisFolder to choose folder with prompt QFolder ignoring application responses duplicate folder ThisFolder to folder Final_Destination with replacing end ignoring beep display dialog "The copy process should now be running..." end tell
The following “considerations” all relate to strings. Note the default behavior for each consideration; this will save you a lot of grief in the future.
case: [Default: Ignoring] Should Applescript considers the difference between Uppercase and Lowercase characters.
set aString to "A Little House On The Prairie" set bString to "A little house on the prairie"
set CompareCase_Default to aString = bString —> true
considering case set CompareCase to aString = bString —> false end considering diacriticals: [Default: Considering] Should Applescript ignore the difference between accented and non-accented characters.
set aString to "Déjà vu!" set bString to "Deja vu!"
set CompareDiacriticals_Default to aString = bString —> false
ignoring diacriticals set CompareDiacriticals to aString = bString —> true end ignoring
expansion: [Default: Considering] Should Applescript ignore the difference between expanded (œ, æ, etc.) and non-expanded (oe, ae, etc.) characters.
set aString to "The oedipus complex" set bString to "The œdipus complex"
set CompareExpansion_Default to aString = bString —> false
ignoring expansion set CompareExpansion to aString = bString —> true end ignoring
hyphens: [Default: Considering] Should Applescript ignore the presence of hyphens “-“ in the compared strings.
set aString to "A large semi-trailer" set bString to "A large semitrailer"
set CompareHyphens_Default to aString = bString —> false
ignoring hyphens set CompareHyphens to aString = bString —> true end ignoring
punctuation: [Default: Considering] Should Applescript ignore the presence of punctuation marks (!, ^, &, %, etc.) in the compared strings.
set aString to "Happy New Year" set bString to "Happy New Year!"
set ComparePunctuation_Default to aString = bString —> false
ignoring punctuation set ComparePunctuation to aString = bString —> true end ignoring
white space: [Default: Considering] Should Applescript ignore the presence of spaces, tabs and return characters in the compared strings.
set aString to "A Little House On The Prairie" set bString to "A little house on the prairie"
set CompareWhiteSpace_Default to aString = bString —> false
ignoring white space set CompareWhiteSpace to aString = bString —> true end ignoring
|
Conversion | Area
- square feet
- square kilometers
- square meters
- square miles
- square yards
Length
- centimeters
- feet
- inches
- kilometers
- meters
- miles
- yards
Liquid volume
Temperature
- degrees Celcius
- degrees Farenheit
- degrees Kelvin
Volume
- cubic centimeters
- cubic feet
- cubic inches
- cubic meters
- cubic yards
Weight
- grams
- kilograms
- ounces
- pounds
| To use these conversion constants you have to ensure that your keep within the same sub-group. For instance you cannot convert a value in a length unit into a volume unit.
set SI_Area to 10 as square meters set IMP_Area to SI_Area as square yards —> square yards 11.959900463011
set SI_Length to 10 as meters set IMP_Length to SI_Length as yards —> yards 10.936132983377
set SI_Liquid to 10 as liters set IMP_Liquid to SI_Liquid as gallons —> gallons 2.641720372842
set SI_Temp to 10 as degrees Celsius set IMP_Temp to SI_Temp as degrees Fahrenheit —> degrees Fahrenheit 50.0
set SI_Volume to 10 as cubic centimeters set IMP_Volume to SI_Volume as cubic inches —> cubic inches 0.610237440947
set SI_Weight to 10 as kilograms set IMP_Weight to SI_Weight as pounds —> pounds 22.04622476038
|
Date / Time |
- January - December / Jan - Dec
- Monday - Sunday / Mon - Sun
| These are pretty much self-explanatory
set aDate to current date —> date "Friday, June 15, 2007 9:32:43 AM"
set TheDay to weekday of aDate —> Friday
|
Miscellaneous |
- anything
- current application
- it
- missing value, null
- my, me (of)
- result
- text item delimiters
- version
| anything: You will seldom see this one used anywhere (I haven't yet). The main purpose of this constant, I guess, is to initialize a variable with this value and later on check to see if it has changed in any way and act on this information. I am unclear as to the exact value of anything, in my test it shows up as “***” which could also be a 'visual” replacement for something else.
set CheckValue to anything repeat with i from 10 to 1 by -1 try set x to 2 / i on error set CheckValue to "error" end try end repeat if not CheckValue is anything then beep end if current application: Here I must disagree with the AppleScript language Guide (in case you were following along). the reason the example stated there works is because of the use of the constant "my". All my testing of this resulted in same dead end: I can't seem to find a useful thing to do with it.
it: For usage within a tell. end tell statement. The it constant will be a reference to object targeted by the tell… end tell statement.
tell application "QuarkXPress Passport" tell document 1 try set WordRefs to object reference of every text of every story where it is "The" end try end tell end tell
missing value, null: These are “seat filler” for properties that has no value. For example: In QuarkXPress, a text box may have a previous text box or a next text box associated to it but that is not always the case.
tell application "QuarkXPress Passport" tell document 1 set PreviousBox to previous text box of text box 1 —> null end tell end tell
my, me (of): Intended to be used with a tell… end tell statement, his constant refers to the script itself. Note: Remember that the use of this constant may speed up your scripts somewhat as AppleScript will bypass its normal “search order” for the terms used in your script to go straight to your handler.
my Handler1() —> {{beep}}
Handler2() of me —> {{beep}}, {{beep}}
on Handler1() beep end Handler1
on Handler2() beep 2 end Handler2 result: This constant will take whatever value the last issued command returns
set x to 3 + 3 get result —> 6 text item delimiters: Although this is said to be an AppleScript property, I consider this more like a special constant hence my listing it here. This special constant contains a list of strings used for identifying text items. As of yet, AppleScript only considers the first element of that list. The main reason I do put this with the constant is that text item delimiters will keep the same value as you last set it and this until you either reset it to another value or quit out of your Script Editor and re-launch again. In case of application script, the TID will remain in effect as long as the app is running or if an internal procedure set it to another value. By default, TID are set to {“”} (an empty string). Note: TID's are always case sensitive
set aString to "A text is a text is a text!"
set oldDelims to AppleScript's text item delimiters set AppleScript's text item delimiters to {space} set WordList to every text item of aString —> {"A", "text", "is", "a", "text", "is", "a", "text!"}
set AppleScript's text item delimiters to {"text", "a"} set ItemList to every text item of aString —> {"A ", " is a ", " is a ", "!"}
set AppleScript's text item delimiters to oldDelims
version: This constant will return the current version of the targeted scriptable application. However, if no application is specified, the current version of AppleScript will be returned.
set AS_Version to version —> "1.10.7"
tell application "QuarkXPress Passport" set QXP_version to version —> "6.5" end tell
|
Save Options | | As it name state, you will use these constant when closing a document. At which time, if any transformation has been made to it you will want to either save the changes, close the document without saving or ask the user what to do.
tell application "Tex-Edit Plus" activate make new window at beginning set contents of window 1 to "test" close window 1 saving ask end tell
|
String | | Writing and combining strings can lead to many unwanted results. The main reason for this is you will find yourself building strings out of fixed text value and “floating” values coming from variables. To make things easier, AppleScript has made “visible” three invisible characters: return, space and tab. Using these in your script will greatly improve their readability.
set FirstName to "Michel" set LastName to "Lemieux" set MemberLevel to "Forum Admin"
set Member_Info to FirstName & space & LastName & tab & MemberLevel —> "Michel Lemieux Forum Admin"
|
Text Style |
- all caps
- all lowercase
- bold
- condensed
- expanded
- hidden
- italic
- outline
- plain
- shadow
- small caps
- strikethrough
- subscript
- superscript
- undrline
| Depending the application you will be targeting, you will sometime be able to get of set the style of some text. These are the constants for the styled text class you will find in those apps. Note: on styles and off styles work in pair, whatever is in the former should be removed form the later and vice-versa. Also, some "styling" are exclusive, for instance you cannot have plain and italic in the on styles list
tell application "QuarkXPress Passport" tell document 1 tell text box 1 set CharStyle to style of character 1 —> {class:text style info, on styles:{italic, shadow, superscript, superior, all caps}, off styles:{plain, bold, underline, outline, subscript, strikethrough, small caps, word underline}} end tell end tell end tell
|