Delete Recent File List.fh_lua

--[[
@Title:       Delete Recent File List
@Type:        Standard
@Author:      Mark Draper
@Version:     1.1.1
@LastUpdated: 9 Feb 2023
@Licence:     This plugin is copyright (c) 2023 Mark Draper and is licensed under the MIT License which
 is hereby incorporated by reference (see https://pluginstore.family-historian.co.uk/fh-plugin-licence)
@Description: Deletes the list of recent files in the Family Historian File > Gedcom File Tasks menu.
              The files themselves are not deleted.
]]

fhInitialise(5,0,0,'save_required')
require('lfs')

-- *********************************************************************

function main()

	-- build up batch file to close FH and modify Registry list

	local Path = fhGetContextInfo('CI_APP_DATA_FOLDER') .. '\\Plugin Data'
	local ZapFile = Path .. '\\~fhModifyRecentFileList.bat'

	lfs.mkdir(Path)				-- create folder if it doesn't already exist

	local tblT = {}
	table.insert(tblT, '@echo off')
	table.insert(tblT, 'cls')
	table.insert(tblT, 'powershell start-sleep -s 1')
	table.insert(tblT, 'taskkill /IM fh.exe /f')
	table.insert(tblT, 'powershell start-sleep -s 1')
	table.insert(tblT, 'reg delete "HKCU\\SOFTWARE\\Calico Pie\\Family Historian\\Recent File List" /f')
	table.insert(tblT, 'del "' .. ZapFile .. '"')

	local msg = 'Family Historian will now be closed automatically in order to delete the File ' ..
			'menu list of recent files.\n\nAre you sure that you want to do this?\n\n' ..
			'The files themselves are not deleted.'

	if fhMessageBox(msg, 'MB_YESNO', 'MB_ICONEXCLAMATION') ~= 'Yes' then return end

	if CountInstances() ~= 1 then
		local msg = 'There may be more than one instance of Family Historian open.\n\n' ..
				'Proceeding with the operation could cause data loss. Please close ' ..
				'any additional copies and re-run the plugin.'
		fhMessageBox(msg, 'MB_OK', 'MB_ICONSTOP')
		return
	end

	local F = io.open(ZapFile, 'w')
	if not F then
		fhMessageBox('Cannot create action file!', 'MB_OK', 'MB_ICONSTOP')
		return
	end
	F:write(table.concat(tblT, '\n') .. '\n')
	F:close()

	fhShellExecute(ZapFile)
end

-- *********************************************************************

function CountInstances()

	-- checks just a single instance of FH is running

	local TempFile = fhGetContextInfo('CI_APP_DATA_FOLDER') .. '\\Plugin Data\\~fhCountInstances.tmp'

	if not os.execute('powershell get-process *fh* > "' .. TempFile .. '"') then
		return
	end

	local Instances = 0

	fhSleep(200)		-- slight pause to ensure output file fully written

	for line in io.lines(TempFile) do
		if line:lower():match('%sfh%s+$') then
			Instances = Instances + 1
		end
	end

	os.remove(TempFile)

	return Instances
end

-- *********************************************************************

main()

Source:Delete-Recent-File-List-4.fh_lua