Enable retrieval of asset keys as a string so can search for files programmatically

It is great having the asset picker functionality while using the Codea editor but would also be helpful to be able to search for assets programmatically. For instance, if the programmer wanted to allow the user in real-time to open a file picker and choose an asset to load into Codea. Similarly, if the user wanted to save a file from inside a Codea project, the user could then open up a directory of files and ensure they don’t already have a file with the same name.

Currently, Codea allows:

dir = asset.documents.all
for key, value in ipairs(dir) do
     print(value)
end

to see the asset keys that one would use to read/write information to the Codea documents directory.

However, the ‘values’ from above are a combination of the assets’ keys and paths and are userdata and so cannot be parsed with lua’s string library. If there was a function that retrieved just the asset keys as strings from the documents directory then that would seem to make it easier to parse and allow programmers to allow users to choose to load certain aspects into Codea projects in real-time.

1 Like

So you should be able to print value.path to get the full path. You can use value.name to get just the file name, and value.ext to get just the path extension. See the documentation under the Graphics -> Assets section

You can also monitor a folder for external changes by doing:

asset.documents.updated = function(dir)
    print(dir.name .. " updated")
end

Perfect, thanks for explaining, @sim.

I will therefore use this convenience function in my programs to see a directory of the files stored in the documents directory:

function docDir()
    -- for obtaining directory of files in Codea's documents directory [note value.path has path to that file]
    for key,value in ipairs(asset.documents.all) do
        print(value.name)
    end
end

:blush:

1 Like