Recursive Image Library Creation In Revolution

I use an icon collection that is a whole pile (around 2000) of png images stored in a hierarchy of folders (eg Objects and People/48×48/plain/). It has become annoying finding and importing these images one by one into my applications as needed so I wrote a little drag and drop plugin to import images into a stack whenever they are dropped. It works until you have an app your working on that uses drag and drop and then it fails so I thought I’d give the rev image library setup a go. Rather than import images into libraries by hand I decided to slap together a little recursive image library from folder script. Copy the code to a button then click it. The library name is built out of the names of the folders starting with the one you selected.

<code>on mouseUp
   local tLibraryFileA
   answer folder "Choose a folder:"
   if the result is "cancel" then exit mouseUp
   recursiveImageLibrary it,"",tLibraryFileA
   repeat for each line tLine in the keys of tLibraryFileA
      createImageLibrary tLine,tLibraryFileA[tLine]
   end repeat
end mouseUp

on recursiveImageLibrary pFolder,pLibraryName,@pLibraryFileA
   local tOldFolder
   put the defaultFolder into tOldFolder
   set the defaultFolder to pFolder
   set the itemDel to "/"
   if pLibraryName <> "" then put " " after pLibraryName
   put item -1 of pFolder after pLibraryName
   set the itemDel to "."
   repeat for each line tFile in the files
      if item -1 of tFile is among the items of "jpg.png.gif" then
         put pFolder&"/"&tFile&cr after pLibraryFileA[pLibraryName]
      end if
   end repeat
   repeat for each line tFolder in line 2 to -1 of the folders
      recursiveImageLibrary pFolder&"/"&tFolder,pLibraryName,pLibraryFileA
   end repeat
end recursiveImageLibrary

on createImageLibrary pLibraryName,pLibraryFiles
   local tHighID,tList
   -- create the stack
   put 230000 into tHighID
   if char 1 to 6 of pLibraryName is not "revLib" then put "revLib" before pLibraryName
   if there is a stack pLibraryName then
      answer error "A library with the same name ("&pLibraryName&") as this library already exists." with "Cancel"
      exit createImageLibrary
   end if
   put revAbsoluteFolderListing(revEnvironmentUserResourcesPath("Icon Libraries",true)) into tList
   put cr & "revGeneralIcons" & cr & "revCompatibilityIcons1" after tList
   repeat for each line l in tList
      if l is empty or there is not a stack l then next repeat
      if the name of stack l is pLibraryName then
         answer error "That library already exists." with "Cancel"
         exit createImageLibrary
      end if
      put the ID of stack l into item (the number of items in tHighID+1) of tHighID
   end repeat
   if there is a file (pLibraryName & ".rev") then
      answer error "A library with the name" && pLibraryName & ".rev" && "already exists." with "Cancel"
      exit createImageLibrary
   end if
   set the name of the templateStack to pLibraryName
   set the destroyStack of the templateStack to false
   lock messages
   set the style of the templateStack to "modeless"
   create invisible stack
   reset the templateStack
   set the mainStack of stack pLibraryName to pLibraryName
   set the ID of stack pLibraryName to (max(tHighID)+1)
   set the fileName of stack pLibraryName to revEnvironmentUserResourcesPath("Icon Libraries") & "/" & pLibraryName & ".rev"
   set the defaultStack to pLibraryName -- just to make sure
   -- now import all the images
   repeat for each line tImage in pLibraryFiles
      if there is a file tImage then
         if tImage is not empty then
            import paint from file tImage
         end if
      end if
   end repeat
   compact stack pLibraryName
   save stack pLibraryName
   close stack pLibraryName
   unlock messages
end createImageLibrary</code>

OK, once you are done there are two IDE mods that you should make.

If you had a big hierarchy of folders like me you will probably want to sort the image library list so choose View > Revolution UI Elements In Lists from the menu then open up the application browser and find the images card of the revImageLibrary stack and edit the script. Just after “delete last char of tStacksList” add “sort tStacksList”

Next for some reason way back in the depths of time the good people at RunRev forgot to upgrade the icon chooser to support image libraries. “What’s the point of image libraries without them being in the icon chooser?” I hear you ask. Very good question.

Replace the preOpenStack handler in revIconChooser card 1 with:

<code>on preOpenStack
   put revAbsoluteFolderListing(revEnvironmentResourcesPath("Icon Libraries")) into tEnvironmentIcons
   put revAbsoluteFolderListing(revEnvironmentUserResourcesPath("Icon Libraries")) into tUserIcons
   put revCombineFilePaths(tUserIcons,tEnvironmentIcons) into tList
   repeat for each line l in tList
      if there is not a stack l then next repeat -- windows placeholder
      if char 1 to 6 of the short name of stack l is "revLib" then put char 7 to -1 of the short name of stack l & cr after tStacksList
      else if char 1 to 3 of the short name of stack l is "rev" then put char 4 to -1 of the short name of stack l & cr after tStacksList
      else put the short name of stack l & cr after tStacksList
   end repeat
   delete last char of tStacksList
   sort tStacksList
    put the text of btn "library chooser" into tText
  if the platform is "MacOS"
   then put "-" into tDivider
   else put empty into tDivider
   if tStacksList is not empty
   then put line 1 to 5 tText & cr & tDivider & cr & tStacksList into btn "library chooser"
   else put line 1 to 5 of tText into btn "library chooser"
   send "menuPick" && the label of btn "library chooser" to btn "library chooser"
   set the thumbPosition of scrollbar "iconchooserscrollbar" to 0
   put the cCurrentIcon of this stack into fld "ID"
   
   # TH-2008-03-06 : Fix for bug 5987
   set the pageIncrement of scrollbar "iconchooserscrollbar" to 20
   set the lineIncrement of scrollbar "iconchooserscrollbar" to 10
