How to get the active script name?

Is there a way to know which is the displayed script in the list “”?



Well, that assumes you only have one text window in your layout. What if I have four text windows? Which one should the imaginary code return? Focus is determined by the mouse position. So you would need a timer to scan the mouse, then examine the window layouts then determine which on the mouse was over, then if it was a text window you could return the filename of the text window.

If you just want to know the name of the script that is currently running, you can use.


Follow that up with some path parsing.

you can use something like this.

import bpy

text = bpy.context.area.spaces[0].text

Nice to know. According to the docs, spaces[0] is always the space with focus.

import bpy

text = bpy.context.area.spaces[0]

Hi this is more accurate.

import bpy
space =
text = space.text

The area with the focus is bpy.context.area. Since an area can have multiple spaces, like splitted 3d views, spaces[0] may not be the correct one, so using active we make sure that it is.

You cant, since you are in the console, the text editor hasnt the focus so you cant know what its the active one. You can supply the name of the script that you want to run or try a workaround like saving the last active text editor text into some kind of variable.

Inspired by that, I made a UI in the Text Property panel (to have the context) and I would like to return the name of the current file of the Text Editor. Problem is when there is no file, returns “None” which has no method “name”, and I didn’t find any way to pass a string “None” or whatever in the field of row.prop …

Thanks for any idea!

import bpy
from bpy.props import *
class TextPanel ( bpy.types.Panel ) :
    bl_label = "File :"
    bl_space_type = "TEXT_EDITOR"
    bl_region_type = "UI"
    def draw(self, context):
        layout = self.layout
        row = layout.row ( align = True )
        if == None :
            print ("then how should I use row.prop? I d like to return the string None")
        else :
            row.prop ( , "name" )
def register():
    bpy.utils.register_class( TextPanel )
def unregister():
    bpy.utils.unregister_class ( TextPanel )
if __name__ == "__main__":

Why do you need to return anything if theres no text? Its ok like that. Besides, anywhere in your script you have to check if theres an active text. If not return nothing.

You can also use poll functions to prevent execution before the draw function.

or a more cheese solution:

row.label(“theres no text”)
row.prop(, “name”)

Thanks for answer!
In case there s no text, I d like to have the info : whatever “None” writtten in the field or “There is no file opened”.
I thought about the row.label that you propose, but it does not look nice to have once a label and then a real field created by row.prop.

I am new in python so I don t know how to pass any other argument or variable to row.prop. I tried indeed to reassign the “name” with = “None” but of course it stays by “NoneType object has no attribute name”.

Finally I found this workaround:
I go on and create a dummy file :

        if == None :
            row.prop ([0] , "name" )