Your first problem is very easy to solve, just make sure that your ruby script executes asynchronously.
I use this function in a couple of my scripts instead of the outer-most loop:
def asyncLoop(range, &proc)
from = range.min
to = range.max
iteration = lambda do |index|
last = (index > to)
proc.call(index, last)
if !last then
UI.start_timer(0.02, false) { iteration.call(index.succ) }
end
end
iteration.call(from)
return nil
end
You can use it like this:
asyncLoop(0...16) do |i, last|
if (!last) then
# Perform ith iteration
else
# Put code to be run after this operation here instead of outside the loop
end
# Code here will run immediately, so leave it empty