New to scripting- Can't find answer elsewhere

Hi,
I have a unique problem I’m trying to solve. Don’t have very much scripting experience but I don’t think what I’m trying to do would be very hard.

I have a folder full of 200 sequentially named image files 001.png through 200.png. What I would like to do is set up a script which would randomly rename and then save (presumably into a different folder) all of the image files. So number 001.png would saved as 137.png for example. But the process would be random so the next time I ran the script 001.png would be 097.png, lets say.

Is there any pre-made script that does this in Os x? Or how hard would it be to write one?

Thanks in advance
-jeremias

There’s certainly no prebuilt script to do exactly what you want because it’s too specialized, but the building blocks are there, and it’s quite simple.

However, you need to be clearer in what you want to do because youe definition can not run as it stands.

You say you have sequentially numbered files 001.png to 200.png and you want to randomly rename 001.png to some other number.

What do you do with pre-existing files?

For example, if the script decides that 001.png should be renamed to 123.png, what happens to the existing 123.png file? Does it get deleted? renamed to another random number? renamed to 001.png (i.e. swap the files?)

That needs to be decided before you can write the script.

Ok I was originally thinking I could create a new folder. So lets say that the original files are Folder A with files 001.png . . .200.png.

I would want the script to create a new folder (or be pointed to a prexisting empty folder, whichever is easier). We’ll call that folder B. Then the script would take 001.png from folder A, save it as some random numbered .png and then place it into folder B. It would then move onto 002.png and do the same thing. I’m basically looking to reshuffle all the file names.

So in the end folder A and folder B would be indistinguishable from the outside. They would both have 200 files 001-200. It’s just that the content would be different.

I just don’t know how the script would keep track of the file names already used. For example if the first file gets saved as 110.png, that can’t be used again and now there are 199 other choices.

I hope that cleared it up a bit.

OK, here’s what I came up with.

It’s pretty well commented, so should speak for itself. It uses pure AppleScript and doesn’t require any OSAX or other applications.

In writing this I found a new-found contempt for the Finder’s scripting ability - I couldn’t convince the Finder to move a file while renaming it at the same time. Any ideas, anyone?


-- prompt the user for source and destination directories
set srcFolder to (choose folder with prompt "Select SOURCE folder")
set destFolder to (choose folder with prompt "Select DESTINATION")

-- get a list of the files in the source folder
set fileList to list folder srcFolder without invisibles

set filesMoved to 0

--repeat until all files are moved
repeat until (number of items in fileList) = 0
	-- get a file at random from the list
	set srcFile to some item of fileList
	-- work out the next file name
	set destFile to my newName(filesMoved + 1)
	try
		-- move the file
		do shell script "mv " & POSIX path of srcFolder & srcFile & " " & (POSIX path of destFolder) & destFile
		-- increment the file counter
		set filesMoved to filesMoved + 1
	on error
		-- Oops. Could not move the file for some reason
		-- could be locked or have some other error such as insufficient disk space.
		display dialog "Could not move " & srcFile buttons {"OK"}
	end try
	-- we've tried this file, so delete it from the list
	set fileList to deleteItemFromList(srcFile, fileList)
end repeat

on deleteItemFromList(theItemToDelete, theList)
	-- this function removes a specified entry from
	-- the list and returns the truncated list
	set tmpList to {}
	repeat with anItem in theList
		-- not-equals character is option-=
		if (anItem as string) ? theItemToDelete then
			-- if this item isn't the one to delete, copy it to the new list
			set end of tmpList to (anItem as string)
		end if
	end repeat
	return tmpList
end deleteItemFromList

on newName(i)
	-- this function converts a number into a three-digit string
	-- prepending with 0's as necessary
	set prefix to ""
	if i < 10 then
		set prefix to "00"
	else if i < 100 then
		set prefix to "0"
	end if
	set fileName to prefix & (i as string) & ".png"
	return fileName
end newName