# Using python to create Macintosh applications, part zero

This document will show you how to create a simple mac-style application using Python. We will glance at how to use file dialogs and messages.

Our example program checktext.py asks the user for a text file and checks what style end-of-lines the file has. This may need a little explanation: ASCII text files are almost identical on different machines, with one exception:

- Unix systems terminate lines with the "linefeed" character, `0x0a`,
- Macintoshes terminate lines with the "carriage return" character, `0x0d` and
- MSDOS and Windows terminate lines with first a carriage return and then a linefeed.

Let us have a look at the program. The first interesting statement in the main program is the call to `macfs.PromptGetFile`. This is one of the routines that allow you to ask the user to specify a file. You pass it one required argument, the prompt string. There are up to four optional MacOS file type arguments you can pass, as 4-byte strings. Specifying no file type will allow the user to select any file, specifying one or more types restricts the user to files of this type. File types are explained in most books on the Mac.

`PromptGetFile` returns two values: an FSSpec object and a success indicator. The FSSpec object is the "official" MacOS way of specifying a file, more on it later. The success indicator tells you whether the user clicked OK or Cancel. In the event of Cancel we simply exit back to the finder.

`PromptGetFile` has a number of friends that do similar things:

- `StandardGetFile` is identical to `PromptGetFile` but without the prompt. It has up to four optional filetype arguments.
- `StandardPutFile` asks the user for an output file. It will warn the user when she tries to overwrite an existing file. The routine has one mandatory argument: a prompt string. Pass the empty string if you do not want a prompt.
- `GetDirectory` asks the user for a folder (or directory, in unix terms). It has one optional argument: a prompt string.

All routines return an FSSpec and a success indicator.

There are many things you can do with FSSpec objects (see the macfs section in the Python Library Reference for details), but passing them to `open` is not one of them. For this, we first have to convert the FSSpec object to a pathname, with the `as_pathname` method. This returns a standard MacOS-style pathname with colon-separated components. This can then be passed to `open`. Note that we call open with mode parameter `'rb'`: we want to read the file in binary mode. Python, like C and C++, uses unix-style line endings internally and opening a file in text mode (`'r'`) would result in conversion of carriage-returns to linefeeds upon reading. This is something that Mac and DOS programmers are usually aware of but that never ceases to amaze unix buffs.

After we open the file we attempt to read all data into memory. If this fails we use `EasyDialogs.Message` to display a message in a standard dialog box and exit. The EasyDialogs module has a few more useful simple dialog routines, more on that in example 1.

The rest of the code is pretty straightforward: we check that the file actually contains data, count the number of linefeeds and returns and display a message with our guess of the end-of-line convention used in the file.

The example0 folder has three text files in Mac, Unix and DOS style for you to try the program on. After that, you can continue with example 1 or go back to the index to find another interesting topic.

Jack Jansen, jack@cwi.nl, 18-July-1996.