How to create a unique osx automator action that randomly renames files?
December 7, 2007 8:26 PM   Subscribe

How to create a unique osx automator action that randomly renames files?

Here's something I'd like to make OSX Automator do: I have a folder full of 1000 files named sequentially 001.jpg, 002.jpg, etc.

I would like an Automator action that randomly renames the files but keeps the original filenames intact.

(Ie. 001.jpg would be renamed 356.jpg for example, 248.jpg would be renamed 129 or some random number)

Any ideas how to make this happen?
posted by jeremias to Computers & Internet (7 answers total)
 
What do you mean rename but keep original names intact? Do you want it to make a copy of the file under a new name? May I ask why you want to do this? The answer may be pertinent.
posted by pmbuko at 9:03 PM on December 7, 2007


Also, what OS do you have? Automator in Leopard is a bit more full-featured than Tiger.
posted by pmbuko at 9:09 PM on December 7, 2007


Automator isn't going to be capable of this, even in Leopard. (The rename file action is buggy in Leopard anyway.)

Let me guess: you're trying to randomize the images in a slideshow?

Quick-and-dirty perl script, let's see if it survives preview without getting mangled:
#!/usr/bin/perl
opendir (DIR, "path/to/your/images");
my @files = grep { /\.jpg/ } readdir(DIR); # get list of all jpegs in that directory

@files = sort {(rand 2) <=> 1} @files; # randomize it
for ($i=0; $i<scalar(@files); $i++) {
$old = $files[$i];
$new = $old;
$new =~ s#^\d*__##; # remove earlier modifications, so you can run this twice
# prepend a random number and two underscores to the filename
$new = $i . "__" . $new;
print "renaming $old to $new\n";
rename($old, $new) or warn("Couldn't rename $f to $i__$f"); # rename the file
}

Use at your own risk, etc.
posted by ook at 10:59 PM on December 7, 2007


Yeah, it survived, except my warn() has the wrong variable names in it. So much for user-friendly. :)
posted by ook at 11:01 PM on December 7, 2007


(And if you want to get the filenames back to what they were originally, comment out the line $new = $i."__".$new; and run it again.)
posted by ook at 11:04 PM on December 7, 2007


Response by poster: Yes. essentially correct.

I have a folder full of images and I want to randomize them. But permanently.

If automator can't do this what about applescript?
posted by jeremias at 5:18 AM on December 8, 2007


#!/bin/bash

#I think OSX comes with bash.
#Randomize contents of all .jpg in working directory:

IFS='
'
nn=`ls *jpg | wc -l`
aa=(`ls *jpg`)
ii=0

while [ $ii -lt $nn ]
do
jj=$ii
while [ $jj == $ii ] ; do jj=`expr $RANDOM % $nn` ;done
mv "${aa[ii]}" .tmp.jpg
mv "${aa[jj]}" "${aa[ii]}"
mv .tmp.jpg "${aa[jj]}"
ii=`expr $ii + 1`
done
posted by sfenders at 5:56 AM on December 8, 2007


« Older I loved being sincerely welcomed to the jungle.   |   Help! My printed PDF pages are so small that... Newer »
This thread is closed to new comments.