XML Editior Script Help

I’ve been writing an plugin for modifying a xml file such as this:

<TextureAtlas imagePath="SlimeArrow.png">
	<SubTexture name="SlimeArrowHud" x="67" y="35" width="5" height="9"/>
	<SubTexture name="Slime" x="66" y="98" width="11" height="11"/>
	<SubTexture name="SlimeArrow" x="81" y="52" width="13" height="4"/>
	<SubTexture name="SlimeArrowBuried" x="81" y="57" width="13" height="4"/>
	<SubTexture name="SlimeArrowPickup" x="114" y="66" width="12" height="12"/>
</TextureAtlas>

However, my script always returns nil for opening a file (Permission is granted,)
can anyone help?

Code

local c = app.fgColor;
local FileName;
local line_number = 0;
---------------------------
local dlg = Dialog("Atlas Editior")
dlg   :separator{ text="SubTextureInfo:" }
      :entry{ id="Texture_name", label="Name:", text="DuctTape"}
	  :button{ text="Chose XML File", -- Chose XML button
            onclick=function()
				
						local handle = io.popen('powershell.exe Add-Type -AssemblyName System.Windows.Forms; $dialog = New-Object System.Windows.Forms.OpenFileDialog; $dialog.ShowDialog() ^| Out-Null; $dialog.FileName') -- CMD command
						FileName = handle:read("*a") -- Gets file name
						handle:close()
						if (FileName != "")
							dlg:modify{ title = "Atlas Editior - " .. FileName} -- Shows what file name you use.
						else
							dlg:modify{ title = "Atlas Editior" .. FileName} -- Shows what file name you use.
						end
                    end }
	  
	  :button{ text="Generate From Selection", -- Non Working Code for editing Atlases, does not work :( 
            onclick=function()
			
						if (FileName && FileName != "") then
							local rectangle = app.activeSprite.selection.bounds -- Get selection box
							line_number = 0
							-- app.alert("Opening: " .. FileName)
							local file, err = io.open(FileName, "r")
							-- For whatever reason, file always returns nil, along with nil.
							if (file ~= nil) then
								local i = 0;
								local lines = {}

								for line in file:lines() do -- Creates a table of lines.
									table.insert(lines, line)
								end
								file:close()
								for line in lines do -- iterate over each line in the file
									if line:find(dlg.Texture_name) then -- if the target string is found in the line
										line_number = i
										line = line:gsub('x="[%d%.%-]+"', 'x="' .. rectangle.x .. '"') -- Subs Values
										line = line:gsub('y="[%d%.%-]+"', 'y="' .. rectangle.y .. '"')
										line = line:gsub('width="[%d%.%-]+"', 'width="' .. rectangle.width .. '"')
										line = line:gsub('height="[%d%.%-]+"', 'height="' .. rectangle.height .. '"')
										lines[i] = line
										break
									end
									i = i + 1
								end
								if (line_number == 0) then
									
									for line in lines do -- iterate over each line in the file
										if line:find("<//TextureAtlas>") then -- if the target string is found in the line
											local NewLine = "		<SubTexture name=\"" ..dlg.Texture_name.."\" x=\"".. rectangle.x .."\" y=\"".. rectangle.y .."\" width=\"".. rectangle.width .."\" height=\"".. rectangle.height .."\"//>" -- Inserts new subtexture with formatting (I hope)
											table.insert(lines, i, NewLine)
											break -- Texture names can only show up once per atlas, saves time!
										end
										i = i + 1
									end
									
								end
								file, err = io.open(FileName, "w") -- Caution! Wipes the file!
								for line in ipairs(lines) do -- Rewrite the file
									file:write(line .. '\n')
								end
								file:close()
							else
								app.alert("Couldn't open file: " .. err) -- err is always nil, so it crashes
							end
						else
							app.alert("Please select a file")
						end
                    end }
      :show{wait=false}
---------------------------------------


hi, CoolModder! :wave:
i wasn’t even able to run the script since there are some syntax errors:
semicolons (i miss them too), use of != instead of ~= and missing then in if block. i also can’t find any mention that lua allows && either, so i replaced it with and and the code runs.
i’d bet there’s more than that, but what caught my attention is the fact you open powershell. i don’t know what exactly you’re trying to do, but dialog:file https://github.com/aseprite/api/blob/main/api/dialog.md#dialogfile seems to me as better option.

following is a basic test. you can select only xml files, click on generate and it will print the content of the file line by line:

local FileName 
local dlg = Dialog("Atlas Editior")
dlg
	:file{ 
		id="Phile",
		label="SubTextureInfo",
		open=true,
		filetypes={ "xml" },
		onchange=function()
			FileName = dlg.data.Phile -- Gets file name
			if (FileName ~= "") then
				dlg:modify{ title = "Atlas Editior - " .. FileName} -- Shows what file name you use.
			else
				dlg:modify{ title = "Atlas Editior"}
			end
		end
	}
	
	:button{ 
		text="Generate From Selection", 
		onclick=function()
		
					if (FileName ~= "") then

						local file, err = io.open(FileName, "r")

						if (file ~= nil) then
						
						--- ::: do something here 
							local filelines = file:lines("*l")
							for _ in filelines do
								print(_) 
							end
						--- ::: 
							file:close()
							
						else
							app.alert("Couldn't open file: " .. err) 
						end
					else
						app.alert("Please select a file")
					end
				end 
	}
	
	:separator() 
	:button{ 
		text="Close", 
		onclick=function()
			dlg:close()
		end 
	}
            
	:show{wait=false}

also, probably don’t use reserved words like lines as names for tables and variables.
well, that’s all i have. ;] hope it helps.

1 Like

Done! Fixed the code, works as intended. Thanks for the help.
Code: GitHub - CoolModder/Asesprite-TF-Atlas-Editior

1 Like