A Very Specific Utility

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

A Very Specific Utility

Post by tentus »

So I recently had a boring problem: I needed to render the same 3d images over and over again, with slight color variations. Then I needed to move the resulting frames into VERY specific folders, and also rename each one to the color it featured. (End result: dozens of the images with the same name in a complex folder setup. Web development can be a silly field sometimes.)

Boring, repetitive work. So I wrote a Love utility to do it for me. Obviously, this meant working inside the Application Data folder, but... meh, whatever.

So here's the code, hopefully someone somewhere sometime will find use for it! The below code isn't exactly what I wrote, since there were some funky rules that would only confuse potential users.

Code: Select all

function love.load()
	love.filesystem.setIdentity("rename_files")
	if not love.filesystem.exists("input") then		-- all incoming files should be dropped into the "input" folder
		love.filesystem.mkdir("input")
	end
	final = "final.png"		-- the shared filename of the final images
	known, done = {}, {}
	local origin = {	-- the name of the files we are going to move.
		"001.png",		"002.png",		"003.png",		"004.png",		"005.png",
		"006.png",		"007.png",		"008.png",		"009.png",		"010.png",
	}
	local destination = {	-- where the files should go to
		"folder_a",		"folder_b",		"folder_c",		"folder_d",		"folder_e",
		"folder_f",		"folder_g",		"folder_h",		"folder_i",		"folder_j",
	}
	for i=1, #origin do
		known[origin] = destination[i]	-- index folder names by filename, for easy lookup later
	end
	find()
end

function love.draw()
	love.graphics.print("Output filename:   " .. final, 10, 10)
	love.graphics.print("Existing Files:", 10, 30)
	for i=1, #found do
		love.graphics.print(found[i], 10, i*20 + 30)
	end
	for i=1, #done do
		love.graphics.print(done[i], 200, i*20 + 10)
	end
end

function love.keypressed(key, unicode)
	if key == "backspace" and #final > 0 then
		final = string.sub(final, 1, -2)
	elseif key == "escape" then
		love.event.push("q")
	elseif key == "f5" then		-- refresh list of input files
		find()
		done = {}
	elseif key == "return" then
		run()
	elseif unicode > 31 and unicode < 127 then
		final = final .. string.char(unicode)
	end
end

function find()		-- loop through the input folder so we can see what's in there before we run the utility
	found = {}
	for i,file in pairs(love.filesystem.enumerate("input")) do
		found[#found + 1] = file
	end
	if #found == 0 then
		found[1] = "No files found."
	end
end

function run()
	done = {}
	if not love.filesystem.exists("output") then	-- make sure we have a home to go to!
		love.filesystem.mkdir("output")
	end
	done[1] = "Output Files:"
	for i,file in pairs(love.filesystem.enumerate("input")) do
		if known[file] then
			local dir = "output/" .. known[file]		-- combine the output folder with the predefined destination folders
			if not love.filesystem.exists(dir) then		-- create folders before we try to use them.
				love.filesystem.mkdir(dir)
			end
			if love.filesystem.write(dir .. "/" .. final, love.filesystem.read("input/" .. file)) then	-- make file
				-- if you wanted to delete the file after copying it, you would do so here
				done[#done + 1] = known[file] .. "/" .. final
			end
		end
	end
end
To use it, line up destination and origin (in this example, "010.png" is going to end up as "folder_j/final.png"). If you want the final filename to be something different and don't want to edit and restart the utility, you can use backspace and type in a new filename (this was handy for me since I was dealing with a lot of colors in rapid succession). Once you're happy the filename, hit Enter to run the program. It'll look through the files in "input", and if it finds a match, it'll copy it over to the appropriate folder and name.

Questions? I've very aware that this is an extraordinarily specific bit of code, but I figured I would share anyhow- you never know.
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: A Very Specific Utility

Post by Robin »

Dude, isn't that more something for a shell script?
Help us help you: attach a .love.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: A Very Specific Utility

Post by tentus »

I wrote a batch script but it gave me guff about having spaces in the directory path. Writing this in Love took less time than figuring out the batch problems would have taken.

But, if you have a script that will work in both XP and OSX, please post it, I'd love to see it!
Kurosuke needs beta testers
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: A Very Specific Utility

Post by kikito »

Robin wrote:Dude, isn't that more something for a shell script?
It looks like a job for ruby to me. :)
When I write def I mean function.
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: A Very Specific Utility

Post by TechnoCat »

kikito wrote:
Robin wrote:Dude, isn't that more something for a shell script?
It looks like a job for ruby to me. :)
Python!
User avatar
pk
Citizen
Posts: 67
Joined: Wed Dec 14, 2011 2:13 am
Location: Texas, United States
Contact:

Re: A Very Specific Utility

Post by pk »

TechnoCat wrote:Python!
awk + sed + grep!
ALL CREATURE WILL DIE AND ALL THE THINGS WILL BE BROKEN. THAT'S THE LAW OF SAMURAI.
User avatar
ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

Re: A Very Specific Utility

Post by ishkabible »

Is it weird that I use Lua like this all the time? I have various Lua modules that allow lua to do all this(threads, filesystem, sockets, inter process communication) I've thought about creating 1 comprehensive Lua module for it but I never did. The idea being that the main difference between Lua and languages like Ruby, Python or Perl is their libraries; if you close that gap(which love comes close to) you have yourself a "desktop language"
Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests