home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Internet / SRC Checker < prev   
Encoding:
Text File  |  1999-03-04  |  7.4 KB  |  313 lines  |  [TEXT/ToyS]

  1. -- Preferences
  2. property kasPrefName : "Width / Height Removal"
  3.  
  4. property kasTypesToDo : {"TEXT"} -- Only run on these types
  5. property kasExtToDo : "" -- Use non-empty match string (e.g. "*.html") to limit to that extension
  6.  
  7. -- Globals
  8. global gasInfoWind -- Info window
  9. global gasInfoPos -- Position of info window
  10. global gasFoldersToDo -- The folders left to process
  11. global gasConverted -- Number gone!
  12. global gasChecked -- Number checked!
  13. global gasImages -- Number images checked!
  14. global gasPrefix -- Adjusted kasPrefix (munge the % parms)
  15. global gasErrorFiles -- List of files with errors
  16. global gasErrorText -- Text list of errors
  17.  
  18.  
  19. on open fsObjs
  20.     -- Load prefs, show window
  21.     pfLoad()
  22.     
  23.     set gasConverted to 0
  24.     set gasChecked to 0
  25.     set gasImages to 0
  26.     set gasErrorFiles to {}
  27.     set gasErrorText to ""
  28.     
  29.     set gasInfoWind to display info titled kasPrefName ¬
  30.         located at gasInfoPos ¬
  31.         message "Scanning…"
  32.     
  33.     -- Do files
  34.     set gasFoldersToDo to {}
  35.     
  36.     repeat with fsObj in fsObjs
  37.         set myInfo to (basic info for fsObj)
  38.         
  39.         if (system type of myInfo is "fold") then
  40.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  41.         else if (system type of myInfo is in kasTypesToDo) then
  42.             if (kasExtToDo is "") or ((collect lines of (catalog name of myInfo) that match kasExtToDo) is not "") then
  43.                 DoOne(fsObj)
  44.             end if
  45.         end if
  46.     end repeat
  47.     
  48.     -- Do folders
  49.     repeat while gasFoldersToDo is not {}
  50.         -- Pop one off the end
  51.         set n to the number of items of gasFoldersToDo
  52.         set fsObj to item n of gasFoldersToDo
  53.         
  54.         if (n > 1) then
  55.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  56.         else
  57.             set gasFoldersToDo to {}
  58.         end if
  59.         
  60.         ShowToGo(n)
  61.         
  62.         -- Process it
  63.         GoDeep(fsObj)
  64.     end repeat
  65.     
  66.     display info gasInfoWind message "DONE!"
  67.     
  68.     pause for 2 with seconds timing -- Let screen wait...
  69.     
  70.     set gasInfoPos to screen location of ¬
  71.         (display info gasInfoWind with disposal)
  72.     
  73.     pfSave() -- Save window location
  74.     
  75.     if (gasErrorText is not "") then
  76.         clip gasErrorText
  77.         tell application "Note Pad" to activate
  78.         input state {keys down:"N", command key down:true} -- New note
  79.         input state {keys down:"V", command key down:true} -- Paste errors
  80.         activate
  81.     end if
  82.     
  83.     if (gasErrorFiles is not {}) then
  84.         display dialog "Open files with errors?" default button 2
  85.         tell application "Finder" to open gasErrorFiles
  86.     end if
  87. end open
  88.  
  89.  
  90. on DoOne(fsObj)
  91.     set aFileInfo to (alias info from fsObj)
  92.     ShowChecked(original name of aFileInfo)
  93.     ConvertFile(fsObj)
  94. end DoOne
  95.  
  96.  
  97. on ConvertFile(fsObj)
  98.     try
  99.         set fileData to ¬
  100.             read data from the data fork of fsObj
  101.     on error errStr
  102.         ShowError(fsObj, errStr)
  103.         return
  104.     end try
  105.     
  106.     set fname to catalog name of (basic info for fsObj)
  107.     set html to parse ML fileData
  108.     set sTags to collect items of html that match "%*src=*"
  109.     set vTags to collect items of html that match "%*virtual=*"
  110.     set fTags to collect items of html that match "%*file=*"
  111.     set uTags to collect items of html that match "%*value=*"
  112.     
  113.     if ShowSrcCnt(number of items of (sTags & vTags & fTags & uTags)) then
  114.         set changed to false
  115.         
  116.         -- Check tags!
  117.         repeat with imgTag in imgTags
  118.             set myParms to item 1 of (parse tag (item imgTag of html))
  119.             set oldW to 0
  120.             set oldH to 0
  121.             set hTags to (collect items of myParms that match "%HEIGHT=*")
  122.             set wTags to (collect items of myParms that match "%WIDTH=*")
  123.             
  124.             -- Got height or widht parms?
  125.             if ShowTagCnt(number of items of hTags, number of items of wTags) then
  126.                 -- Get oldW, oldH!
  127.                 if (hTags is not {}) then set oldH to parse tag val (item (item 1 of hTags) of myParms)
  128.                 if (wTags is not {}) then set oldW to parse tag val (item (item 1 of wTags) of myParms)
  129.                 -- Remove H/W tags from the parsed tag
  130.                 set myParms to (edit list myParms with edits hTags with removal of items)
  131.                 set myParms to (edit list myParms with edits wTags with removal of items)
  132.             end if
  133.             
  134.             -- Find our "SRC=" file…
  135.             set sTag to (collect items of myParms that match "%SRC=*")
  136.             if (sTag is {}) then
  137.                 ShowError(fsObj, "No SRC tag in " & fname)
  138.             else
  139.                 set sTag to item (item 1 of sTag) of myParms
  140.                 
  141.                 try
  142.                     set picFile to (resolve SRC tag sTag relative to file fsObj)
  143.                 on error
  144.                     ShowError(fsObj, "Can't find: " & sTag)
  145.                     set picFile to 0
  146.                 end try
  147.                 
  148.                 if (picFile is not 0) then
  149.                     ShowImageFile(picFile as string)
  150.                     -- Update the HTML
  151.                     set box to picture bounds of (the picture info for (the image from picFile))
  152.                     set w to (item 3 of box) - (item 1 of box)
  153.                     set h to (item 4 of box) - (item 2 of box)
  154.                     if (w is not oldW or h is not oldH) then
  155.                         set changed to true
  156.                         set w to "WIDTH=" & w
  157.                         set h to "HEIGHT=" & h
  158.                         set myParms to myParms & {w, h}
  159.                         set item imgTag of html to (compile tag myParms)
  160.                     end if
  161.                 end if
  162.             end if
  163.         end repeat
  164.         
  165.         -- Rename to lowercase?
  166.         if (changed) then
  167.             -- Write data
  168.             write data to the data fork of fsObj from buffer (compile ML html)
  169.             ShowConverted(fname)
  170.         end if
  171.     end if
  172. end ConvertFile
  173.  
  174.  
  175. on ShowImgCnt(cnt)
  176.     display info gasInfoWind ¬
  177.         message ("Images: " & cnt) ¬
  178.         at line 10
  179.     
  180.     return cnt > 0
  181. end ShowImgCnt
  182.  
  183.  
  184. on ShowTagCnt(cntW, cntH)
  185.     set n to cntW + cntH
  186.     display info gasInfoWind ¬
  187.         message ("Tags: " & n) ¬
  188.         at line 11
  189.     
  190.     return n > 0
  191. end ShowTagCnt
  192.  
  193.  
  194. on ShowError(fsObj, errStr)
  195.     set gasErrorFiles to gasErrorFiles & fsObj
  196.     set gasErrorText to gasErrorText & (fsObj as string) & " • " & errStr & return
  197.     
  198.     display info gasInfoWind ¬
  199.         message ("Error: " & errStr) ¬
  200.         at line 20 ¬
  201.         using color (25 * 1024)
  202. end ShowError
  203.  
  204.  
  205. on ShowConverted(fname)
  206.     set gasConverted to gasConverted + 1
  207.     
  208.     display info gasInfoWind ¬
  209.         message ("Converted: " & gasConverted) ¬
  210.         at line 8 ¬
  211.         using color (15 * 1024)
  212.     
  213.     display info gasInfoWind ¬
  214.         message ("Last: " & fname) ¬
  215.         at line 9 ¬
  216.         using color (16 * 1024)
  217. end ShowConverted
  218.  
  219.  
  220. on ShowChecked(fname)
  221.     set gasChecked to gasChecked + 1
  222.     
  223.     display info gasInfoWind ¬
  224.         message ("File: " & fname) ¬
  225.         at line 6
  226.     
  227.     display info gasInfoWind ¬
  228.         message ("Checked: " & gasChecked) ¬
  229.         at line 7 ¬
  230.         using color 15
  231. end ShowChecked
  232.  
  233.  
  234. on ShowImageFile(fname)
  235.     set gasImages to gasImages + 1
  236.     
  237.     display info gasInfoWind ¬
  238.         message ("Image: " & fname) ¬
  239.         at line 15
  240.     
  241.     display info gasInfoWind ¬
  242.         message ("Images: " & gasImages) ¬
  243.         at line 16
  244. end ShowImageFile
  245.  
  246.  
  247. on ShowAction(msg)
  248.     display info gasInfoWind ¬
  249.         message msg ¬
  250.         at line 2 ¬
  251.         using color (15 * 1024 + 15)
  252. end ShowAction
  253.  
  254.  
  255. on ShowToGo(n)
  256.     display info gasInfoWind ¬
  257.         message ("Folders to go: " & n) ¬
  258.         at line 14 ¬
  259.         using color (15 * 32)
  260. end ShowToGo
  261.  
  262.  
  263. on GoDeep(foldObj)
  264.     display info gasInfoWind ¬
  265.         message "Path: " & (foldObj as string)
  266.     
  267.     set daddy to foldObj as string
  268.     
  269.     -- Do kinds that match
  270.     ShowAction("Scanning files…")
  271.     
  272.     if (kasExtToDo is "") then
  273.         set myItems to the entries in foldObj ¬
  274.             whose types are in kasTypesToDo
  275.     else
  276.         set myItems to the entries in foldObj ¬
  277.             whose types are in kasTypesToDo ¬
  278.             whose names match kasExtToDo
  279.     end if
  280.     
  281.     repeat with myItem in myItems
  282.         DoOne((daddy & myItem) as alias)
  283.     end repeat
  284.     
  285.     -- Do folders
  286.     ShowAction("Scanning subfolders")
  287.     
  288.     set myItems to the entries in foldObj ¬
  289.         whose kinds are a folder
  290.     
  291.     repeat with myItem in myItems
  292.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  293.     end repeat
  294.     
  295.     -- Done
  296.     ShowAction("…")
  297. end GoDeep
  298.  
  299.  
  300. on pfLoad()
  301.     try
  302.         set ourPrefs to (load preference named kasPrefName)
  303.         set gasInfoPos to item 1 of ourPrefs
  304.     on error
  305.         set gasInfoPos to {-1, -1}
  306.     end try
  307. end pfLoad
  308.  
  309.  
  310. on pfSave()
  311.     save preference {gasInfoPos} named kasPrefName
  312. end pfSave
  313.