I'm doing what you're trying to do, but with this addon:
WoWInterface Downloads : AutoRack
It allows you to define a list of items for each of up to 8 of your bag slots, each hotkey-able, and it'll move the highest priority item in each list that isn't on cooldown into its corresponding slot. For example, I use it with 2 slots. In one slot, I have a list of healthstones and health potions, in that order. In the second slot, I have mana gems and mana pots, also in that order. Usually at the start of a fight, I'll have a healthstone and a mana gem in the corresponding 2 slots. If I hit my hotkey for the healthstone/health pot slot to use the healthstone that was there, conjured items are now on cooldown, so it'll swap a health pot into the first slot, and a mana pot into the second.