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>

2 thoughts on “Recursive Image Library Creation In Revolution”

  1. I not found the revIconChooser card 1. Where is it?

    Replace the preOpenStack handler in revIconChooser card 1 with

    Salut,
    Josep M

Comments are closed.