12.9. Exercises

  1. Write a script called countMe that logs the number of times it?s been called. Then run the script 10 times in a repeat loop to test it out.

  2. The surface area of a sphere with radius r is given by the following formula:

    Surface area = 4 π r2

    Add a handler called surfaceArea to the circle script object developed in this chapter and test it out.

  3. AppleScript has a built-in class called point that is simply a two-item list of numbers that you can use to conveniently represent a coordinate point. For example, the following creates a point with coordinates (100, 200) and assigns it to the variable P1:

    set P1 to {100, 200} as point

    Extend the rectangle and square classes to contain a point property called origin that stores the object?s origin, with a default value of (0, 0). Add methods called setOrigin and getOrigin to set and get the values of this origin.

  4. In Script Editor, save your definitions of the rectangle and square handlers in a file called rects.scpt in your folder. (Why can?t you use the store script command to do this?) Then, use the load script command to load the script file you created in Exercise 3. Create a rectangle with a width of 15 and a height of 20 and a square with sides of 12. Calculate and log the area of the rectangle and the perimeter of the square.

  5. A dictionary is a data structure used in many programming applications (not to be confused with the application dictionaries that you deal with in AppleScript). For example, on the Mac, the Cocoa programming ...

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.