home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Programming / Dialog Director 0.6 / Dialog Dumper / Dialog Dumper.as < prev    next >
Encoding:
Text File  |  1997-04-17  |  15.0 KB  |  538 lines  |  [TEXT/ToyS]

  1. property dBounds : null
  2. property dToClipboard : true
  3. property dOutFileName : "DD.Out.as"
  4. property dAuto : true
  5. property dGreyscale : true
  6. property dDoTimeout : false
  7. property dTimeout : 10
  8. property dDoFontName : false
  9. property dFontName : "Chicago"
  10. property dDoFontSize : false
  11. property dFontSize : 12
  12. property dDoFontStyle : false
  13. property dFontStyle : ""
  14. property debugDiDum : false
  15.  
  16. --open [alias "Ram:Test:Dialog Dumper.rsrc"]
  17. open [choose file with prompt "Choose a dialog resource file:" of type "rsrc"]
  18.  
  19. on open srcFile
  20.     global rf, fileName, thePushButtons, itemNum
  21.     set rf to null
  22.     CheckForOsaxen()
  23.     if not OptionsDialog() then return
  24.     OpenOutput()
  25.     set srcFile to item 1 of srcFile
  26.     set rf to res open srcFile
  27.     
  28.     try
  29.         set AppleScript's text item delimiters to [":"]
  30.         set fileName to last text item of (srcFile as string)
  31.         set AppleScript's text item delimiters to ""
  32.         set rDlogCount to res count rf type "DLOG"
  33.         if rDlogCount = 0 then ¬
  34.             Fail("The file “" & fileName & "” has no 'DLOG' resources!")
  35.         PutLn("-- " & fileName)
  36.         repeat with n from 1 to rDlogCount
  37.             set dInfo to res get info rf type "DLOG" index n
  38.             if length of dInfo's res name ≠ 0 then
  39.                 set dName to "d" & dInfo's res name
  40.             else
  41.                 set dName to "dlog" & dInfo's res id
  42.             end if
  43.             Put1("set " & dName & " to {")
  44.             
  45.             --
  46.             --    the dialog
  47.             --
  48.             set d to res get rf type "DLOG" index n
  49.             set dlog to cast d using template "DLOG" with label
  50.             set R to dlog's boundsrect
  51.             if item 1 of R = 0 and item 2 of R = 0 then
  52.                 Put1("size:[" & R's item 4 & ", " & R's item 3 & "]")
  53.             else
  54.                 Put1("bounds:" & RectToString(R))
  55.             end if
  56.             if dAuto and dDoTimeout then PutLn(", timeout after:" & dTimeout & " ¬")
  57.             if length of dlog's title ≠ 0 then Put1(", name:\"" & dlog's title & "\"")
  58.             PutWindowStyle(dlog's procid)
  59.             
  60.             --
  61.             --    the items
  62.             --
  63.             set d to GetRes("DITL", dlog's items_id)
  64.             set ditl to cast d using template "DITL" with label
  65.             PutLn(", contents:[ ¬")
  66.             set thePushButtons to []
  67.             set itemNum to 0
  68.             set i to 1
  69.             repeat
  70.                 set i to PutDialogItem(ditl, i)
  71.                 if i ≤ length of ditl then
  72.                     PutLn("}, ¬")
  73.                 else
  74.                     PutLn("} ¬")
  75.                     exit repeat
  76.                 end if
  77.             end repeat
  78.             PutLn("    ] ¬")
  79.             
  80.             --
  81.             --    the function calls
  82.             --
  83.             PutLn("}" & return)
  84.             if dAuto then
  85.                 Put1("dd auto dialog " & dName)
  86.             else
  87.                 Put1("dd install")
  88.             end if
  89.             if dDoFontName or dDoFontSize or dDoFontStyle then
  90.                 Put1(" font {")
  91.                 if dDoFontName then Put1("name:\"" & dFontName & "\"")
  92.                 if dDoFontSize then
  93.                     if dDoFontName then Put1(", ")
  94.                     Put1("size:" & dFontSize)
  95.                 end if
  96.                 if dDoFontStyle then
  97.                     if dDoFontName or dDoFontSize then Put1(", ")
  98.                     Put1("style:" & dFontStyle)
  99.                 end if
  100.                 Put1("}")
  101.             end if
  102.             if dGreyscale then Put1(" with greyscale")
  103.             Put1(return)
  104.             if not dAuto then
  105.                 PutLn("set d1 to dd make dialog " & dName)
  106.                 PutLn("repeat")
  107.                 PutLn("    set dItem to dd interact with user")
  108.                 if length of thePushButtons = 0 then
  109.                     PutLn("    if dItem ≠ null then exit")
  110.                 else
  111.                     set s to "dItem = " & item 1 of thePushButtons
  112.                     if length of thePushButtons > 1 then
  113.                         repeat with i from 2 to length of thePushButtons
  114.                             set s to s & " or dItem = " & item i of thePushButtons
  115.                         end repeat
  116.                     end if
  117.                     PutLn("    if " & s & " then exit")
  118.                 end if
  119.                 PutLn("end")
  120.                 PutLn("dd uninstall")
  121.             end if
  122.             PutLn(return)
  123.             --exit repeat -- *** Debugging ***
  124.         end repeat
  125.     on error errText number errNum from offendingObject partial result resultList to expectedType
  126.         if debugDiDum then -- Propergate errors when debugging 
  127.             res close rf
  128.             CloseOutput()
  129.             error errText number errNum from offendingObject partial result resultList to expectedType
  130.         else
  131.             Fail("Error number " & errNum)
  132.         end if
  133.     end try
  134.     
  135.     res close rf
  136.     CloseOutput()
  137. end open
  138.  
  139.  
  140. --
  141. --    Functions to output the dialog item descriptions
  142. --
  143.  
  144. on PutClassAndBounds(t, d)
  145.     Put1("        {class:" & t & ", bounds:" & RectToString(d's display_rect))
  146. end PutClassAndBounds
  147.  
  148. on PutCntlClassAndBounds(t, d, c)
  149.     set R to d's display_rect
  150.     set y to R's item 1
  151.     set x to R's item 2
  152.     set R to c's boundsrect
  153.     set h to (R's item 3) - (R's item 1)
  154.     set w to (R's item 4) - (R's item 2)
  155.     Put1("        {class:" & t & ", bounds:[" & x & ", " & y & ", " & x + w & ", " & y + h & "]")
  156. end PutCntlClassAndBounds
  157.  
  158. on PutCheckbox(d)
  159.     PutClassAndBounds("check box", d)
  160.     Put1(", name:\"" & d's item_info & "\"")
  161. end PutCheckbox
  162.  
  163. on PutPushButton(d)
  164.     PutClassAndBounds("push button", d)
  165.     Put1(", name:\"" & d's item_info & "\"")
  166. end PutPushButton
  167.  
  168. on PutRadioButton(ditl, i) -- Merges adjacent radio buttons into a single radio group
  169.     set d to item i of ditl
  170.     set r0 to d's display_rect
  171.     set x0 to item 2 of r0
  172.     set y0 to item 1 of r0
  173.     set w to (item 4 of r0) - x0
  174.     set dx to 0
  175.     set dy to 0
  176.     set maxDown to ""
  177.     set n to "\"" & d's item_info & "\""
  178.     set i0 to i
  179.     repeat while i < length of ditl
  180.         set d to item (i + 1) of ditl
  181.         if (d's item_type) mod 128 ≠ 6 then exit repeat
  182.         set R to d's display_rect
  183.         set x to item 2 of R
  184.         set y to item 1 of R
  185.         if (x - x0) ≠ 0 then
  186.             if dx = 0 and dy ≠ 0 then set maxDown to ", max down:" & 1 + i - i0
  187.             set dx to x - x0
  188.         end if
  189.         set dy to y - y0
  190.         set n to n & ", \"" & d's item_info & "\""
  191.         set x0 to x
  192.         set y0 to y
  193.         if w < (item 4 of R) - x then ¬
  194.             set w to (item 4 of R) - x
  195.         set i to i + 1
  196.     end repeat
  197.     set item 4 of r0 to (item 2 of r0) + w
  198.     PutClassAndBounds("radio group", {display_rect:r0})
  199.     Put1(", button offset:[" & dx & ", " & dy & "]" & maxDown & ", contents:[" & n & "]")
  200.     return i + 1
  201. end PutRadioButton
  202.  
  203. on PutPopUp(c, cntl, varCode)
  204.     set n to c's refcon
  205.     if length of c's title ≠ 0 then
  206.         Put1(", name:\"" & c's title & "\"")
  207.         if c's max ≠ 0 then ¬
  208.             Put1(", name width:" & c's max)
  209.     else if n ≠ 0 and varCode mod 8 < 4 then --        * Type-In field *
  210.         Put1(", text field:" & n)
  211.     end if
  212.     if c's |value| ≠ 0 then ¬
  213.         Put1(", value:" & c's |value|)
  214.     
  215.     if n ≠ 0 and varCode mod 8 ≥ 4 then --            * Resource list Pop-Up *
  216.         Put1(", contents:\"" & (cast cntl from 19 length 4 to string) & "\"")
  217.     else
  218.         set mu to GetRes("MENU", c's min)
  219.         set m to cast mu using template "MENU" with label
  220.         set s to ""
  221.         repeat with i in m's |menu items|
  222.             if s = "" then
  223.                 set s to "\"" & i's menuitem & "\""
  224.             else
  225.                 set s to s & ", \"" & i's menuitem & "\""
  226.             end if
  227.         end repeat
  228.         Put1(", contents:[" & s & "]")
  229.     end if
  230. end PutPopUp
  231.  
  232. on PutControl(d)
  233.     set cntl to GetRes("CNTL", ItemInfoToNum(d))
  234.     set c to cast cntl using template "CNTL" with label
  235.     set procid to (c's procid)
  236.     set cType to procid div 16
  237.     if cType = 25 or cType = 63 then --                *** Pop-Up ***
  238.         PutCntlClassAndBounds("pop up", d, c)
  239.         PutPopUp(c, cntl, procid mod 16)
  240.         --set cntl to [c, m]
  241.     else if cType = 22 then --                        *** List Box ***
  242.         PutCntlClassAndBounds("list box", d, c)
  243.         if c's |value| ≠ 0 then ¬
  244.             Put1(", value:" & c's |value|)
  245.         Put1(", contents:[" & "\"•••\"" & "]")
  246.     else if cType = 5 then --                          *** Gauge ***
  247.         PutCntlClassAndBounds("gauge", d, c)
  248.         if c's |value| ≠ 0 then ¬
  249.             Put1(", value:" & c's |value|)
  250.         Put1(", max value:" & (c's max) - (c's min))
  251.     else if cType = 10 then --                        *** Group Box ***
  252.         if length of c's title ≠ 0 then -- Adjust bounds
  253.             tell d's display_rect to set item 1 to (item 1) + 11
  254.             tell c's boundsrect to set item 3 to (item 3) - 11
  255.         end if
  256.         PutCntlClassAndBounds("group box", d, c)
  257.         if length of c's title ≠ 0 then ¬
  258.             Put1(", name:\" " & c's title & " \"")
  259.         if procid ≥ 164 then ¬
  260.             Put1(", style:secondary group")
  261.     else
  262.         PutUnknown(d)
  263.     end if
  264. end PutControl
  265.  
  266. on PutTextField(d)
  267.     PutClassAndBounds("text field", d)
  268.     if length of d's item_info ≠ 0 then ¬
  269.         Put1(", value:\"" & d's item_info & "\"")
  270. end PutTextField
  271.  
  272. on PutStaticText(d)
  273.     PutClassAndBounds("static text", d)
  274.     Put1(", contents:\"" & d's item_info & "\"")
  275. end PutStaticText
  276.  
  277. on PutIcon(d)
  278.     PutClassAndBounds("icon", d)
  279.     Put1(", contents:" & ItemInfoToNum(d))
  280. end PutIcon
  281.  
  282. on PutPict(d)
  283.     PutClassAndBounds("pict", d)
  284.     Put1(", contents:" & ItemInfoToNum(d))
  285. end PutPict
  286.  
  287. on PutUnknown(d)
  288.     PutClassAndBounds("UNKNOWN_" & d's item_type, d)
  289.     Put1(", name:\"" & d's item_info & "\"")
  290. end PutUnknown
  291.  
  292. on PutDialogItem(ditl, i)
  293.     global thePushButtons, itemNum
  294.     set itemNum to itemNum + 1
  295.     --PutLn("Item: " & i)
  296.     set d to item i of ditl
  297.     set c to (d's item_type) mod 128
  298.     --if c <4 then set c to c +4
  299.     if c = 0 then -- User item
  300.         PutClassAndBounds("group box", d) --PutGroupBox(d)
  301.     else if c = 4 then
  302.         set thePushButtons to thePushButtons & itemNum
  303.         PutPushButton(d)
  304.     else if c = 5 then
  305.         PutCheckbox(d)
  306.     else if c = 6 then
  307.         return PutRadioButton(ditl, i)
  308.     else if c = 7 then
  309.         PutControl(d)
  310.     else if c = 8 then
  311.         PutStaticText(d)
  312.     else if c = 16 then
  313.         PutTextField(d)
  314.     else if c = 32 then
  315.         PutIcon(d)
  316.     else if c = 64 then
  317.         PutPict(d)
  318.     else
  319.         PutUnknown(d)
  320.     end if
  321.     return i + 1
  322. end PutDialogItem
  323.  
  324.  
  325. --
  326. --    Functions to convert the resource data
  327. --
  328.  
  329. property winIDs : [0, "1", 2, 3, 4, 5, 8, 12, 16, 1985, 1987, 1989, 1991, 1993, 1995, 1997, 1999]
  330. property winStyles : ["document window", "modal", "plain dialog", ¬
  331.     "plain dialog", "document window", "movable modal", "document window", ¬
  332.     "document window", "document window", "palette", "palette", "palette", "palette", ¬
  333.     "sideways palette", "sideways palette", "sideways palette", "sideways palette"]
  334.  
  335. on PutWindowStyle(n)
  336.     repeat with i from 1 to length of winIDs
  337.         if n = item i of winIDs then
  338.             Put1(", style:" & item i of winStyles)
  339.             exit repeat
  340.         end if
  341.     end repeat
  342. end PutWindowStyle
  343.  
  344. on RectToString(R)
  345.     tell R to return "[" & item 2 & ", " & item 1 & ", " & item 4 & ", " & item 3 & "]"
  346. end RectToString
  347.  
  348. on ItemInfoToNum(d)
  349.     set n to d's item_info & "??"
  350.     return (ASCII number n) * 256 + (ASCII number character 2 of n)
  351. end ItemInfoToNum
  352.  
  353. on GetRes(rType, rID)
  354.     global rf
  355.     try
  356.         return res get rf type rType id rID
  357.     on error
  358.         MissingRes(rType, rID)
  359.     end try
  360. end GetRes
  361.  
  362.  
  363. --
  364. --    The user interface
  365. --
  366.  
  367. on OptionsDialog()
  368.     if dBounds = null then set dBounds to dd calc dialog bounds [320, 294]
  369.     if dAuto then
  370.         set dMode to 1
  371.     else
  372.         set dMode to 2
  373.     end if
  374.     set dDDumper to {bounds:dBounds, style:movable modal, name:"Dialog Dumper", contents:[¬
  375.         {class:push button, bounds:[10, 263, 100, 283], name:"To Clipboard"}, ¬
  376.         {class:push button, bounds:[220, 263, 310, 283], name:"Cancel"}, ¬
  377.         {class:push button, bounds:[115, 263, 205, 283], name:"To File…"}, ¬
  378.         {class:group box, bounds:[10, 15, 310, 94], name:" Options "}, ¬
  379.         {class:radio group, bounds:[66, 23, 126, 41], button offset:[70, 0], contents:["Auto", "Live"], value:dMode}, ¬
  380.         {class:check box, bounds:[16, 43, 156, 61], name:"Greyscale", value:dGreyscale}, ¬
  381.         {class:check box, bounds:[16, 63, 126, 81], name:"Timeout after", value:dDoTimeout, enabled:261}, ¬
  382.         {class:text field, bounds:[132, 64, 172, 80], value:dTimeout, enabled:[dAnd, 7, 261]}, ¬
  383.         {class:static text, bounds:[177, 64, 217, 80], contents:"secs"}, ¬
  384.         {class:check box, bounds:[16, 126, 70, 144], name:"Font:", value:dDoFontName}, ¬
  385.         {class:pop up, bounds:[72, 126, 282, 146], contents:"FONT", value:dFontName, enabled:10}, ¬
  386.         {class:check box, bounds:[16, 152, 70, 170], name:"Size:", value:dDoFontSize}, ¬
  387.         {class:text field, bounds:[75, 153, 115, 169], value:dFontSize, enabled:12}, ¬
  388.         {class:pop up, bounds:[120, 152, 144, 172], text field:13, contents:["9;10;12;14;18;24;2;36"], enabled:12}, ¬
  389.         {class:group box, bounds:[150, 159, 278, 244], name:"         ", style:secondary group}, ¬
  390.         {class:check box, bounds:[160, 150, 240, 168], name:"Style", value:dDoFontStyle}, ¬
  391.         {class:check box, bounds:[172, 168, 268, 186], name:"Bold", value:IsStyle("bold"), enabled:16}, ¬
  392.         {class:check box, bounds:[172, 186, 268, 204], name:"Italic", value:IsStyle("italic"), enabled:16}, ¬
  393.         {class:check box, bounds:[172, 204, 268, 222], name:"Underline", value:IsStyle("underline"), enabled:16}, ¬
  394.         {class:check box, bounds:[172, 222, 268, 240], name:"Outline", value:IsStyle("outline"), enabled:16}, ¬
  395.         {class:icon, bounds:[270, 22, 302, 54], contents:200}, ¬
  396.         {class:static text, bounds:[16, 24, 66, 40], contents:"Mode:"}, ¬
  397.         {class:group box, bounds:[10, 115, 310, 252], name:" Default Font "} ¬
  398.             ] ¬
  399.         }
  400.     
  401.     set dVals to dd auto dialog dDDumper with greyscale
  402.     tell dVals
  403.         if item 2 then return false
  404.         set dBounds to last item
  405.         set dToClipboard to item 1
  406.         set dAuto to item 5 = 1
  407.         set dGreyscale to item 6
  408.         set dDoTimeout to item 7
  409.         set dTimeout to item 8 as integer
  410.         set dDoFontName to item 10
  411.         set dFontName to item 11
  412.         set dDoFontSize to item 12
  413.         set dFontSize to item 13 as integer
  414.         set dDoFontStyle to item 16
  415.         set s to ""
  416.         if item 17 then set s to ", bold"
  417.         if item 18 then set s to s & ", italic"
  418.         if item 19 then set s to s & ", underline"
  419.         if item 20 then set s to s & ", outline"
  420.         if length of s = 0 then
  421.             set s to "plain"
  422.         else
  423.             set s to text 3 thru (length of s) of s
  424.             if s contains "," then set s to "[" & s & "]"
  425.         end if
  426.         set dFontStyle to s
  427.     end tell
  428.     return true
  429. end OptionsDialog
  430.  
  431. on IsStyle(s)
  432.     return dFontStyle contains s
  433. end IsStyle
  434.  
  435. --
  436. --    Functions to manage the script output
  437. --
  438.  
  439. on OpenOutput()
  440.     global gBuf
  441.     set gBuf to ""
  442. end OpenOutput
  443.  
  444. on CloseOutput()
  445.     global gBuf, gHasSF
  446.     --tell application "Transcript" to «event miscEcho» gBuf
  447.     if dToClipboard then -- Check the result from the options dialog
  448.         activate
  449.         set the clipboard to gBuf -- From Jon's Commands
  450.     else
  451.         set outFile to new file with prompt "Save script as:" default name dOutFileName
  452.         set f to open for access outFile with write permission
  453.         try
  454.             set eof f to 0
  455.             write gBuf to f
  456.             if gHasSF then ¬
  457.                 tell application "Finder" to set creator type of outFile to "ToyS"
  458.             set AppleScript's text item delimiters to [":"]
  459.             set dOutFileName to last text item of (outFile as string)
  460.             set AppleScript's text item delimiters to ""
  461.         on error
  462.             close access f
  463.             error
  464.         end try
  465.         close access f
  466.     end if
  467. end CloseOutput
  468.  
  469. on Put1(s)
  470.     global gBuf
  471.     set gBuf to gBuf & s
  472. end Put1
  473.  
  474. on PutLn(s)
  475.     global gBuf
  476.     set gBuf to gBuf & (s as string) & return
  477. end PutLn
  478.  
  479.  
  480. -- ---> osaxen checkers and error handling --->
  481.  
  482. on CheckForOsaxen()
  483.     global gHasSF
  484.     try -- Scriptable Finder
  485.         with timeout of 3 seconds
  486.             tell application "Finder" to exists
  487.         end timeout
  488.         set gHasSF to true
  489.     on error
  490.         set gHasSF to false
  491.     end try
  492.     
  493.     try -- Programmer’s Tool
  494.         alias ((path to extensions folder as string) & "Scripting Additions:programmer's tool")
  495.         cast 9 to string -- Could be in there, but no compile or restart done!
  496.     on error
  497.         MissingOsax("Programmer's Tool")
  498.     end try
  499.     
  500.     try -- Jon’s Commands
  501.         clipboard info for "DiDi"
  502.     on error
  503.         MissingOsax("Jon’s Commands")
  504.     end try
  505.     
  506.     try -- Resource Utilities
  507.         res unique id 0 type "ICON"
  508.     on error
  509.         MissingOsax("Resource Utilities")
  510.     end try
  511.     
  512.     try -- Dialog Director
  513.         dd count dialogs
  514.     on error
  515.         MissingOsax("Dialog Director v0.6")
  516.     end try
  517. end CheckForOsaxen
  518.  
  519. on MissingRes(rType, rID)
  520.     global fileName
  521.     Fail("The file “" & fileName & "” is missing the '" & rType & "' resource ID " & rID & ".")
  522. end MissingRes
  523.  
  524. on MissingOsax(n)
  525.     Fail("The “" & n & "” Scripting Addition is not installed on this computer.
  526.  
  527. Dialog Dumper requires this file to be able to run.")
  528. end MissingOsax
  529.  
  530. on Fail(msg)
  531.     global rf
  532.     if rf ≠ null then res close rf
  533.     beep
  534.     display dialog msg buttons "Quit" default button 1
  535.     quit
  536. end Fail
  537.  
  538. -- <--- osaxen checkers and error handling <---