PowerBox Plug-in hint 1: calling a plug-in script from "outside"

9 views
Skip to first unread message

info.sim...@googlemail.com

unread,
Jul 31, 2024, 7:34:30 AM7/31/24
to Notecase Pro

While developing my plugin PowerBox I came up with the idea of calling a routine from the plugin from another script to avoid redundancy.

The tricky part is to find the document ID of the loaded plug-in and the note ID of the script you want to execute.

Given my plug-in uses an INI file for configuration, the initialize routine of the plugin will write the document ID to the INI file. Therefore it can be red from any other script reading the INI file.

If you know the note ID of the script in the plug-in you can then use Nc_Script_Execute to run the script from the plug-in.


What’s my use case?

I use the autoreplace feature of NoteCase Pro to call a script from the plug-in. Specifically I use the „@“ sign as shortcut to run below script which then executes the „Link to“ script from the plug-in. I use the link feature heavily to link stored contatcs / vCards to meeting notes or company references


The full code would be (without extensive error checking):


CONST_NOTE_ID = "SbQFZxxKs7eeeA2VCRUnIQ"
CONST_INI_NAME = "sn_ncp40.ini"

-- ================================
-- get ini file
-- ==============================
function getIniFile()
strIni = CONST_INI_NAME
-- get app's config directory path
strIniPath = Nc_Config_ConfigDir_Get()
strFile = strIniPath .. strIni
return strFile
end

-- ==========================
-- load configuration into array
-- ============================
function loadConfig()
strFile = getIniFile()

local t = {}
file = io.open(strFile, "r")
if file == nil then
return nil
end

for line in io.lines(strFile) do
local key, value = line:match("^([%w_]+)%s-=%s-(.+)$")
if value ~= nil then
t[key] = value
end
end
file:close()
return t
end

-- ==================================
curConfig = {}
curConfig = loadConfig()
pluginID = tonumber(curConfig["LibraryID"])

-- plugin doc found
rc = Nc_Script_Execute(pluginID, CONST_NOTE_ID)

A more sophisticated solution would be to use another routine I developed.


„runPlugInScript“ accepts a string as value for a custom property „$$ScriptID“. With this approach one doesn’t have to know the note ID. The routine will go through all notes and look for the provided value in the custom property „$$ScriptID“ to find the note to execute.


-- ===========================
-- This function executes a script from an installed plugin
-- ===========================
function runPluginScript(strScriptID)
local n

nPlugInDocID = getIniValue("LibraryID")
if nPlugInDocID == -1 then
return -1
end

-- cycle through notes and find script ID
nNoteCount = Nc_Doc_NoteCount(nPlugInDocID)

for n=nNoteCount-1,0,-1 do
-- get info on the note (note indexes start from 0)
strNoteID = Nc_Note_ID_GetByIdx(nPlugInDocID, n)
strCustPropValue = getCustPropValueByName(nPlugInDocID, strNoteID, CONST_SCRIPTID)

if strCustPropValue ~= nil then
if strCustPropValue == strScriptID then
Nc_Script_Execute(nPlugInDocID, strNoteID)
end
end

end
end

Reply all
Reply to author
Forward
0 new messages