Is there a way to find out how many photos the g1X has taken up to now, or is thw only a way, to estimate the number?
The eventproc UIFS_WriteFirmInfoToFile writes a file (default name firminfo.txt), which might include total shutter count. However beware that this value can actually get reset in some unclear circumstances (has on several of my cams) so isn't entirely reliable. You can call it like this
con 1> =return call_event_proc('UI.CreatePublic')
con 2> =return call_event_proc('UIFS_WriteFirmInfoToFile',0)
You should reboot after using it, because UI.CreatePublic registers a bunch of stuff which may interfere with shooting. You can look UIFS_WriteFirmInfoToFile in the funcs*.csv and call it directly, but in that case, the second parameter has to be a *pointer* pointing to 0 or the name of the file you want to write.
So I wanted to implement the simple logic in pseudocode:
if camera is not in record mode
switch to record mode
The key thing here is that the 'rec' command is a CLI command, not a chdkptp or CHDK Lua function. chdkptp CLI is NOT a scripting language with conditional logic or status codes (there's supposed to be a setting to make errors non-fatal, but it appears I didn't make it work for command files run with source

)
So you can either do the rec switch in your own camera side Lua code, calling switch_mode_usb, or execute the CLI rec command from chdkptp Lua.
For the first, option, you can just take the camera side code from 'rec' command found in cli.lua, like
exec status, err=con:execwait[[
if not get_mode() then
switch_mode_usb(1)
local i=0
while not get_mode() and i < 300 do
sleep(10)
i=i+1
end
if not get_mode() then
return false, 'switch failed'
end
return true
end
return false,'already in rec'
]]
Note the above must be all one line if you actually put it in an exec command.
For the second option, you could use
exec cli:execute('rec')
This alone should be sufficient, since cli:execute returns its status and output, and exec catches any errors. Note however that any other errors (like a dead connection) would also be ignored. If you want to see the output / error messages of a CLI command executed like this, you can use
exec cli:print_status(cli:execute('...'))
Or if you want to do something with the status, you can do
exec local status,err = cli:execute('...') if not status then ... do some error handling ... end