I guess from your script, you are leveling enchanting by casting the same enchant on an item that either you are wearing or is in your bag. I've not tried it, but I'd suggest you need to have your customcode and castspellaction statements in a while loop, as the customcode will only be called after the castspell is complete.
Why not just buy a load of vellumes (or however it's spelt) an cast the enchants on them. At least you can vendor them or put them on the ah. I use the vellumes for high level enchating with PB all the time, no need to complicate it with customcode and lua statements.
hope that helps