LUA snippets
Common tasks to be realized with LUA.
Get just the names of items that are below a certain item (branch)
The whole function can be copied to a LUA script and directly be used.
--
-- Returns all names in the item tree, which exists below a certain path
--
-- param "path" (string):
-- An item path of the item tree.
-- param "recursive" (bool):
-- Take also deeper nested items into account.
-- param "preserveSubPath" (bool):
-- Is only used if "recursive" is true.
-- If this parameter is true, the names will contain also the sub path.
-- If it is false, only the name will be used.
--
-- Returns an array with the found names.
--
-- If path is empty or does not contain an existing item path, a warning
-- will be logged and the returned array will be empty.
--
function getSubItemNames(path, recursive, preserveSubPath)
local names = {}
if nxa.ItemExists(path) then
local items = nxa.GetItemIds2(path, "*", recursive)
if items then
if preserveSubPath then
local fromPosition = string.len(path) + 2
for index, item in ipairs(items) do
names[index] = string.sub(item, fromPosition)
end
else
for index, item in ipairs(items) do
local fromPosition = item:match(".*()\\") + 1
names[index] = string.sub(item, fromPosition)
end
end
end
else
nxa.LogWarning("Path '" .. path .. "' does not contain any sub items.")
end
return names;
end