Mac Format

HOWTO |open and save files

-

1 Adding menus

To add ways to open and save files, first you need to add menus and then script instructio­ns that make the menu items do what we want when selected. Choose Tools > Menu Builder, and click New to create a new menubar set. You get three menus ready to go, although we’ll tweak them a little. First, however, you may have noticed your stack window just got slightly shorter. As part of simplifyin­g cross-platform menu creation, the card slides under the window’s title bar by 22 pixels. Now the resize script we made earlier makes the large ‘work’ field fit the top of the card perfectly.

2 Edit Menu

In the Menu Builder window, select ‘Edit’ on the left, then select ‘Clear’ in the right-hand list. Click the New Item button in the right-hand section to add a new menu item below ‘Clear’. Name this “Select All”, and set the Shortcut to ‘A’ with the Cmd checkbox clicked. Delete the Preference­s item and the dash entry just above it. Now click Auto Script on the left. This adds a generic script structure to the Edit menu, and if you click the Edit Script button you can add your own instructio­ns. Replace the Cut menu’s commented line with cut . It’s that simple! The script for the Copy line is copy (similarly, Paste is paste , and Clear is clear ). The Select All menu item’s script is select text of field "work" .

3 Open and Save

Select the File menu in the Menu Builder window and add Save and Save As menu items to its list. Click Auto Script, then the Edit Script button. At the top of the Code Editor window add global gTheFile . We’ll use this to track which file we open so we can save changes back to it. Now, in the section for the Open menu item, add the following:

answer file "Select a text file" with type "Text Files|txt|TEXT" if it is not empty then put it into gTheFile open file gTheFile read from file gTheFile until EOF close file gTheFile put it into field "work"

end if

The ‘answer file’ instructio­n opens a file selection dialogue, and the rest filters out all but text-based documents. If ‘it’ is not empty, then ‘it’ contains the path to the chosen file. Store this in the gTheFile variable; open the file this refers to, read the contents of the file until EOF (‘end of file’) then close the file again. The data we read – stored in the ‘it’ variable – is put into the ‘work’ field. Saving is similar. If gTheFile contains something, the file it refers to is opened, field ‘work’ is written to it (replacing whatever it contains), then it’s closed.

if gTheFile is not empty then open file gTheFile write field "work" to file gTheFile close file gTheFile end if

Save As must first ask the user to choose where to save the new file and what to call it, using the ‘ask file’ command. Assume the user knows to add an appropriat­e end to the file name. The result is put into the global gTheFile variable (so we can do regular saves later), then we go through the same process of opening the file, writing to it, then closing the file.

ask file "Save this as" if the result is not empty then put it into gTheFile open file gTheFile write field "work" to file gTheFile close file gTheFile end if

The lines to put into the New menu item’s script are put empty into field "work" and put empty into gTheFile , and the Quit menu item’s script is simply quit .

 ??  ?? The Menu Builder makes menu creation exceptiona­lly easy, including generating script templates.
The Menu Builder makes menu creation exceptiona­lly easy, including generating script templates.
 ??  ?? With just a few lines of script, you can make a truly functional tool.
With just a few lines of script, you can make a truly functional tool.

Newspapers in English

Newspapers from Australia