get current project name

In case anyone needs this, here’s how you can get the name of the current project you are in. Useful for progmatically creating / modifying project data. It’s a workaround, because Codea doesn’t offer an API for doing this.

How it works: look at each project and compare the number of tabs and the contents of the Main tab to the current project. If there’s a match, than that project is the one you are currently inside.

function getProjectName()
    local projects = listProjects()
    local this_tabs = listProjectTabs()
    local this_main = readProjectTab("Main")
    
    for id, name in ipairs(projects) do
        local project_tabs = listProjectTabs(name)
        local project_main = readProjectTab(string.format("%s:%s", name, "Main"))
        if #this_tabs == #project_tabs and this_main == project_main then
            return name
        end
    end
end

@se24vad I’m not quite sure the purpose of the above code. If the above code has to be added to a project, wouldn’t it be easier to just add a variable with the project name. Also, when a project is created, the first line contains the project name. Another way would be to create a project that would read the Main tab of every project and automatically add a variable with its project name. As for creating/modifying project data, you don’t need to know the name of the project you’re in. Maybe I’m totally overlooking the purpose of your code.

yes, you’re right. all of your arguments speak against this overly complicated function.

I just wrote a filesystem manager for myself, because I want to create an editor similar to ACME which will allow me to edit files of any project from within any of my projects. Think, editing a dependency project (e.g. a TouchHandler) from inside a project that hosts it (e.g. VoxelEditor) or extending a dependency by adding new files (=tabs) to the project while working inside another project, like your game.

That filesystem manager should be able to tell in which project I’m right now and which files are dependencies of this project. Also it should do it kinda reliably, because not all users leave the comments, hence the name of the project might be deleted.

But maybe I’m totally overengineering this.

@se24vad Here’s another function to get the current projects name.

function setup()
    projectName=getProjectName()
    print("this projects name "..projectName)
end
    
function getProjectName()
    saveProjectTab("DUMMYTAB","dummytab") -- create dummy tab
    for _,proj in pairs(listProjects()) do  -- loop thru all projects
        for _,tab in pairs(listProjectTabs(proj)) do    -- loop thru tab names
            if tab=="DUMMYTAB" then  -- found dummy tab
                saveProjectTab("DUMMYTAB",nil)   -- remove dummy tab
                return(proj)    -- return this projects name 
            end
        end
    end
end

@dave1707 hey, that’s a pretty smart and easy solution. didn’t think about that :slight_smile: