Example python script

The following file will take control of Stradview via the script watching feature, so long as the file 'auto_script.txt' is being watched.


import time
import os

# Names of real (watched) and temporary script files
script = "auto_script.txt"
tmp_script = "tmp_script.txt"

# Wrap the file replace option around a try loop so we can keep attempting it
def try_replace(src, dst):
    try:
        os.replace(src, dst)
        return True
    except:
        return False

# Main loop which creates a script file and runs it
for i in range(0, 100):

    # Set up script file with right information in tmp file
    # so that the creation and editing don't trigger updates
    zoom = 1.0 + 0.01*i
    fid = open(tmp_script, "w")
    fid.write("func START_PARAM_GROUP\n")
    fid.write(f"param OUTLINE_ZOOM {zoom:.2f}\n")
    fid.write("param SEE_OUTLINES false\n")
    fid.write("func END_PARAM_GROUP\n")
    fid.close()
    modified = os.path.getmtime(tmp_script)

    # Replace the script file with this file
    # Keep trying until we can do so (another process is also accessing it)
    replaced = try_replace(tmp_script, script)
    stop_time = time.perf_counter() + 0.25 # Shouldn't take anything like this long
    while (not replaced) and (time.perf_counter() < stop_time):
        time.sleep(0.001) # prevent this process from hammering the file
        replaced = try_replace(tmp_script, script)

    # Wait until the script has run (modification time changed)
    check = os.path.getmtime(script)
    stop_time = time.perf_counter() + 20.0 # Allowing 20 seconds for script to run
    while (check == modified) and (time.perf_counter() < stop_time):
        time.sleep(0.001) # prevent this process from hammering the file
        check = os.path.getmtime(script)