How to detect Renoise has started, fully loaded (renoise.song() is no longer nil) and run a script?

Hi, how do I make it so that after Renoise has fully, completely, loaded (i.e. renoise.song() is no longer nil, etc)
that is run?

and also : Renoise.ScriptingTool.API.lua

Here’s a standalone example:

local tool = renoise.tool()

local function my_script_main()
 local song = assert(renoise.song(), "Song should be loaded")

  -- your code here....
end

-- run the script anytime a song is loaded
tool.app_new_document_observable(my_script_main)

-- if already loaded, run it immediately
if renoise.song() then
  my_script_main()
end