How to create a unique osx automator action that randomly renames files?
December 7, 2007 8:26 PM   RSS feed for this thread 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 comments 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


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 want to hear songs that are ...   |   Using Preview for PDFs, what c... Newer »

You are not logged in, either login or create an account to post comments



Related Questions
Stripping some (but not all) formatting from rtf... May 2, 2007
How do I set up a document-capture system on my Mac? September 16, 2006
How to make this automator action? June 30, 2006
How do I talk to Apple's Bluetooth driver? April 22, 2006
How do I script removal of the head and tail of a... January 7, 2006