vipercard > script reference

Overview | Commands | Syntax | Properties | Functions | Event Handlers | Compatibility

abs(x)

Absolute value.

Examples:



put abs(-0.5) into x
put abs(0.5) into x

atan(x)

Arctangent, in radians.

Examples:



put atan(0.5) into x


average()

Simple mean of numbers.

Supports any number of arguments.

You can also provide a comma-delimited string.

Examples:



put average(1,2,3) into x
put average("1,2,3") into x -- returns 2

charToNum(x)

From ascii-like number to a character. Note that characters in a field are displayed in Mac OS Roman encoding.

Note that the newline character is always \n (10).

Examples:



put charToNum(97) into x



clickH()

Get x coordinate of a recent mouse click. See clickLoc for more details.

Examples:



put clickh() into x


clickLoc()

Get the coordinates of a recent mouse click.

This isn't necessarily the most recent mouseClick, it is only updated

1) clicks before the script runs

2) when you check the mouseClick

3) when you simulate a click with click or drag

Examples:



wait until the mouseClic
answer "you clicked at" && the clickLoc


clickV()

Get y coordinate of a recent mouse click. See clickLoc for more details.

Examples:



put clickv() into x


commandKey()

Check if this modifier key is pressed.

Examples:



if the commandKey is down then
    answer "cmd key is down"
end if
-- same as
if the cmdKey is down then
    answer "cmd key is down"
end if


contains

Can you find a string within a string?

Examples:



if "abc" contains "b" then
    answer "yes"
end if

cos(x)

Examples:



put cos(0.5 * pi) into x

date()

Returns the current date.

Examples:



the date
date()
the abbrev date
the short date
the long date
the English date

exp(x)

e to the power of x.

Examples:



put exp(0.5) into x



exp2(x)

2 to the power of x.

Examples:



put exp2(0.5) into x

is a

Check the type of an expression.

Examples:



if x is a number then
    answer "a"
end if
if x is a integer then
    answer "b"
end if
if x is a logical then
    answer "c"
end if
if x is a point then
    answer "d"
end if
if x is a rect then
    answer "e"
end if

is in

Can you find a string within a string?

Examples:



if "b" is in "abc" then
    answer "yes"
end if

-- is within is the same,
if "b" is within "abc" then
    answer "yes"
end if


keyChar()

In an afterkeydown or afterkeyup handler, check the character. Is affected by shift.

Examples:



put keyChar() into x
on afterKeyDown
    if keyChar() is "i" and the shiftKey is up and \
            the optionKey is down and the commandKey is up then
        answer "you pressed option-i"
    else if keyChar() is "U" and the shiftKey is down and \
            the optionKey is up and the commandKey is up then
        answer "you pressed shift-u"
    end if
end afterKeyDown

keyRepeated()

In an afterkeydown handler, did this event come from the user holding the key down?

Examples:



if not keyRepeated() then
    add 1 to uniqueKeyPresses
end if

length(s)

Returns the length of a string, in characters.

Examples:



answer length("abc") -- displays 3
answer the length of "" -- displays 0
answer the length of "abc" -- displays 3

ln(x)

Examples:



put ln(0.5) into x





log2(x)

Base-2 logarithm.

Examples:



put log2(0.5) into x

max(...)

Supports any number of arguments. You can also provide a comma-delimited string.

Examples:



put max(1,2,3) into x
put max("45,49,40") into x -- returns 49

me

Refers to the object that owns the current script.

Similar to, but distinct from 'target'.

Examples:



put the id of me into theTarget

set the width of me to 100

min()

Supports any number of arguments. You can also provide a comma-delimited string.

Examples:



put min(1,2,3) into x
put min("45,49,40") into x -- returns 40

mouse()

Is the mouse button currently down. Returns one of the constants down or up.

Examples:



if the mouse is down then
    answer "mouse button is clicked"
end if

mouseClick()

Was the mouse recently clicked? Doesn't include the click that created the message, it needs to be later.

When you check the mouseClick, it will only return true once before resetting back to false. See also: clickloc, clickh, clickv.

Examples:



wait until the mouseClick
answer the clickloc


mouseH()

The x coordinate of the current mouse location.

Examples:



put mouseH() into x


mouseLoc()

The coordinates of the current mouse location.

Examples:



put mouseLoc() into x


mouseV()

The y coordinate of the current mouse location.

Examples:



put mouseV() into x


number

Count parts of text

answer the number of chars in "abc"
answer the number of items in "a,b,c"
answer the number of lines in myList

Count current objects

answer the number of cards in this stack
answer the number of buttons in this card
answer the number of fields in this card
repeat with x = 1 to the number of cards in this stack
    set the name of card x of this stack to "c" & x
end repeat

Get the number of an object.

answer the number of this card
put the number of cd fld id 1234 into x
put "abc" into cd fld x

numberToStr(x)

Convert number to string.

Examples:



put numberToStr(0.5) into x

numToChar(x)

From a character to an ascii-like number. Note that characters in a field are displayed in Mac OS Roman encoding.

Note that the newline character is always \n (10).

Examples:



put numToChar("a") into x


objectById(id)

Gets the full id from the numeric id.

Examples:



-- assuming there is a cd btn id 1234
put objectById(1234) into x
-- x is now "card button id 1234".
-- you can now do this
set the topleft of x to 100, 200

offset(needle, haystack)

Search for a string within a string, and return the position where found. If not found, returns 0. (one-based indexing).

Examples:



put offset("b", "abc") into x
-- x is now 2.


optionKey()

Check if this modifier key is pressed.

Examples:



