Skip to content

Commit 024a387

Browse files
committed
- Added a file dialog example
- Added pointers to library documentation
1 parent a547dca commit 024a387

4 files changed

Lines changed: 132 additions & 2 deletions

File tree

‎Mac/Demo/example0.html‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<HTML><HEAD><TITLE>Using python to create Macintosh applications, part zero</TITLE></HEAD>
2+
<BODY>
3+
<H1>Using python to create Macintosh applications, part zero</H1>
4+
<HR>
5+
6+
This document will show you how to create a simple mac-style
7+
application using Python. We will glance at how to use file dialogs and
8+
messages. <p>
9+
10+
Our example program <a href="example0/checktext.py">checktext.py</a> asks
11+
the user for a text file and checks what style end-of-lines the file has.
12+
This may need a little explanation: ASCII text files are almost identical
13+
on different machines, with one exception:
14+
<ul>
15+
<li> Unix systems terminate lines with the "linefeed" character, <code>0x0a</code>,
16+
<li> Macintoshes terminate lines with the "carriage return" character,
17+
<code>0x0d</code> and
18+
<li> MSDOS systems terminate lines with first a carriage return and then a linefeed.
19+
</ul>
20+
21+
Let us have a look at the program. The first interesting statement in the main
22+
program is the call to <code>macfs.PromptGetFile</code>. This is one of the routines
23+
that allow you to ask the user to specify a file. You pass it one required
24+
argument, the prompt string. There are up to four optional MacOS <em>file type</em> arguments
25+
you can pass, as 4-byte strings. Specifying no file
26+
type will allow the user to select any file, specifying one or more types restricts
27+
the user to files of this type. File types are explained in most books on the Mac. <p>
28+
29+
<code>PromptGetFile</code> returns two values: an <em>FSSpec</em> object and a
30+
success indicator. The FSSpec object is the "official" MacOS way of specifying a
31+
file, more on it later. The success indicator tells you whether the user clicked OK
32+
or Cancel. In the event of Cancel we simply exit back to the finder. <p>
33+
34+
<code>PromptGetFile</code> has a number of friends that do similar things:
35+
<ul>
36+
<li> <code>StandardGetFile</code> is identical to <code>PromptGetFile</code> but
37+
without the prompt. It has up to four optional filetype arguments.
38+
<li> <code>StandardPutFile</code> asks the user for an output file. It will
39+
warn the user when she tries to overwrite an existing file. The routine has one
40+
mandatory argument: a prompt string. Pass the empty string if you do not want a prompt.
41+
<li> <code>GetDirectory</code> asks the user for a folder (or directory, in unix terms).
42+
It has one optional argument: a prompt string.
43+
</ul>
44+
All routines return an FSSpec and a success indicator. <p>
45+
46+
There are many things you can do with FSSpec objects (see the
47+
<a href="http://www.python.org/doc/lib/macfs.html">macfs</a> section in the
48+
<a href="http://www.python.org/doc/lib/Top.html">Python Library Reference</a>
49+
for details), but passing them to <code>open</code> is not
50+
one of them. For this, we first have to convert the FSSpec object to a pathname, with
51+
the <code>as_pathname</code> method. This returns a standard MacOS-style pathname with
52+
colon-separated components. This can then be passed to <code>open</code>. Note that
53+
we call open with mode parameter <code>'rb'</code>: we want to read the file in binary
54+
mode. Python, like C and C++, uses unix-style line endings internally and opening a
55+
file in text mode (<code>'r'</code>) would result in conversion of carriage-returns to
56+
linefeeds upon reading. This is something that Mac and DOS programmers are usually aware
57+
of but that never ceases to amaze unix buffs. <p>
58+
59+
After we open the file we attempt to read all data into memory. If this fails we use
60+
<code>EasyDialogs.Message</code> to display a message in a standard dialog box and exit.
61+
The EasyDialogs module has a few more useful simple dialog routines, more on that in
62+
<a href="example1.html">example 1</a>. <p>
63+
64+
The rest of the code is pretty straightforward: we check that the file actually contains
65+
data, count the number of linefeeds and returns and display a message with our guess of the
66+
end-of-line convention used in the file. <p>
67+
68+
The <a href="example0">example0</a> folder has three text files in Mac, Unix and DOS style
69+
for you to try the program on. After that, you can continue with <a href="example1.html">example 1</a>
70+
or go back to the <a href="index.html">index</a> to find another interesting topic. <p>
71+
72+
<HR>
73+
<A HREF="http://www.cwi.nl/~jack">Jack Jansen</A>,
74+
<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 18-July-1996.
75+
</BODY></HTML>

