The original question was answered so I will mark it so as other users might look for the same thing and then the sample script will be of help (and we do not want to continue writing here with regards to the second question)
This is now outside the help that can be offered here in the forum from me (perhaps someone else can help). Also if you have a commercial licence, open up a support request.
If you do not have a licence with support, then see the internet and (see search link below - see stackoverflow post in the results), or try and search the forum, or open up a new discussion here if the above does not help.
All the best