TweetFollow Us on Twitter

Volume Number:2
Issue Number:5
Column Tag:Fortran's World

The Clipboard and Desk Scrap Revealed

By Mark E. McBride, MacTutor Contributing Editor

Before moving to this month's topic, there are two items of news. First, I have received Apple's new 20 megabyte hard disk . As reported in the January issue of MacTutor the new HD20 will not work with the current 2.1 release of MacFortran, nor will it work on a Mac Plus or under HFS. Discussion with Absoft/Microsoft revealed that a solution will be forthcoming, probably with the 2.2 version (which is to be released sometime this spring or early summer). I will pass along any information I receive.

[We at MacTutor think it is a disgrace that Microsoft is dragging their feet on the Fortran Upgrade. All the other languages for the Mac now run correctly under HFS. Fortran not only is buggy under HFS, it doesn't run at all! It is unfair to Fortran customers for Microsoft to be so slow to respond to the new systems, especially since they have taken great pains to see that their own internally developed software works! Obviously, those products which Microsoft distributes from other vendors take a back seat to their own products. Absoft has supplied Microsoft with the new version of Fortran and we think if Microsoft can't speed that product to market, they should return the rights back to Absoft so the product may be properly supported and upgraded in a timely manner. -Ed.]

The move to the hard disk forced me to upgrade memory. Actually, I find it fairly amazing that I have written the current and all the previous columns using 128k and two drives. The only time I have felt constrained by the 128k is trying to debug programs with large data structures. The debugger takes about 40k, leaving very little room for your program. Note, however, that the shell application in the November 1985 issue was compiled and debugged on a 128k machine.

The second piece of news of some importance is that the include files in MacFortran 2.1 have some errors in the parameter definitions. These errors became apparent the first time I tried to do memory management related toolbox calls from MacFortran. I talked with Absoft about the problems I was having and they disclosed the existence of the errors. Fortunately, these errors can be corrected by editing the toolbox include files. Listing 1 is a reprint Absoft sent to me which shows the correct parameter definitions for the affected toolbox calls. Be sure to correct the parameter definitions in both the toolbx.par file and in the and files. You will need to correct these include files to use this month's function program.

A related note is that some of the update/ correction files have been appearing on Compuserve's MAUG board. They can be found under the Mac Developer's download library on programming tools.

The Desk Scrap

All of us are familiar with the clipboard in the standard Macintosh application. The clipboard holds a copy of the last item cut or copied so that it can be pasted back in at a later time. Further, we know that cutting or copying to the clipboard allows us to exit the application to the finder and then enter a different application with the clipboard intact (if the second application conforms to the standards for cutting and pasting). The desk scrap is the vehicle for transferring information between applications. These other applications can be standalone programs or desk accessories. The desk scrap normally resides in the heap in memory, but it can be moved to a disk by an application if memory space is a problem. Figure 1 illustrates the typical relationship between two applications, a desk accessory, and the TEscrap and the desk scrap. This article will focus on the use of a private scrap in an application with the desk scrap.

The application program is responsible for insuring that the proper information is retrieved from the desk scrap or moved to the desk scrap. Although it is possible to read and write information directly to the desk scrap, most applications use a private scrap for cut, copy, and paste operations and then pass information to and from the desk scrap using routines supplied in the toolbox. Also, it is important to note that the rom text edit routines (accessed via TExxxx toolbox calls) maintain their own private scrap. We will save the discussion of the TE scrap and the desk scrap for another time. The TEfromScrap and TEtoScrap Pascal calls are not part of the rom and are not availabe from MacFortran.

