Working backwards from the end of your post...
I believe you have to opt into it before you start your process so that you can collect the operaions. Go to 'Getting Started with Commands' here and look at the sections, 'Recording Your Work (Optional)' and 'Saving Your Work', particularly the subsection, 'Scripts and Log Files'. Below there talks about running the commands as scripts.
However, there is another way to interact with PyMOL rather than the simpler text-based, direct PyMOL scripting language commands. Since it is Python based and an 'Application Programming Interface' (API) has been built in, you can actually use Python to control it and issue commands via the API. You may have seen some of these in the documentation. (I'll suggest illustrating the use of cmd.zoom() below, which has the simpler PyMOL scripting language equivalent of zoom.) I have links to the PyMOL API documentation here under 'Resources'. The Python version can be tricky to write but it opens up more complex programmatic control to do things if you know a little Python. So the investment can be worth. To give you a better sense of using the PyMOL API , click on the 'launch binder' badge, towards the top of that pymol-binder page I directed you to. That will launch a temporary session on a remote computer provided via the MyBinder service. When it starts up, a page will come up with a list of notebooks; however, the quick-start basic example is down on that page that first comes and so scroll down to 'Basic Example'. Now you wouldn't use it that way probably since you are using the GUI right now, but you can actually paste in the PyMOL API commands on either line that says 'PyMOL>', usually above and below the structure view. (That is the PyMOL command line you may have already used. You can also paste the simpler PyMOL scripting commands there for running them in the GUI without them being an an external script file.) For example if you load a structure and zoom way out, pasting in cmd.zoom() will zoom it back to feature the structure prominently. (That isn't a great example because you can just type the PyMOL scripting language version zoom on that command line and accomplish the same thing; however, the PyMOL API commands would more likely be embedded in Python as part of a larger effort.) You can paste blocks of Python code although it can be tricky because of the indentation. And executing processes as a Python script like that session demonstrates may be more useful. (The GUI can run Python scripts as well.) I have an example of making functions using Python in the example notebook 'demo_palette.ipynb' that is one of the ones you can work through. In static form, look for the define function set_my_view() here for an example. (It's in the code cell presently indicted with 'In[5]' under 'Iterating over combinations applied to chains from a set color palette'.)
Why is this person going on about Python and an API? I'm not planning to use that. Sometimes when looking into the documentation it isn't readily apparent why there's seemingly two versions of commands. And it can be more confusing when people post solutions because it may not be stated what interface they are using. Hopefully know there can be two versions of most commands will allow you to better translate suggestions to your chosen approach.
Getting back to your question ...
And then once you have the commands you can either use the script or make it a Python function that you can run or call, respectively.