In iTerm2, in the menu bar go to Scripts > Manage > New Python Script
Select Basic. Select Long-Running Daemon
Give the script a decent name (I chose auto_dark_mode.py
)
Save and open the script in your editor of choice.
Copy and paste the script below and save
Go back to iTerm2, go to Scripts in the menu bar and select the script you just saved.
Try toggling Dark mode to see what happens! Reminder it's under Appearance in the System Settings
Note in the script below on lines 15 and 17 that we have a string that we're passing to iTerm2. Change these to whatever you like. The text is whatever appears in the dropdown in the iTerm2 settings under Profile/Color for when you'd want to change the theme manually.
After you change the script you'll have to stop and start the script if it's running already