14. NS BASIC TechNote

How to download to your Names file  -- February 7,1996
--------------------------------------------------------

Not making an appearance on Newton OS 2.0 units is the Newton Connection 
Kit.
This leaves people who used to download their name and address files to 
their
Newtons with no way to do so. 

It's actually pretty easy to do using NS BASIC and Apple's new Newton Press
package. All it takes is a bit of work to format the data properly before 
you
download it. This method assumes you have a Newton 2.0 unit.

Names entries are in a frame. If you put together a string with the 
contents
of that frame already set up, NS BASIC can insert the entries. (NS BASIC 
could
also be used to put together the frame, but in this example, I'm offloading
that task to the host machine)

Here's a sample Names frame:

{name: {first:"John",last:"Nadeau"}, company:"The Wine Establishment",
address:"250 The Esplanade Suite 104", address2:"", city:"Toronto",
region:"ON", postal_code:"M5A 1J2", country:"", email:"", phones: ["861-
1331","861-1098","",""], notes: [{text:"Aubry Glazer voice mail 487-5183",
viewBounds: {left: 10, top: 7, right:230, bottom: 99}, viewStationery: 
'para,
viewFont: 18435}], cardType: '1, sorton: "NadeauJohn", class: 'person}

In this example, a Hypertalk script was used to create this data from a
Hypercard address stack. The code used is at the end of this tech note in
Program 2.

The resulting file was then pasted into Newton Press, and a package 
created.
It was then downloaded. Running Program 1 copies the data from the file and
puts it into the Names soup.

There are a couple of other interesting possibilities from this method. As 
the
data is moved as a package, it can be transferred by a variety of means:
downloading by serial or Appletalk connection, as email, as beaming: any 
way a
package can be moved. Secondly, this method provides a convenient way of
downloading all sorts of other data to Newtons, such as product files and
graphics.

Program 1: Extract Names from a Newton Press package:

0010 rem get data from NewtonBook
0020 LET pkgRef:=getPkgRef("addresses",getstores()[1])
0030 LET pkgInfo:=getPkgRefInfo(pkgRef)
0040 LET dataFile:=pkgInfo.parts[0].book.contents[0].data
0050 LET p=0
0060 LET i=0
0070 open ch,"names",sorton

0100 rem mainline
0110 GOSUB 1000 //get a record
0120 if rec="EOF" then stop
0125 LET i=i+1
0126 PRINT i
0130 LET frame:=compile(rec)
0140 LET entry:=u:frame()
0150 put ch,entry
0190 GOTO 0100 //mainline

1000 rem get a record
1020 LET p1=strPos(datafile,"}" & chr(13),p)
1030 if p1=NIL then goto 1050
1035 LET rec=substr(datafile,p,p1-p+1)
1040 LET p=p1+2
1045 return
1050 LET rec="EOF"
1060 return

Program 2: Create data for Newton Press

(Note: this is just a sample of how you might do this: you'll have to use
whatever tools you have on your host system to create a similar program)

on mouseUp
  put "Macintosh HD:Address Export" into f
  open file f
  go card 1 of bg "file Card"
  lock screen
  repeat with i=1 to the number of cards of this bg
    set cursor to busy
    if the number of words of bg field Name is 1 then
      put "" into firstname
      put bg field Name into lastname
    else
      put bg field name into firstname
      delete the last word of firstname
      put the last word of bg field name into lastname
    end if

    put "name: {" into s
    put "first:" & quote & firstname & quote & ",last:" ª
    & quote & lastname & quote & "}, " after s
    put "company:" & quote & bg field company & quote & ", " after s
    put "address:" & quote & bg field address1 & quote & ª
    ", address2:" & quote & bg field address2 & quote & ", " after s
    put "city:" & quote & bg field city & quote & ", region:" ª
    & quote & bg field state & quote & ª
    ", postal_code:" & quote & bg field postalCode & quote & ª
    ", country:" & quote & bg field country & quote & ", " after s
    put "email:" & quote & bg field email & quote & ", " after s
    put "phones: [" & quote & bg field phone1 & quote & "," ª
    & quote & bg field phone2 & quote & "," after s
    put quote & bg field phone3 & quote & "," & quote & ª
    bg field phone4 & quote & "], " after s
    
    if bg field notes is not empty then
      put the number of chars of bg field notes into lines
      put "notes: [{text:" & quote & bg field notes & quote & ", "  & ª
      "viewBounds: {left: 10, top: 7, right:230, bottom: " & lines & "}, " & ª
      "viewStationery: 'para, viewFont: {family: 'geneva, face: 0, size: 
9}}],
" after s
    end if
    put "cardType: '1, " after s
    if bg field name is empty then
      put bg field company into sortOn
      put "'company" into class
    else
      put lastName & firstName into sortOn
      put "'person" into class
    end if
    put "sorton: " & quote & sortOn & quote & ", " after s
    put "class: " & class after s
    write "{" & s & "}" & return to file f
    go next card of this bg
  end repeat
  close file f
  
end mouseUp

Further Enhancement: Getting the type of phone number to appear beside the
number

The type of the phone number is a subclass of the phone fields. The 
possible
subclasses are:

homePhone
workPhone
faxPhone
otherPhone
carPhone
beeperPhone
mobilePhone
homefaxPhone

You could modify the output program to have a number of setClass() calls to
fix the types after the frame is complete.