--[[ OBS Lua script Monitor a text file for changes --]] local obs = obslua local textFile, interval, debug -- OBS settings local activeId = 0 -- active timer id local current = {} -- current values to compare with text file -- called when an update to the text file is detected -- put your code in here to update scenes, etc... local function update(k, v) if debug then obs.script_log(obs.LOG_INFO, string.format("%s has changed to %s", k, v)) end end local function checkFile(id) -- if the script has reloaded then stop any old timers if id < activeId then obs.remove_current_callback() return end if debug then obs.script_log(obs.LOG_INFO, string.format("(%d) Checking text file...(%d)", id, interval)) end local f, err = io.open(textFile, "rb") if f then local line for line in f:lines() do -- check for key=value local k, v = line:match("^([^=]+)%=(.+)$") if k and v then -- success : now check if the value has changed if current[k] ~= v then current[k] = v update(k, v) end end end f:close() else if debug then obs.script_log(obs.LOG_INFO, string.format("Error reading text file : ", err)) end end end local function init() -- increase the timer id - old timers will be cancelled activeId = activeId + 1 -- only proceed if there is a text file selected if not textFile then return nil end -- start the timer to check the text file local id = activeId obs.timer_add(function() checkFile(id) end, interval) obs.script_log(obs.LOG_INFO, string.format("Text monitor started")) end ---------------------------------------------------------- -- called on startup function script_load(settings) end -- called on unload function script_unload() end -- called when settings changed function script_update(settings) textFile = obs.obs_data_get_string(settings, "textFile") interval = obs.obs_data_get_int(settings, "interval") debug = obs.obs_data_get_bool(settings, "debug") init() end -- return description shown to user function script_description() return "Monitor a text file for changes" end -- define properties that user can change function script_properties() local props = obs.obs_properties_create() obs.obs_properties_add_path(props, "textFile", "Text File", obs.OBS_PATH_FILE, "", nil) obs.obs_properties_add_int(props, "interval", "Interval (ms)", 1000, 20000, 500) obs.obs_properties_add_bool(props, "debug", "Debug") return props end -- set default values function script_defaults(settings) obs.obs_data_set_default_string(settings, "textFile", "") obs.obs_data_set_default_int(settings, "interval", 1000) obs.obs_data_set_default_bool(settings, "debug", false) end -- save additional data not set by user function script_save(settings) end