Upon entry to an application the desk scrap resides on the application heap and has the format shown in Figure 2. The information in the desk scrap may be passed in more than one format. The two standard format types are 'TEXT' and 'PICT'. Desk scrap information of type 'TEXT' is simply a series of plain unformatted ASCII text codes. Desk scrap information of type ' PICT' is data representing commands to draw a Quickdraw picture (see Pascal Procedures, MacTutor Vol 1 #11 October 1985 for an example of a Pascal routine which puts a ' PICT' type to the desk scrap). Inside Macintosh states that applications should write at least one of these two types of information to the scrap with the preferred format written first. Applications should be able to read at least one, if not both of these data formats. Additionally, user can put data types of their own construction out to the desk scrap for passing of information to other applications which can recognize those private types.

Using the Desk Scrap from Fortran

From within an application there are several approaches to using the desk scrap. The basic approach I take is using general purpose subroutines to move data between an application's private scrap and the desk scrap. The basic toolbox calls available to use the scrap from Fortran are:

Function InfoScrap :Inquire Deskscrap

Function UnLoadScrap :Deskscrap to disk

Function LoadScrap :Disk to Deskscrap

Function GetScrap :Deskscrap to Private Scrap

Function ZeroScrap :Empty Deskscrap

Function PutScrap :Private Scrap to Deskscrap

The source code for a routine called RScrap is given in Listing 2. The routine is accessed by 'call RScrap(Priscrap, scraplength)'. The argument Priscrap is a handle to the application's private scrap. The length of the scrap copied to the private scrap is returned in the argument scraplength. If there was a problem in copying the desk scrap into the private scrap, the error code generated (a negative value) will be returned in the argument scraplength. RScrap is set up so that the first call to it from an application automatically forces a transfer of the desk scrap to the private scrap. Any further calls to RScrap during execution of the application transfers the desk scrap only if the desk scrap's contents have changed since the last call.

To be able to use the desk scrap effectively, RScrap defines the variables to be able to access information about the desk scrap using InFOSCRAP. A data structure for the scrap record is constructed using a pointer and offsets (reviewing sections 1.06 and 1.07 of the Toolbox appendix of the MacFortran manual might be useful at this point). This data record allows access to any of the information contained in the scrapStuff structure. This same technique is used to access information in a grafport record structure or a window record structure under MacFortran. RScrap gets the scrap record information as its first step. It uses this information, along with calls to GETSCRAP (with a NIL private scrap handle) and MAXMEM to see if enough available memory exists.

Before calling the routine RScrap from the application, the user will want to create space on the heap for the private scrap. This handle is passed to RScrap as the Priscrap argument. RScrap makes a call to GETSCRAP with an appropriate specification of data type (e.g., 'PICT' or 'TEXT') and with the private scrap handle passed (RScrap could easily be modified to pass the type specification as an argument). GETSCRAP will resize the private scrap to the appropriate size, move a copy of the data of the type specified from desk scrap to the private scrap, and return the length of the scrap copied. If the length returned is negative, it represents the operating system error result or the scrap manager error result. Next, RScrap gets the current scrap count by making a call to INFOSCRAP and using the record structure offset given in scrapCount. If the user has a show clipboard option, then the loaded copy of the desk scrap could be displayed after exiting RScrap.

A subroutine similiar to RScrap (called PScrap) could easily be developed which moves the private scrap to the desk scrap. The routine would need to pass the private scrap handle and length. The PScrap routine will need to zero the desk scrap with ZEROSCRAP and then use PUTSCRAP to copy the private scrap to the desk scrap. Development of PScrap will be left to the reader.

Integrating the desk scrap and a private scrap in an application becomes a matter of calling RScrap and PScrap at the appropriate times. During the processing of the main event loop, there are five places where the user will want to concern themselves with the interaction of the private scrap and the desk scrap. First, the user should call RScrap during the initialization portion of the application program. This will load the desk scrap into the private scrap and will set the Count variable within RScrap to the current scrapCount value. Second , on each pass through the main event loop, after the check for SYSTEMTASK, the user will want to call RScrap. This allows RScrap to check for a change in the desk scrap (because of a desk accessory) and recopy the desk scrap to the private scrap, when necessary. Third, in the routine that handles menu events, if the user selects a desk accessory the application should call PScrap, before calling the desk accessory. Fourth, the user should call RScrap or PScrap (as appropriate) during activate and deactivate events if a system window (i.e., desk accessory) is involved. Finally, the application should call PScrap to update the desk scrap when exiting the application.

Importing Data via the Desk Scrap

My original motivation for investigating the desk scrap was to see if I could import data for statistical subroutines from either Multiplan or a word processor, simplifying data entry. The integer function Import given in Listing 3 (as part of the TestImport program) reads TEXT from the desk scrap into a Fortran array. The formal call to Import is:


where A is the array of maximum size rmax rows and cmax columns. The integer value returned will be zero if Import successfully read TEXT scrap into the array; otherwise the integer value returned will be the error code returned during the GETSCRAP process (which was returned by the call to RScrap). The function reads data from the desk scrap assuming that blanks, commas, or tabs separate the data values (thus handling data cut from Multiplan as well as a word processor). The routine will not handle non-numeric characters in the fields. If the desk scrap exceeds the dimensions of the array, the sub-array bounded by (rmax, cmax) will be read. If the data doesn't fill the array, the remaining elements of the array maintain their values from entry, but rmax and cmax are set to the size read in. Elements in the scrap which are not specified (i.e., two succesive tabs or commas) are left unchanged in the array.

Examination of Listing 3 reveals how the function works. First, space for the private scrap is allocated on the heap using NEWHANDLE. Then the 'TEXT' contents (if any) of the desk scrap is copied to the private scrap using the routine RScrap. The private scrap is then locked in memory for the duration of the function. If the RScrap routine returned a negative result, then the function sets its result to the error code, unlocks and disposes the private scrap, and exits.

If the RScrap routine returns a positive number for scrapLength, then data of type 'TEXT' was copied to the private scrap and its length is the positive number returned. The Import function result is set to zero. In this case, the function loops through the private scrap creating a string variable for each data value found and counting the number of data elements for that line. If data is found for a particular row and column element then the data element is read into the array A. If no data is found (e.g. two commas or tabs in a row), then the element of A is left unchanged. The process continues for as long as their is data in the private scrap or room in the array A.

After all the data has been read into the array A, the values of rm and cm are reset to the largest values used (if less than the original values passed). Finally, the private scrap is unlocked, its handle disposed, and the function exits to the calling program.

A short program to test the function is also given as part of Listing 3. This program simply prints the data array A to the screen twice, once for the full dimensions of A and once based on the dimensions returned by Import. A test data set is given in Listing 4, but your own could be created with any text editor or Multiplan. The test data contains many strange combinations of commas, tabs, and spaces to separate the values. Figure 3 shows the screen from the program in Listing 3 and the data from Listing 4.

This ends our introduction to the desk scrap. Till next time, happy computing.

Listing 1
* This file contains parameter definitions which were incorrect
* in the initial release of Microsoft Fortran 2.1.  Several OS
* traps were defined with the 'save A0' flag set to zero (this
* indicates that A0 should be restored) for OS functions which * return 
a value in A0.
* These traps are found in the following Fortran include files:

* Maxmem is a special case in that it returns 2 values.  Pascal
* returns one of these in the function's argument; the
* FORTRAN toolbx.sub function has no provision for this.  For
* those requiring the Grow value, the MAXMEM2 definition 
* below should work.  MAXMEM will return only
* the size of the largest contiguous block, MAXMEM2 will
* return only the Grow size.

* Function MaxMem (VAR Grow: Size): Size;

* Function NewHandle (LogicalSize: Size): Handle;

* Function HandleZone (H: Handle): THz;

* Function RecoverHandle (P: Ptr): Handle;

* Function NewPtr (LogicalSize:  Size):  Ptr;

* Function GetTrapAddress (trapNum:  INTEGER): LongInt;

* Function GetZone : THz;

* Function PtrZone (P: Ptr): THz;
subroutine RScrap(Priscrap,scraplength)
*  This subroutine reads the deskscrap into a private scrap
*  located at the handle given by Priscrap.  It only loads the
*  deskscrap if the scrap has changed (read is automatically
*  forced during the first call) and if there is enough available
*  memory  to read in the deskscrap.
*  Mark E. McBride
*  211 N. University Ave.
*  Oxford, OH  45056
*  (513) 523-1438
 implicit none   ! keep us out of trouble
*  Variables needed to get private scrap
 integer*4 Priscrap! handle to private scrap
 integer*4 scraplength  ! length of private scrap
 integer*4 offset! desk scrap offset
 integer*2 Count ! scrap count
*  This defines the PscrapStuff information record structure
 integer*4 PscrapStuff  ! Pointer to Infoscrap record
 integer*4 scrapSize ! size of desk scrap
 integer*4 scrapHandle  ! handle to desk scrap
 integer*2 scrapCount! count changed by Zeroscrap
 integer*2 scrapState! tells where desk scrap is
 integer*4 scrapName ! pointer to desk scrap name
parameter(scrapSize  =z'0') ! offset to scrapSize
parameter(scrapHandle=z'4') ! offset to scrapHandle
parameter(scrapCount =z'8') ! offset to scrapCount
parameter(scrapState =z'A') ! offset to scrapState
parameter(scrapName  =z'C') ! offset to scrapName

*  Other general variables needed
 integer*4 toolbx,Grow
*  Keep Count between successive calls of RScrap
 Save Count
*  Initial data values to force reading of desk scrap for first
*  call of RScrap

 data Count,Grow/-9999,100/
*  Begin subroutine by checking to see if desk scrap has
*  changed then copy only if 'TEXT' available in desk scrap and
*  if there is memory available.
PscrapStuff=toolbx(INFOSCRAP) ! get desk scrap info record
if (word(PscrapStuff+scrapCount).ne.Count) then
 Count=word(PscrapStuff+scrapCount)! scrap changed
 Grow=toolbx(MAXMEM,Grow) ! compact & check memory
 ! check for 'TEXT' without copy
  if ((scraplength>=0).and.(scraplength<Grow)) then 
 ! get available scrap
      Count=word(PscrapStuff+scrapCount) ! update Count
 end if
end if
Program TestImport
 implicit none   ! keep us out of trouble
 integer*4 kmax,nmax,toolbx,i,j,Import
 real*8 B(10,5)
 data B/50*-1.0/
 data nmax,kmax/10,5/
 write(*,*) 'The matrix is set to 10 rows and 5 columns max'
 if (Import(B,nmax,kmax)=0) then
   write(*,*) 'The matrix as the full 10 by 5 '
   write(*,100) ((B(i,j),j=1,5),i=1,10)
100  format(5f8.1) 
   call toolbx(MOVETO,300,100)
   type 'Press return to continue'
   call toolbx(MOVETO,300,160)
   write(*,*) 'The submatrix of the 10 by 5 which was read in'
   do 120 i=1,nmax
120  write(*,100) (B(i,j),j=1,kmax)
   call toolbx(MOVETO,300,250)
   type 'Press return to continue'
   write(*,*) 'No type TEXT found in Clipboard'
 end if

 integer*4 function Import(A,rm,cm) 
*  This function pulls an array of numbers from the deskscrap
*  The array size is passed in cm (number of columns) and rm
*  (number of rows).  The program reads in the data assuming that 
*  blanks, commas, or tabs separate the data values.  The routine 
*  will not handle non-numeric characters in the fields.
*  If the deskscrap data exceeds the dimensions of the matrix
*  only those elements within (rm,cm) will be read.  If the
*  data doen't fill the matrix, the remaining elements of the 
*  matrix maintain their values from entry and rm,cm are set 
*  to the size read in.
*  Mark E. McBride
 implicit none   ! keeps us out of trouble
 integer*4 rm,cm ! maximum number of rows and columns
 real*8 A(rm,cm) ! array of elements to be passed
 character*256 strg! general purpose string variable
 integer*4 i,j,col,row,cmax,rmax ! assorted looping variables
 integer*4 toolbx,temp  ! toolbox interface
 integer*4 PriScrap! handle to copy of desk scrap
 integer*4 length! private scrap length
 integer*4 space,comma,tab,return ! delimiting characters
 integer*1 c1    ! single character as integer
 character*1 ch  ! single character as character
 equivalence (c1,ch)
 parameter (space=32)
 parameter (comma=44)
 parameter (tab=9)
 parameter (return=13)
*  Set up scrap handles
 PriScrap=toolbx(NEWHANDLE,0) ! get handle for private scrap
 call RScrap(PriScrap,length) ! read in scrap of type 'TEXT'
 call toolbx(HLOCK,PriScrap)! lock copy down temporarily
 if (length > 0) then! got something in scrap
   temp=long(PriScrap)  ! get ptr from handle to data
* get the data
   cmax=0 ! observed maximum column
   rmax=0 ! observed maximum row
   while (i<length)
     while(byte(temp+i)=space .and. i<=length) 
 ! skip leading spaces
     if (i>length) goto 10
     j=1! indexes current character in strg
     strg=""! set current string null
     c1=byte(temp+i) ! get first byte
     while ((
     1             ( ! process if not separator
              strg(j:j)=ch! stuff character to string
       if (i<=length) then! test for end of scrap
       end if
     col=col+1   ! bump column counter
     if ((col<=cm).and.("")) read(strg,*) A(row,col) ! read data 
     cmax=min(col,cm)! reset  column used count
     if (c1=return) then  ! check for end of line
       if (row>rm) goto 10! check array boundary
     end if
 end if
10 call toolbx(HUNLOCK,PriScrap) ! unlock copy of scrap
 call toolbx(DISPOSHANDLE,PriScrap) ! dispose 
 rm=rmax! set rm for exit
 cm=cmax! set cm for exit

Fig. 3 Program reads and displays clipboard

LIsting 4: A random sample of test date to try

1 11,,1111
 22,222 2222  22222
3,33,333   3333    33333 333333
 4,44,444     4444 44444
     6 66 666 6666 66666
  7 77 777 7777 77777
8 88 888 8888 88888 888888
999 999  9999    99999

Community Search:
MacTech Search:

Software Updates via MacUpdate

The best games like Animal Crossing on m...
Animal Crossing amiibo Festival is out right now for the Wii U, reminding us of just how much fun that world can be. Or at least to go back and check in on our villages once in a while. [Read more] | Read more »
Between 2 Taps - Tap for Tap interview M...
Hello, and welcome back to Between 2 Taps, Tap for Tap’s Indie Dev interview series. [Read more] | Read more »
Facility 47 (Games)
Facility 47 1.0.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.0.1 (iTunes) Description: You wake up alone and freezing in an icy cell. You try the cell door but it’s locked, it seems that you are stuck with no... | Read more »
The best Photoshop alternative on iPad
Instagram and Lightroom are great and all, but sometimes people need to get extra creative with their image editing.Like, Photoshop creative. If you're one of these people, take a look at our pick for the best mobile Photoshop experience on iPad... | Read more »
The Walking Dead: No Man’s Land guide -...
A new update for The Walking Dead: No Man’s Land was released last week, making it the perfect time for you to head back to your base and take out some walkers. Here’s the lowdown on what’s new to the game, and how to take advantage. [Read more] | Read more »
Goat Rider guide - Tips and tricks to st...
We've all been there. One second, we're riding high on a crazed goat, and the next, we've been tossed off it like someone who's no good at goat ridin'. [Read more] | Read more »
Real Boxing 2 CREED: How to become a gre...
Just in time for Rocky fans who can’t wait to see CREED, the latest movie, we have the official tie-in game,Real Boxing 2 CREED. It builds on the success of its predecessor and there’s lots to take in so we at 148apps thought we’d run you through... | Read more »
CoinOp Heroes 2 guide - How to build an...
CoinOp Heroes 2 justlaunched and, like all clickers, it's dangerously addictive stuff. You have to furiously tap your screen to defeat wave after wave of foes and earn an insane amount of cash to spend on character upgrades and an army of minions... | Read more »
Dr. Panda Firefighters (Education)
Dr. Panda Firefighters 1.0.1 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0.1 (iTunes) Description: FIGHT FIRES AND SAVE THE DAY!Work together with Dr. Panda and his firefighting team to rescue his trapped... | Read more »
Puddle + (Games)
Puddle + 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Puddle is back in a new "+" edition featuring enhanced graphics, new videos and Apple TV support ! No IAP and No Ads. Dive into Puddle... | Read more »

Price Scanner via

Save $30-$40 on new Apple TVs after rebate
Adorama has new Apple TVs on sale for up to $40 off MSRP after mail-in rebate, good through December 15th. Shipping is free, and Adorama charges NY & NJ sales tax only: - 32GB Apple TV: $119.99... Read more
13-Inch Haswell MacBook Air At Two Years – Th...
The 13-inch mid-2013 “Haswell” MacBook Air I ordered in Apple’s November 2013 Black Friday sale was my first new Mac in four and a half years — the longest interval I’ve gone between system upgrades... Read more
Target Black Friday Early Access deals: $100...
Target is offering early access to their Black Friday deals on Apple products on their online store for today, the 25th, only. Choose free shipping or free local store pickup (if available): - Apple... Read more
BlackBerry Q3 Mobility Index Report Finds iOS...
BlackBerry has announced results of its thirteenth Good Mobility Index Report, showing that organizations are increasingly building custom secure apps. Among Good Powered by BlackBerry (formerly Good... Read more
Wednesday roundup of early Black Friday Mac s...
Save up to $500 on a new Mac with these early Black Friday deals from Apple resellers, currently the lowest prices available for these models: (1) B&H Photo has all new Macs on sale for up to $... Read more
iPod nano on sale for $119, $30 off MSRP
Walmart has the 16GB iPod nano (various colors) on sale for $119.20 on their online store for a limited time. That’s $30 off MSRP. Choose free shipping or free local store pickup (if available). Sale... Read more
Adorama Black Friday deals: Up to $400 off Ma...
Adorama has released their Black Friday deals for 2015. Save up to $400 on MacBook Pros, $200 on MacBooks and MacBook Airs, and $270 on iMacs. Use code RYBFDEAL during checkout to see these prices.... Read more
B&H Photo Deals: $200 off 12-inch 1.2GHz...
In addition to the B&H Photo Black Friday week sales we posted yesterday, B&H has lowered their price on two products to $200 off MSRP: - 12″ 1.2GHz Gray Retina MacBook: $1399 save $200 - 13... Read more
Best Buy Early Access: Today only, Up to $125...
Best Buy has iPad Air 2s on sale for up to $125 off MSRP and Apple Watch models on sale on their online store for up to $100 off MSRP with special codes through midnight CT tonight. Choose free... Read more
UPPERCASE DESIGNS Premium Ultra Thin Keyboard...
UPPERCASE Designs today announced its new Premium Ultra Thin Keyboard Protector and its Palm Rest Protector Set for the 12-inch MacBook. The accessories provide durable protection for the 12-inch... Read more

Jobs Board

Hardware Systems Integration Engineer - *App...
# Hardware Systems Integration Engineer - Apple Watch Job Number: 39380139 Santa Clara Valley, Califo ia, United States Posted: Apr. 23, 2015 Weekly Hours: **Job Read more
Sr. Technical/Project Manager, *Apple* Educ...
# Sr. Technical/Project Manager, Apple Education Job Number: 36588557 New York City, New York, United States Posted: Jul. 30, 2015 Weekly Hours: 40.00 **Job Summary** Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Merchant Operations Manager: *Apple* Pay -...
Changing the world is all in a day's work at Apple . If you love innovation, here's your chance to make a career of it. You'll work hard. But the job comes with more than Read more
*Apple* Pay QA Manager - Apple Inc. (United...
Changing the world is all in a day's work at Apple . If you love innovation, here's your chance to make a career of it. You'll work hard. But the job comes with more than Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.