Help with a Tool Change Script
As far as I can recognize, the g-code file cannot be evaluated by printer firmware.

It is expected that slicer software will be aware of tool head selection and process tool changes according to g-code provided in an on-tool change in most slicers. 

The best way to think about g-code (in RepRap word) is a one-way communication as the file is not treated as a script that can read and evaluate parameters on the go. (firmware capabilities).

references: 

RepRap G-code list
Printer quick start guide