-- Notecase Pro sample script (requires Notecase Pro >= 3.1.4) -- -- Description: -- import .ini file as Notecase document, sections mapping to root note, key=value mapped to note's name/content -- -- This script's author, Miroslav Rajcic, hereby waives all copyright -- and related or neighboring rights to this script, pursuant to the -- Creative Commons CC0 Universal relinquishment of rights found at -- http://creativecommons.org/publicdomain/zero/1.0/ -- setting: should we load content from within the comment lines too (0 or 1) nImportComments = 1 function trim (s) return (string.gsub(s, "^%s*(.-)%s*$", "%1")) end function trim_left(s) return (s:gsub("^%s*(.-)$", "%1")) end -- make sure app version supports necessary scriptable program commands nMaj, nMin, nRev = Nc_Config_GetAppVersion() if (nMaj < 3 or (nMaj == 3 and nMin < 5) or (nMaj == 3 and nMin == 5 and nRev < 2)) then Nc_GUI_MessageBox("ERROR: Script requires NcPro version >= 3.5.2") return end -- get info on the currently active document (data is inserted into it) nDocID = Nc_Doc_ID_GetCur() if Nc_Doc_ReadOnly_Get(nDocID) > 0 then Nc_GUI_MessageBox("Current document is read-only. Can not proceed!") return end -- open file dialog for user to select file to be imported strFileName = Nc_GUI_FileDlg(0, "", "Open Ini File") -- alternative example of having hardcoded file path to be imported -- strFileName = "D:\\111\\notecase_pro.ini" -- check to see if user canceled file selection and abort the script if no file was selected if strFileName == "" then return end -- open designated .ini file f = io.open(strFileName, "r") if f == nil then Nc_GUI_MessageBox("Failed to open " .. strFileName .. "!") return end nFileSize = f:seek("end") f:seek("set") -- reset to the start of the file Nc_GUI_ProgressDlg_Start("Importing file", nFileSize) -- remember NoteID corresponding to the last found .ini section strLastSectionID = "" -- for each text line in the ini file for line in f:lines() do -- update progress local nPos, strErr = f:seek() -- can be nil if EOF reached if nPos ~= nil then Nc_GUI_ProgressDlg_SetPos(nPos) else Nc_GUI_MessageBox(strErr) end -- trim whitespaces from the line line = trim_left(line) -- trim comments if those are allowed to be imported local bIsComment = 0 if nImportComments > 0 then local c = string.sub(line, 1, 1) if c == ";" or c == "#" then -- comment found, strip comment marker bIsComment = 1 line = string.sub(line, 2) end end if 0 == bIsComment or nImportComments > 0 then -- process only non-empty lines local nStrLen = string.len(line) if nStrLen > 0 then -- examine first character in the line local c = string.sub(line, 1, 1) if c == "[" then line = trim(line) -- check also the last character to verify the section line format c = string.sub(line, -1) if c == "]" then -- this is a section name line, insert top-level note local strSectionName = string.sub(line, 2, -2) strLastSectionID = Nc_Note_Insert(nDocID, "", -1, strSectionName) end else -- this is a value=key line local nPos = string.find(line, "=") if nPos ~= nil then local strKey = string.sub(line, 1, nPos-1) local strValue = string.sub(line, nPos+1) -- check if we are within a valid section if string.len(strLastSectionID) > 0 then -- insert new note under the current section note local strNoteID = Nc_Note_Insert(nDocID, strLastSectionID, -1, strKey) -- set text content for the new note Nc_Note_Content_Set(nDocID, strNoteID, strValue, 0) end -- if string.len( end -- if nPos ~= nil end -- if c == "[" end -- if nStrLen > 0 end -- if 0 == bIsComment ... end -- end for -- close the .ini file f:close() Nc_GUI_ProgressDlg_Close() Nc_GUI_MessageBox("Done!")