Plugins and Language Packs for Family Historian

Create Individual Shortcut.fh_lua

--[[
@Title: Create Individual Shortcut
@Author: Jane Taubman  
@Version: 2.0
@LastUpdated: 30 Mar 2020
@Description: 
Creates a Windows command file on the desktop for the selected record,  
which can be clicked to launch the correct project and person.

2.0 V7 compatibility
]]

require "iuplua"
iup.SetGlobal("CUSTOMQUITMESSAGE","YES")


function main()
    local bNotWindows = false
    local userpath = os.getenv("UserProfile")
    local filepath,err = getRegKey('HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\Desktop').."\\"
    if err then
        -- Registry Read failed use default desktop path
        filepath = userpath..'\\Desktop\\'           
    end	 
 
    filepath = filepath:gsub('%%USERPROFILE%%',userpath)	
    local pathdesc = 'Desktop'
    -- Requires V6.1
    if cvtVer('fh') < cvtVer('6.1.0') then
        fhMessageBox('This plug in requires at least 6.1.0 of Family Historian, please upgrade')
        return
    end
    -- Only Works on Windows
    if fhGetPluginDataFileName("LOCAL_MACHINE"):lower():match(":\\users\\public\\") then
        bNotWindows = true
        filepath = fhGetContextInfo('CI_PROJECT_PUBLIC_FOLDER')
        pathdesc = 'project public folder'
        if filepath == '' then
            fhMessageBox("Sorry, on Mac's this can only work when using Projects")
            return
        end
    end
    -- Get Path to FH
    local fhpath,err = getRegKey('HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\FH.exe\\')
    if err then
        -- Registry Read failed check if file exists on default path
        local fhpath = 'C:\\Program Files (x86)\\Family Historian\\program\\fh.exe'
        if not(exists(fhpath)) then
            fhpath = 'C:\\Program Files\\Family Historian\\program\\fh.exe'
            if not(exists(fhpath)) then
                fhMessageBox('Family Historian Not Found!')
                return
            end
        end
    end
    -- Get Default Window for Startup
    local wintype = 'f'
    local winval,err = getRegKey('HKEY_CURRENT_USER\\Software\\Calico Pie\\Family Historian\\2.0\\Preferences\\Rec Win Startup')
    if not(err) then
        if winval == 'TRUE' then
            wintype = 'r'
        end
    end
    -- Collect Settings
    local ptr = fhGetCurrentRecordSel('INDI')
    
    if #ptr > 0 then
        id = fhGetRecordId(ptr[1])
        name = fhGetDisplayText(ptr[1])
    end
    local filetype = 'static'
    id,name,filetype,wintype,ok = mainDialog(id,name,filetype,wintype)
    if ok ~= 1 then
        -- Window was Closed
        return
    end
    if id == null then
        -- No Record selected
        fhMessageBox('No record selected')
        return
    end
    local gedcom = fhGetContextInfo('CI_GEDCOM_FILE')
    -- Fill out template
    local newbatch = batchfile
    newbatch = string.gsub(newbatch, '{key}', fhpath)
    newbatch = string.gsub(newbatch, '{gedcom}', gedcom)
    newbatch = string.gsub(newbatch, '{id}', id)
    if wintype == 'f' then
        newbatch = string.gsub(newbatch, '{wintype}', 'FocusWin')
    else
        newbatch = string.gsub(newbatch, '{wintype}', 'RecordWin')
    end
    local filename = ''
    local message = ''
    if filetype == 'static' then
        filename = filepath..'Launch Family Historian.cmd'
        message = 'A link has been added to your '..pathdesc..' called Launch Family Historian, just double click the link to launch Family Historian'
    else
        filename = filepath..name..' ('..id..')'..'- Family Historian.cmd'
        message = 'A link has been added to your '..pathdesc..' for '..name..', just double click the link to launch Family Historian'
    end
    -- Save File and output message box
    SaveStringToFile(newbatch,filename)
    fhMessageBox(message,'MB_OK','MB_ICONINFORMATION')
    
end
------------------------------------------------------ Template
batchfile = [[
echo [Startup]  >%temp%\fhini.ini
echo Window={wintype} >>%temp%\fhini.ini
echo Record=I{id} >>%temp%\fhini.ini
start "Run FH" "{key}" "/ini:%temp%\fhini.ini" "{gedcom}"
]]
------------------------------------------------------ functions
require "luacom"

function getRegKey(key)
    local sh = luacom.CreateObject "WScript.Shell"
    local ans
        if pcall(function () ans = sh:RegRead(key) end) then
        return ans
    else
        return nil,true
    end
end

function OpenFile(strFileName,strMode)
    local fileHandle, strError = io.open(strFileName,strMode)
    if not fileHandle then
        error("\n Unable to open file in \""..strMode.."\" mode. \n "..strFileName.." \n "..tostring(strError).." \n")
    end
    return fileHandle
end -- function OpenFile
-- Save string to file --

function SaveStringToFile(strString,strFileName)
    local fileHandle = OpenFile(strFileName,"w")
    fileHandle:write(strString)
    assert(fileHandle:close())
end -- function SaveStringToFile

function cvtVer(s)
    if s:lower() == 'fh' then
        s = table.concat({fhGetAppVersion()},'.')
    end
    local ver,factor,max = 0,10000,3
    local t = {}
    for word in s:gmatch("%w+") do
        table.insert(t,tonumber(word))
    end
    for j = 1,max do
        t[j] = t[j] or 0
        ver = ver + (t[j] * factor^(max-j))
    end
    return ver
end

function exists(filename)
    local f=io.open(filename,"r")
    if f~=nil then
        io.close(f)
        return true
    else
        return false
    end
end
------------------------------------------------------------- Prompt Dialog

function mainDialog(id,name,filetype,wintype)
    local ok = 0
    
    if id and id > 0 then
        dynamicName = name..' ('..id..')'
    else
        dynamicName = 'Please use the Select Button Above'
    end
    local staticName = "Launch Family Historian"
    local boxTitle = "Create Individual Shortcut"
    local static = iup.toggle{title=staticName,
    tip="File will be named 'Launch Family Historian'"}
    local dynamic = iup.toggle{title=dynamicName,
    tip="File will be named with the selected Individuals Name"}
    
    if filetype == 'static' then nValue = static else nValue = dynamic end
    nametype = iup.radio
    {
        iup.vbox
        {
            static,
            dynamic
        };
        value=nValue,
    }
    frame = iup.frame{nametype; title="Output Filename"}
    frame.size = '250x20'
    local wRecord = iup.toggle{title='Records Window',tip="Launch with Records Window Open"}
    local wFocus = iup.toggle{title='Focus Window',tip="Launch with Focus Window Open"}
    if wintype == 'f' then rValue = wFocus else rValue = wRecord end
    windowtype = iup.radio
    {
        iup.vbox
        {
            wRecord,
            wFocus
        };
        value=rValue,
    }
    winFrame = iup.frame{windowtype; title="Workspace Selection"}
    winFrame.size = '250x20'
    lbl_indiname = iup.label { title = "Currently Selected Individual:\n "..dynamicName,size=500,padding="10x10"}
    btn_change = iup.button{ title = "Select Individual" }

    function btn_change:action()
        tblIndi = fhPromptUserForRecordSel('INDI',1)
        if #tblIndi == 0 then
            return
        else
            id = fhGetRecordId(tblIndi[1])
            name = fhGetDisplayText(tblIndi[1])
            dynamic.title = name..' ('..id..')'
            lbl_indiname.title = dynamic.title
            iup.SetFocus(btn_create)
            return
        end
    end
    btn_change.padding = '5x5'
    btn_create = iup.button{ title = "Create Launch File" }

    function btn_create:action()
        ok = 1
        return iup.CLOSE
    end
    btn_create.padding = '5x5'
    btn_create.size = '250x20'
    dialog = iup.dialog{
        iup.vbox {
            btn_change,
            iup.fill{},
            lbl_indiname,
            iup.fill{},
            iup.fill{},
            frame,
            winFrame,
            iup.fill{},
            btn_create
        }
        
        ; margin="10x10",
        title=boxTitle,
        size=274,
        resize="YES",
        minbox="NO",
        maxbox="NO"
    }
    dialog:show()
    iup.SetFocus(btn_create)
    if (iup.MainLoopLevel()==0) then
        iup.MainLoop()
    end
    if static.value == 'ON' then filetype = 'static' else filetype = 'dynamic' end
    if wFocus.value == 'ON' then wintype = 'f' else wintype = 'r' end
    dialog:destroy()
    return id,name,filetype,wintype,ok
end
------------------------------------------------------ main
main()

Source: Create-Individual-Shortcut-1.fh_lua