pixxxel schubser's code is good for what you want to do, unless your layer is in a group. Then the getByName will not work on those layers. As to the two scripts, you can do this by writing the layer name to a csv or xml file that the other script can read and use until changed.
So you would add this code to pixxxel schubser's to write the name to a file
file = new File(fileName);//insert name and path
file.encoding = 'UTF8';
file.open('w');
file.write(aLayName);
file.close();