Asset naming to copy tabs from one project to another

In some recent thread, it was suggested to use readText and saveText, rather than readProjectTab and saveProjectTab. I’m looking for hints on how to do that so as to satisfy the compiler and have it actually work.

Suppose we have the text name of a project in documents. “MTarget” is the name.

We can’t say local proj = asset.documents.."MTarget". We get a run-time error saying attempt to concatenate a userdata value.

If we knew the name of the project at compile time we could say local proj = asset.documents.MTarget, but what do we say when we don’t know the name at compile time?

Advice welcome. Thanks!

@RonJeffries I thought that’s how we were supposed to use assets for an asset that didn’t exist yet. When I put that line in a project, I don’t get any type of error.

local proj = asset.documents…“MTarget”

works for me.

hm, i’ll go look, maybe a typo i’m missing, thanks

I was misrepresenting the problem. I’m trying to get to the point where I can write to a new tab in a different project, whose name I have as a string.

The code I’m using is this:

            local xxx = asset.documents.Files.all
            local yyy = asset.documents
            yyy = yyy.."MTarget"
            print("loop")
            for i,v in pairs(xxx) do
                if v.name:match(".lua") then
                    print (i, v.name, v.type) 
                    local txt = readText(v)
                    local toWrite = yyy..v.name
                    print("write ", toWrite)
                    --saveText(toWrite,txt)
                end
            end--]]

It is the v.name concat that doesn’t work. However, and this may get me where I need to go, this does work:

                    local toWrite = asset.documents.."MTarget"..v.name

It seems that you cannot save a partially concatenated asset name and then concatenate to it, but you can concatenate twice.

So, I think I can work with this but it seems quite odd.

Thanks, I shall press onward …

Update: that asset with two concats executes but has no dot in between MTarget and v.name, so the saveText goes off into space. I’ll try putting one in explicitly.

OK that doesn’t work either. You’re writing to this:

Asset Key: MTarget.Tests.lua (path: "/var/mobile/Containers/Data/Application/8237698C-306E-444D-BA10-95B7089EF5DA/Documents/MTarget.Tests.lua")

And that’s not the right name. One would have to change the name to Tests_lua? or Tests? I don’t know.

I’d like to see a working example of accepting two project names and copying all the tabs from the one to the other, using readText and saveText.

So far, the suggestion that read and saveText will suffice may be true, but it sure isn’t straightforward like the read and save project tab stuff.

Perhaps I’m missing some obvious thing: I often do.

@RonJeffries Could I suggest what WebRepo does:

local asset_path = asset.documents .. “ProjectName.codea/TabName.lua”
                    
-- Write directly to the file
local file = io.open(asset_path.path, "w")
file:write(data)
file:close()

The file will only appear if the tab is already added to the target project (manually of saveProjectTab) but should work correctly to write to the correct file.

@RonJeffries Here are the readtext and savetext lines of code I use in my project backup code. The backupName in savetext is created on the fly based on the date and time that I do the backup. The name in readtext is also a variable name that gets selected from a list of backups.

saveText(asset.documents.Dropbox..backupName,str)

bkupFile=readText(asset.documents.Dropbox..name)

@RonJeffries Heres something I just thru together to copy tabs from “input” and creating and saving the tabs in “output” after pressing copy.

 function setup()
    parameter.text("input")
    parameter.text("output")
    parameter.action("copy",copy)    
end

function copy()
    lst=listProjectTabs("Documents:"..input)
    if #lst>0 then
        if hasProject(output) then
            print("project already exists")
        else
            createProject(output)           
            for a,b in pairs(lst) do
                print(b.."  copied")
                str=readProjectTab(input..":"..b)
                saveProjectTab(output..":"..b,str)
            end
            print(output.."  created")
        end
    end
end

@dave1707 yes, can use the project tab code. Simeon said elsewhere, recently, that read and saveText sound suffice. maybe it’s possible, but it’s not pretty.

@Steppers interesting. i’m writing code to add certain tabs to a project. so i think i’ll stick with the project tab stuff. thanks!