in reply to Main logic of this script

TL;DR

When you have a well-thought-out question to ask; do so. But, first, remove the irrelevant parts. You'll have better luck getting useful replies when you pose precise questions.



Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
  1. code
  2. verbatim error and/or warning messages
  3. a coherent explanation of what "doesn't work actually means.