Apologies if I got any terminology wrong.
In the 20th century US Navy (and I believe this to be the case for the RN as well) officers were - and are - divided into two classes: unrestricted line officers, and restricted line officers. The former are essentially fighting officers, and the latter are technical specialists, including engineers; only unrestricted line officer can command ships. Barring some really bizarre circumstance, an engineer would not find himself in command of a ship, even temporarily. For that matter, neither would Spock - again, assuming we hold to the standards of the 20th-21st centuries. The chain of succession would go something like Kirk -> executive officer -> senior department head -> and so on, in order of seniority.