1/2 Solved Phrase generation before opening phrase editor?

Hi, I’m this far thus far:

function phraseEditorVisible()
--If no Phrase in instrument, create phrase, otherwise do nothing.
if renoise.song().instruments[renoise.song().selected_instrument_index]:can_insert_phrase_at(1) == true then
renoise.song().instruments[renoise.song().selected_instrument_index]:insert_phrase_at(1) else end

--Select created phrase.

--Check to make sure the Phrase Editor is Visible
if not renoise.song().instruments[renoise.song().selected_instrument_index].phrase_editor_visible then
renoise.app().window.active_middle_frame = 3
--If Phrase Editor is already visible, go back to pattern editor.
else renoise.song().instruments[renoise.song().selected_instrument_index].phrase_editor_visible=false 
renoise.app().window.active_middle_frame = 1
end end

renoise.tool():add_keybinding {name = "Global:Paketti:PhraseEditorVisible", invoke = function() phraseEditorVisible() end}

How much further should I go to make sure that “phrase_not_exist” = yes = “create_phrase” = created = “open phrase editor”?

EDIT: How do I select the pre-created phrase and then open the Phrase Editor?

I would only like to create one phrase per one instrument, thus far. Ideally I would one day be able to create say a c-5 phrase and then when I press enter on a c#5 note, I am taken to a c#5 phrase. but even to be able to use an instrument note in the channel column to toggle a simple phrase would be perfection.

EDIT I tacked in arenoise.song().instruments[1]:insert_phrase_at(1) to create phrases, but that will shoot an error after the first one. How to detect if there’s already a phrase, and only create a phrase if there is one? Well it seems “Can_insert_phrase_at” will solve the “error after creating”.

but still I can’t figure out how to select the created phrase.

EDIT2: Figured it out. Now a phrase is auto-generated if not existing, auto-selected, and made visible, by pressing enter in the Pattern Editor. I could probably fix this so that whichever instrument is under the cursor, is auto-selected, checking if there’s a phrase, and then going to that phrase in the editor. That’s probably the next best thing.