if the optionKey is down then
    answer "opt key is down"
end if

param(n)

Get the nth value passed into the current procedure. Can be used to build a function that takes any number of arguments, see example.

Examples:



on mySumOfNumbers
    put 0 into total
    repeat with x = 1 to paramCount()
        put total + param(x) into total
    end repeat
    answer "total is" && total
end mySumOfNumbers

on mouseUp
    mySumOfNumbers 1, 2, 3
end mouseUp

paramCount()

Get the number of values passed into the current procedure. Can be used to build a function that takes any number of arguments, see example.

Examples:



on mySumOfNumbers
    put 0 into total
    repeat with x = 1 to paramCount()
        put total + param(x) into total
    end repeat
    answer "total is" && total
end mySumOfNumbers

on mouseUp
    mySumOfNumbers 1, 2, 3
end mouseUp

params()

Get all of the values passed into the current procedure.

Examples:



put params() into x


random(n)

n must be an integer.

Returns random value between 1 and n.

Examples:



put random(20) into roll

result()

The return value of the last called function or procedure.

Examples:



on myProc
    return "a"
end myProc
on mouseUp
    myProc
    answer the result
end mouseUp

round(x)

Returns integer nearest to number. Odd integers plus 0.5 round up, even integers plus 0.5 round down.

Examples:



put round(1.7) into x

screenRect()

Examples:



put screenRect() into x


seconds()

Seconds since January 1, 1904.

Examples:



put seconds() into x

selectedChunk()

A full description of the current selection, or "" if there is no selection.

Returns a string that looks like this: char 2 to 4 of cd fld id 567.

Examples:



set the textSize of the selectedChunk to 24
put the textFont of the selectedChunk into x
put word 2 of the selectedChunk into selStart 
put word 4 of the selectedChunk into selEnd 
put the selectedChunk into x

selectedField()

The field that contains current selected text, or "" if there is no selection.

(If compatibility mode is enabled, not recommended for new code, uses HyperCard's formatting and returns a string like

"card field 4".)

Examples:



put selectedField() into x
set the loc of the selectedField to 23, 45
set the textalign of the selectedField to "center"

selectedLine()

The number of the line of the current selected text, or "" if there is no selection.

(If compatibility mode is enabled, not recommended for new code, uses HyperCard's formatting and returns a string like

"line 3 of card field 4".)

Examples:



put selectedLine() into x


selectedText()

The value of the current selected text, or the empty string if there is no selection.

Examples:



put selectedText() into x

the selection

Reading from the selection retrieves the value of the current selected text, or the empty string if there is no selection.

Writing to the selection replaces the currently selected text with new text.

Examples:



put the selection into x
put "" into the selection
put "abc" into the selection

-- chunks are supported
put char 2 to 3 of the selection into x
put "" into char 2 to 3 of the selection
put "abc" into char 3 of the selection
put item 2 to 3 of the selection into x
put "" into item 2 to 3 of the selection
put "abc" into item 3 of the selection

-- you can delete part of the selection
delete char 2 to 4 of the selection
delete item 3 of the selection

shiftKey()

Check if this modifier key is pressed.

Examples:



if the shiftKey is down then
    answer "shift key is down"
end if


sqrt(x)

Examples:



put sqrt(0.5) into x

sin(x)

Examples:



put sin(0.5 * pi) into x


strToNumber(x)

Parse string to number. Supports scientific notation. If cannot be parsed, returns "false"

Examples:



put strToNumber("12") into x
put strToNumber("-12") into x
put strToNumber("1e6") into x
ask "what is a number"
put it into x
if strToNumber(x) is false then
    answer "that was not a number"
end if



sum()

Supports any number of arguments.

You can also provide a comma-delimited string.

Examples:



put sum(1,2,3) into x
put sum("1,2,3") into x -- returns 6


tan(x)

Examples:



put tan(0.125 * pi) into x


target

Refers to the object that was most recently acted on.

Here's one way this can be useful: If you have many buttons that all basically perform the same action, you could have an 'on mouseup' handler in the card script instead of a separate script within each button. This script in the card could check the target to see which of the buttons were clicked.

Different than 'me', which is the owner of the currently running script, which might be different than the button that was clicked.

Examples:



set the width of the target to 100

put the short id of the target into x


there is a

Check for the existence of an object.

Examples:



if there is a cd btn id 12345 then
    answer "found"
end if
if there is not a cd btn id 12345 then
    answer "not found"
end if

ticks()

Ticks (60th of a second) since January 1, 1904.

Examples:



put ticks() into x

tool()

The current simulated tool, as set by the choose command. (The choose command can be used to draw lines and shapes).

The actual current tool will be the browse tool when any script is running.

Examples:



choose pencil tool
answer tool() -- shows "pencil"

toLowerCase()

Examples:



answer toLowerCase("ABC") -- shows "abc"

toUpperCase()

Examples:



answer toUpperCase("abc") -- shows "ABC"


trunc(x)

Get integer part of a number. i.e. for positive numbers, always round down to the nearest integer.

Examples:



put trunc(0.5) into x



annuity(rate, periods)

Computes the total cost of an annuity now that will pay you one unit per period, over the specific number of periods.

You want to purchase an annuity that pays you $10,000 a year for 10 years. The interest rate is 10%. How much will it cost you now? The amount loaned is 10000 * annuity(0.10, 10), or 61445.67.

compound(rate, periods)

Returns the value of one unit of principal invested at the interest rate and compounded over the specified number of periods.

Example: $100 invested for 20 years at 10% interest compounded yearly:

futureValue = 100 * compound(0.10, 20) = about $673