‎Mac/Demo/example0/checktext.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""checktext - Check that a text file has macintosh-style newlines"""
2+
3+
import macfs
4+
import sys
5+
import EasyDialogs
6+
import string
7+
8+
def main():
9+
fsspec, ok = macfs.PromptGetFile('File to check end-of-lines in:', 'TEXT')
10+
if not ok:
11+
sys.exit(0)
12+
pathname = fsspec.as_pathname()
13+
fp = open(pathname, 'rb')
14+
try:
15+
data = fp.read()
16+
except MemoryError:
17+
EasyDialogs.Message('Sorry, file is too big.')
18+
sys.exit(0)
19+
if len(data) == 0:
20+
EasyDialogs.Message('File is empty.')
21+
sys.exit(0)
22+
number_cr = string.count(data, '\r')
23+
number_lf = string.count(data, '\n')
24+
if number_cr == number_lf == 0:
25+
EasyDialogs.Message('File contains no lines.')
26+
if number_cr == 0:
27+
EasyDialogs.Message('File has unix-style line endings')
28+
elif number_lf == 0:
29+
EasyDialogs.Message('File has mac-style line endings')
30+
elif number_cr == number_lf:
31+
EasyDialogs.Message('File probably has MSDOS-style line endings')
32+
else:
33+
EasyDialogs.Message('File has no recognizable line endings (binary file?)')
34+
sys.exit(0)
35+
36+
if __name__ == '__main__':
37+
main()
38+
39+

‎Mac/Demo/index.html‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ <H1><IMG SRC="html.icons/python.gif">Macintosh Python crash course</H1>
1919
http://www-acs.ucsd.edu/~jstrout/python/</A>.
2020
<P>
2121

22+
The <a href="http://www.python.org/doc/lib/Top.html">Python Library Reference</a> contains a section on
23+
<a href="http://www.python.org/doc/lib/Macintosh-Specific-Services.html">Macintosh-specific modules</a>
24+
that you should also read. Documentation is also available in PostScript and other
25+
forms, see the <a href="http://www.python.org/doc/">documentation</a> section
26+
on the webserver. <p>
27+
2228
Some of these documents were actually written while I was working on a "real"
2329
project: creating a single-button application that will allow my
2430
girlfriend to read her mail (which actually pass thry <EM>my</EM>
@@ -36,6 +42,12 @@ <H1><IMG SRC="html.icons/python.gif">Macintosh Python crash course</H1>
3642
<H2>Table of contents</H2>
3743

3844
<UL>
45+
<LI>
46+
<A HREF="example0.html">Using python to create Macintosh applications,
47+
part zero</A> whets your appetite by showing you how to ask the user
48+
for a filename, and how to display a message. It explains about end-of-line
49+
confusion while doing so.
50+
3951
<LI>
4052
<A HREF="example1.html">Using python to create Macintosh applications,
4153
part one</A> explains how to create a simple modal-dialog application
@@ -111,4 +123,5 @@ <H2>Table of contents</H2>
111123

112124
<HR>
113125
<A HREF="http://www.cwi.nl/~jack">Jack Jansen</A>,
114-
<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 18-May-1996.
126+
<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 18-July-1996.
127+
</BODY></HTML>

‎Mac/Demo/using.html‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
</HEAD>
55
<BODY>
66
<H1>Using Python on the Macintosh</H1>
7-
<EM>(preliminary)</EM>
87
<HR>
98

109
This document is an introduction to using Python on the Apple
@@ -369,6 +368,10 @@ <h2>Troubleshooting</h2>
369368
imported. BBEdit has a popup menu which allows you to inspect (and
370369
set) the end-of-line convention used in a file. <p>
371370

371+
<h2>Where to go from here</h2>
372+
373+
The next section to check out is the <a href="index.html">annotated sample programs</a>.<p>
374+
372375
<HR>
373376
<A HREF="http://www.cwi.nl/~jack">Jack Jansen</A>,
374377
<A HREF="mailto:jack@cwi.nl">jack@cwi.nl</A>, 19-Apr-1996.

0 commit comments

Comments
 (0)