home *** CD-ROM | disk | FTP | other *** search
/ Internet File Formats / InternetFileFormatsCD.bin / text / latex / mac / alpha60.hqx / Tcl / SystemCode / appleEvents.tcl < prev    next >
Encoding:
Text File  |  1995-06-11  |  4.8 KB  |  167 lines

  1.  
  2. # make alias list to pass to AEBuild
  3. proc makeAlis {name} {
  4.     return "\[alis(╟[coerce TEXT $name -x alis]╚)\]"    
  5. }
  6.  
  7. proc makeAlises {args} {
  8.     set str "\["
  9.     set sep ""
  10.     foreach name $args {
  11.         append str "${sep}alis(╟[coerce TEXT $name -x alis]╚)"
  12.         set sep ","
  13.     }
  14.     append str "\]"
  15.     return $str
  16. }
  17.  
  18. # Queued replies are passed through AEPrint and then to this routine.
  19. proc handleReply {rep} {
  20.     global ALPHA lastReply
  21. #    switchTo $ALPHA
  22.     set lastReply $rep
  23. }
  24.  
  25. # Return an object record specifying the desired think project file.
  26. proc fileObject {name} {
  27.     join [concat {obj\{want:type('SFIL'), from:'null'(), form:'name', seld:╥} [file tail $name] {╙\}}] ""
  28. }
  29.  
  30. proc sendOpenEvent {filler appname fname} {
  31.     if {$filler == "noReply"} {
  32.         AEBuild $appname aevt odoc "----" [makeAlis $fname]
  33.     } else {
  34.         AEBuild -r $appname aevt odoc "----" [makeAlis $fname]
  35.     }
  36. }
  37.  
  38. # Send multiple open events
  39. proc sendOpenEvents {appname args} {
  40.     AEBuild -r $appname aevt odoc "----" [eval makeAlises $args]
  41. }
  42.  
  43. #================================================================================
  44. # General Apple Event handling routines
  45. #
  46. # (written by Tom Pollard for use in the MacPerl package)
  47. #================================================================================
  48.  
  49. # Quit an application.
  50. proc sendQuitEvent {appname} {
  51.     AEBuild $appname "aevt" "quit" 
  52. }
  53.  
  54. # Close one of an application's windows, designated by number.
  55. proc sendCloseWinNum {appname num} {
  56.     AEBuild $appname "core" "clos" "----" [AEWinByPos $num]
  57. }
  58.  
  59. # Close one of an application's windows, designated by name.
  60. proc sendCloseWinName {appname name} {
  61.     AEBuild $appname "core" "clos" "----" [AEWinByName $name]
  62. }
  63.  
  64. # Obtain the number of lines in one of an application's
  65. # windows, designated by name.
  66. proc sendCountLines {appname name} {
  67.     set winObj [AEWinByName $name]
  68.     set res [AEBuild -r $appname "core" "cnte" "----" $winObj kocl type('clin')]    
  69.     if {[regexp {:(.*)\}} $res allofit nlines]} {
  70.         return $nlines
  71.     } else {
  72.         return 0
  73.     }
  74. }
  75.  
  76. # Get a selected range of lines from one of an application's
  77. # windows, designated by name.  If $last is missing, then a single
  78. # line is returned; if both $first and $last are missing, then
  79. # the complete window contents are returned.
  80. proc sendGetText {appname name {first {missing}} {last {missing}}} {
  81.     global ALPHA
  82.     set winObj [AEWinByName $name]
  83.     if {$first != "missing"} {
  84.         if {$last != "missing"} {
  85.             set rangDesc [AELineRange $first $last]
  86.         } else {
  87.             set rangDesc [AEAbsPos $first]
  88.         }
  89.         set objDesc "obj{want:type('clin'), from:$winObj, $rangDesc }"
  90.     } else {
  91.         set objDesc "obj{want:type('ctxt'), from:$winObj, form:'indx', seld:abso('all') }"
  92.     }
  93.     set res [AEBuild -r $appname "core" "getd" "----" $objDesc]    
  94.     if {![regexp {╥.*╙} $res text]} { set text {} }
  95.     return [string trim $text {╥╙}]
  96. }
  97.  
  98. # Set a selected range of lines in one of an application's
  99. # windows, designated by name.  If $last is missing, then a single
  100. # line is changed; if both $first and $last are missing, then
  101. # the complete window contents are replaced by the new text.
  102. proc sendSetText {appname name text {first {missing}} {last {missing}}} {
  103.     set winObj [AEWinByName $name]
  104.     if {$first != "missing"} {
  105.         if {$last != "missing"} {
  106.             set rangDesc [AELineRange $first $last]
  107.         } else {
  108.             set rangDesc [AEAbsPos $first]
  109.         }
  110.         set objDesc "obj{want:type('clin'), from:$winObj, $rangDesc }"
  111.     } else {
  112.         set objDesc "obj{want:type('ctxt'), from:$winObj, form:'indx', seld:abso('all') }"
  113.     }
  114.     set res [AEBuild -r $appname "core" "setd" "----" $objDesc "data" [curlyq $text]]    
  115.     if {![regexp {╥.*╙} $res text]} { set text {} }
  116.     return [string trim $text {╥╙}]
  117. }
  118.  
  119. ################################################################################
  120. # Utility functions for constructing AppleEvent descriptors for AEBuild
  121. ################################################################################
  122.  
  123. proc AEFilename {name} {
  124.     return "obj{want:type('file'), from:'null'(), [AEName $name] } "
  125. }
  126.  
  127. proc AEWinByName {name} {
  128.     return "obj{want:type('cwin'), from:'null'(), [AEName $name] } "
  129. }
  130.  
  131. proc AEWinByPos {absPos} {
  132.     return "obj{want:type('cwin'), from:'null'(), [AEAbsPos $absPos] } "
  133. }
  134.  
  135. proc AELineRange {absPos1 absPos2} {
  136.     set lineObj1 "obj{ want:type('clin'), from:'ccnt'(), [AEAbsPos $absPos1] }"
  137.     set lineObj2 "obj{ want:type('clin'), from:'ccnt'(), [AEAbsPos $absPos2] }"
  138.     return "form:'rang', seld:rang{star:$lineObj1, stop:$lineObj2 } "
  139. }
  140.  
  141. proc AEAbsPos {posName} {
  142. #
  143. # Use '1' or 'first' to specify first position
  144. # and '-1' or 'last' to specify last position.
  145. #
  146.     if {$posName == "first"} { 
  147.         set posName 1 
  148.     } elseif {$posName == "last"} { 
  149.         set posName -1 
  150.     }
  151.     if {$posName >= -1} {
  152.         return "form:indx, seld:long($posName)"
  153.     } else {
  154.         error "AEAbsPos: bad argument"
  155.     }
  156. }
  157.  
  158. proc AEName {name} {
  159.     return "form:'name', seld:[curlyq $name]"
  160. }
  161.  
  162. proc curlyq {str} {
  163.     return "\╥$str\╙"
  164. }
  165.  
  166. ################################################################################
  167.