Get the texture from selected node or from UV/Image Editor


I’m having some trouble with that script and Cycles:

Sometimes it works properly, and sometimes it doesn’t use the texture that is supposed to be active. It seems to be related to Cycles materials, I can kinda fix it by switching to blender internal and recreating the shaders with the texture, but that’s a bit of a hassle.

So I’m trying to modify the script, instead of using I’d like to use the texture that I select in the node editor, or the texture that I’m looking at in the UV/Image editor, both are fine.

I’m still a noob at scripting with Blender, can anyone help me finding the command I need to do so?