import obspython as obs
import os
# Global variables for script state
source_name = ""
output_path = ""
interval = 1
last_file = ""
def script_description():
return "Exports a cleaned-up 'current_file_name' (no folders, no extension) to a text file."
def update_text_file():
global source_name, output_path, last_file
if not source_name or not output_path:
return
source = obs.obs_get_source_by_name(source_name)
if source:
settings = obs.obs_source_get_settings(source)
full_path = obs.obs_data_get_string(settings, "current_file_name")
if full_path:
# --- THE CLEANUP LOGIC ---
# 1. Get just the filename (e.g., "Song.mp3")
filename_with_ext = os.path.basename(full_path)
# 2. Split the name from the extension and keep the name (e.g., "Song")
clean_name = os.path.splitext(filename_with_ext)[0]
# Only write to disk if the song has actually changed
if clean_name != last_file:
try:
with open(output_path, "w", encoding="utf-8") as f:
f.write(clean_name)
last_file = clean_name
obs.blog(obs.LOG_INFO, f"Updated text file: {clean_name}")
except Exception as e:
obs.blog(obs.LOG_WARNING, f"Failed to write to file: {e}")
obs.obs_data_release(settings)
obs.obs_source_release(source)
def script_update(settings):
global source_name, output_path, interval
source_name = obs.obs_data_get_string(settings, "source")
output_path = obs.obs_data_get_string(settings, "path")
interval = obs.obs_data_get_int(settings, "interval")
obs.timer_remove(update_text_file)
if source_name and output_path:
obs.timer_add(update_text_file, interval * 1000)
def script_properties():
props = obs.obs_properties_create()
p = obs.obs_properties_add_list(props, "source", "Playlist Source:",
obs.OBS_COMBO_TYPE_EDITABLE,
obs.OBS_COMBO_FORMAT_STRING)
sources = obs.obs_enum_sources()
if sources:
for s in sources:
name = obs.obs_source_get_name(s)
obs.obs_property_list_add_string(p, name, name)
obs.source_list_release(sources)
obs.obs_properties_add_path(props, "path", "Output Text File:",
obs.OBS_PATH_FILE_SAVE, "Text files (*.txt)", None)
obs.obs_properties_add_int(props, "interval", "Update Interval (seconds):", 1, 60, 1)
return props