12.6. Storing, Loading, and Running Scripts from Files

You saw how the load script command was used in Chapter 8 to load a set of handlers from a file. In the following Try It Out, you see commands for storing a script in a file and running a script stored in a file.

12.6.1.

12.6.1.1. Try It Out: Illustrating the store script and run script Commands

In the following program, you define a simple script, store it in a file, and then run the script you stored in that file.

  1. Type the following program into Script Editor:

    -- Illustrate the store script and run script commands
    
    set scriptFileName to ((path to scripts folder as string) & "test.scpt")
    
    script testScript
        display dialog "Hello" buttons {"OK"} default button 1
    end script
    
    -- write the script to a file
    
    store script testScript in file scriptFileName replacing yes
    
    -- run the script stored in the file
    
    run script file scriptFileName
  2. Run the program. You get the dialog shown in Figure 12-5.

    Figure 12.5. Figure 12-5
12.6.1.2. How It Works

You store a script object in a file using the store script command. The optional replacing parameter specifies whether to overwrite a preexisting file. If the file exists and you specify replacing no, the program terminates with an error (unless you catch it within a try statement). If you omit the replacing parameter completely, the program asks you if you want to overwrite a preexisting file. ...

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.