end preOpenStack</code>

Then edit the script of the “library chooser” button on that card. Where it says:

<code>else  put "rev" & pWhich into tStackName</code>

Change to

<code>else 
      put "rev" & pWhich into tStackName
      if there is not a stack tStackName then
         put "revLib"&pWhich into tStackName
      end if
   end if</code>

Database functions and commands

Some handy little general database functions and commands I wrote for an app I’m working on. They need some extra error handling but they are good for getting up and running quickly.

<code>function getTable pTable
   local tReturn,tSQL
   put "SELECT * FROM "&pTable into tSQL
   put revDataFromQuery(tab ,cr ,sDB ,tSQL) into tReturn
   if tReturn contains "revdberr" then return ""
   set the itemDel to tab
   return tReturn
end getTable

function getTableRecord pTable,pKey,pID
   local tReturn,tSQL,tQuery
   put "SELECT * FROM "&pTable&" WHERE "&pKey&" = "&pID into tSQL
   put revQueryDatabase(sDB,tSQL) into tQuery
   if not revdb_iseof(tQuery) then 
      revMoveToFirstRecord tQuery
      repeat for each item tColumn in revDatabaseColumnNames(tQuery) 
         put unSQLString(revDatabaseColumnNamed(tQuery,tColumn)) into tReturn[tColumn]
      end repeat
   end if
   revCloseCursor tQuery
   return tReturn
end getTableRecord

command deleteTableRecord pTable,pKey,pID
   local tReturn,tSQL,tQuery
   put "DELETE FROM "&pTable&" WHERE "&pKey&" = "&pID into tSQL
    revExecuteSQL sDB,tSQL
end deleteTableRecord

on updateTableRecord pTable,pKey,pID,pRecordA
   local tSQL,tField
   put "UPDATE "&pTable&" SET" into tSQL
   repeat for each line tField in keys(pRecordA)
      if pRecordA[tField] is a number then
          put " "&tField&"="&pRecordA[tField]&"," after tSQL
      else
         put " "&tField&"="&sqlString(pRecordA[tField])&"," after tSQL
      end if
   end repeat
   delete char -1 of tSQL
   put " WHERE "&pKey&"="&pID into tSQL
   revExecuteSQL sDB,tSQL
end updateTableRecord</code>

Finding out the LAN IP

I had to work out the LAN IP for the machine my app is running on because the IP is needed to enter into an iPhone app so it can sync to the desktop. This is just an temporary measure while we implement zeroconf. Here’s a little modification of Ken Ray’s great GetMACAddress function to return the LAN IP.

<code>function getLANIP
   -- value to return
   local retVal,tCard,ifConfigs,winExists,sys32Exists,temp
   set the itemDel to "."
   switch (the platform)
      case "MacOS"
         if item 1 of the systemVersion <10 then
            return ""
         else
            -- OS X
            repeat with X=1 to 10
               put "en"&X into tCard
               put shell("ifconfig "&tCard) into ifConfigs
               if char 1 to 9 of ifConfigs = "ifconfigs:" then
                  exit repeat
               else if char 1 to 4 of ifConfigs = "zsh:" then
                  return ""
               else
                  get matchText(ifconfigs,"(?s)inet (.*?) ",retVal) -- These are spaces on either side of (.*?)
                  if it is false then
                     return ""
                  else
                     exit repeat
                  end if
               end if
            end repeat
         end if
         break
      case "Win32"
         -- All Windows
         put (there is a file (specialFolderPath("system") & "/IPCONFIG.EXE")) into winExists
         put (there is a file (specialFolderPath("system") & "/SYSTEM32/IPCONFIG.EXE")) into sys32Exists
         if winExists or sys32Exists then
            set the hideConsoleWindows to true
            put shell("ipconfig /all") into temp
            if word 2 of systemVersion() < 6 then
               get matchText(temp,"IP Address[\. ]*: ([A-Z0-9-.]*)",retVal)
            else
               get matchText(temp,"IPv4 Address[\. ]*: ([A-Z0-9-.]*)",retVal)
            end if
         else
            return ""
         end if
         break
   end switch
   return retVal
end getLANIP</code>

revWeb Widget for WordPress

Adding a revlet to a wordpress sidebar just got a whole lot easier with this plugin. Just unzip and copy the files to your wordpress plugins folder and it will appear in your plugins list for you to activate. Once activated you will have a widget to add to your sidebar where you can enter the revlet url, CSLID, stack name, stack width and stack height.

You won’t find this plugin in the add new plugin search in wordpress admin because I haven’t uploaded it yet.

[dm]1[/dm]
[mp:id=6]