8.9. Saving and Loading Handlers

With your general purpose list handling routines, you can go ahead and use them in any program that needs to work with lists. One way to do that is simply to copy the code into any program that needs access to these handlers.

As you see in the following Try It Out, another alternative is to save your collection of handlers in a file in the normal way and then load it, using the load script command, into any program that needs to use it. The advantage of this approach is that you don't have to copy the code into each program. And if you ever find a bug in one of your handlers or want to make improvements, you only have to do it in the original file and not hunt down every file that contains the copied code.

8.9.1.

8.9.1.1. Try It Out: Saving and Loading the List Handlers

Now you see how you can load your handlers into another program.

  1. Type the list handlers listSum, listSort, removeItem, and insertItem from previous examples. Just type the handlers, not the code at the end of each program that tested them out. For space considerations, I don't show the code again here, just the structure of the program.

    -- General purpose list handlers
    
    -- Insert the code for the listSum handler here
    on listSum(L)
        ...
    end listSum
    
    -- Insert the code for the listSort and quickSort handlers here
    on listSort(L)
        ...
    end listSort
    
    on quickSort (L, leftIndex, rightIndex)
        ...
    end quickSort
    
    -- Insert the code the insertItem and removeItem handlers here on insertItem (L, ...

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.