I am looking for a way to auto run python code when a project opens. I have it built in notebooks, but when I tried to make it into a script tool, it would never run in the active map. Does anyone has any ideas on how to get it to run code on start up?
I have a feeling the answer might depend on what code you're trying to have run on startup.
My initial gut reaction would be to say you actually need a wrapper around your project, instead of the reverse.
Your described order:
How I'd hypothetically accomplish that [in some instances]:
Where this gets tricky is that you mentioned the phrase "run in the active map", which implies to me that you're doing operations that require the arcpy.mp library, and you're using the token 'Current' in those function calls. (Or otherwise performing operations that are logically similar).
That's potentially quite a bit more complex, depending on what you're trying to do, because many of those functions presume that you have Pro open and are calling from inside Pro, if you're using that token.
So, I'd recommend reframing the question. What are you ultimately trying to do? Why did you arrive at "I need a script" and "the script needs to run automatically."
There's a solid chance we can get around the issue entirely by approaching the problem in a different way.