home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_SML / as_examples.sml next >
Encoding:
Text File  |  1997-07-28  |  4.2 KB  |  156 lines  |  [TEXT/Moml]

  1. (* as_examples *)
  2. (* 1997Jul27 e *)
  3.  
  4. (* example 1: using the Process.system() call to send mail *)
  5.  
  6. load "Process";
  7.  
  8. val mail_appl_name = ref "\"Eudora Pro 3.1.1\"";
  9.  
  10. fun send_mail to subj msg =
  11.   Process.system(
  12.     "tell application " ^ !mail_appl_name ^
  13.     "\n make message at end of mailbox \"Out\" of mail folder \"\"" ^
  14.     "\n set field \"to\" of message 0 to \"" ^ to ^ "\"" ^
  15.     "\n set field \"subject\" of message 0 to \"" ^ subj ^ "\"" ^
  16.     "\n set field \"\" of message 0 to \"" ^ msg ^ "\"" ^
  17.     "\n queue message 0\nend tell"
  18.   );
  19.  
  20. (*
  21. send_mail "e@flavors.com" "testing system()" "Hi. I'm testing mosml142.";
  22. *)
  23.  
  24. (* example 2: using the Process.system() call to present dialogs *)
  25.  
  26. load "Process";
  27.  
  28. Process.system(
  29. "display dialog \"My Dialog\" with icon stop default answer \"Right!\"");
  30.  
  31. (* 
  32. display dialog  anything  -- title of dialog
  33. [default answer  anything]  -- default editable text
  34. [buttons  list]  -- list of up to three buttons
  35. [default button  anything]  -- name or number of default button
  36. [with icon  anything]  -- name or id of the icon to display
  37. [with icon  stop/note/caution]  -- display one of these system icons
  38. Result:   reply  -- record containing the button hit and text entered (if any)
  39. *)
  40.  
  41. (* example 3: using AppleScript to present dialogs & get results *)
  42.  
  43. load "AppleScript";
  44.  
  45. val dlg = AppleScript.as_compile(
  46. "display dialog \"My Dialog\" with icon stop default answer \"Right!\"");
  47.  
  48. AppleScript.as_run_script dlg;
  49.  
  50. (* example 3: AppleScript compile error *)
  51.  
  52. load "AppleScript";
  53. load "Int";
  54.  
  55. val bogusOSAID = AppleScript.as_compile "";
  56.  
  57. val dlg2 = AppleScript.as_compile(
  58. "display dialog \"My Dialog\" with icon note buttons \"Yes\", \"No\"" ^
  59. "default answer \"Right!\"")
  60. handle AppleScript.AppleScriptErr (n,s) =>
  61.     ( print (Int.toString n ^ " " ^ s); bogusOSAID);
  62.  
  63. AppleScript.as_run_script dlg2
  64.  handle AppleScript.AppleScriptErr (n,s) => (Int.toString n ^ s);
  65.  
  66. (* example 4: AppleScript compile error corrected; dialog with buttons *)
  67.  
  68. load "AppleScript";
  69.  
  70. val dlg2 = AppleScript.as_compile(
  71. "display dialog \"My Dialog\" with icon note buttons {\"Yes\", \"No\"}" ^
  72. "default answer \"Right!\"");
  73.  
  74. AppleScript.as_run_script dlg2
  75.  handle AppleScript.AppleScriptErr (n,s) => (Int.toString n ^ s);
  76.  
  77. (* example 5: Speech provided with Scripting Tools *)
  78.  
  79. load "Process";
  80. load "AppleScript";
  81.  
  82. Process.system( "speak \"Good night Irene.\"" );
  83.  
  84. fun speak what =
  85.   AppleScript.as_run_text ( "speak \"" ^ what ^ "\"")
  86. ;
  87.  
  88. speak "My name is Tobar Radar Robot.";
  89. speak "My name is Toe bar Ray dar Ro bot.";
  90. speak "Hello, Megan and Doug!";
  91. speak "Good night Irene.";
  92.  
  93. fun speak_with voice what =
  94.   AppleScript.as_run_text ( "speak \"" ^ what ^ "\" voice \"" ^ voice ^ "\"")
  95. ;
  96.  
  97. (* example 6: using return results, with Speech *)
  98.  
  99. load "AppleScript";
  100. load "Substring";
  101.  
  102. AppleScript.as_run_text "list voices";
  103.  
  104. (* handy function to convert AS text list result to ml str list *)
  105.  
  106. fun as_list_elements s =
  107.   let open Substring
  108.       val ss = all s
  109.       val ln = size ss
  110.       val s1 = dropl (fn c => c = #"{") ss
  111.       val s2 = dropl (fn c => c = #"\"") s1
  112.       val s3 = dropr (fn c => c = #"}") s2
  113.       fun strrev [] r = r
  114.         | strrev (h::t) r = strrev t ((string h)::r)
  115.       fun lsd s =
  116.         dropl (fn c => c = #"\"" orelse c = #" " orelse c = #",") s
  117.       fun els s r =
  118.         let val e = takel (fn c => c <> #"\"") s
  119.             val z = size e
  120.         in if z > 0
  121.            then els (lsd (triml z s)) (e :: r)
  122.            else strrev r []
  123.         end
  124.   in if ln = (size s3 + 3)
  125.      then els s3 [] 
  126.      else []
  127.   end
  128. ;
  129.  
  130. fun speak_all_voices what = 
  131.   let val voice_list = as_list_elements (AppleScript.as_run_text "list voices")
  132.       fun spk voice = (speak voice; (* say the voice name *)
  133.                        speak_with voice what; ())
  134.   in app spk voice_list
  135.      handle AppleScript.AppleScriptErr (n,s) =>
  136.       print (Int.toString n ^ " " ^ s); ()
  137.   end
  138. ;
  139.  
  140. speak_all_voices "Hello, Megan and Doug!";
  141.  
  142. (* example 7: miscellaneous stuff *)
  143.  
  144. load "AppleScript";
  145.  
  146. AppleScript.as_run_text( "(scripting additions folder) as text");
  147.  
  148. AppleScript.as_run_text
  149.  "(((scripting additions folder) as text) & \"Speech\")";
  150.  
  151. AppleScript.as_run_text
  152. "list terminology file (((scripting additions folder) as text) & \"Speech\")";
  153.  
  154. (* *)
  155.  
  156.