Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can anyone help me write the following AppleScript (or other) script: random execution of files

3 views
Skip to first unread message

Mike Levin

unread,
Jul 17, 2003, 5:56:52 AM7/17/03
to
Hi all -

I need a script or piece of code (preferably executable - I have no
compiler) for Mac OS9 which would do the following: I specify a folder, and
an executable file (program). It then takes every file in every subfolder of
the folder, in random order, and runs the program on that file, until it
goes through them all. For example, the folder might contain a set of
folders each containing short audio blips and if I specify a player, it will
play them in random order. Can this be done with AppleScript? Or maybe PERL
is the way to go? I don't know how to go through a list of files in
subdirectories in random order on a Mac in OS9 (I could do it in C on a Unix
system, which might be useful if I were running OSX). Any ideas would be
greatly appreciated. Please cc: to mlev...@comcast.net. Thanks!

Mike


David Phillip Oster

unread,
Jul 18, 2003, 2:20:41 PM7/18/03
to
In article <BB3BEB25.117F0%mlev...@comcast.net>,
Mike Levin <mlev...@comcast.net> wrote:

Let us break this down for implementation:

* get the program and the folder from the user.
* explore the folder, and make a list of its complete contents.
* shuffle the list (we want each item to play once, but the order is random)
* for each item in the list,
* hand it to the program, and
* wait for the program to finish with it.


The first and last tasks above have some tricky points:

The first point:
* get the program and the folder from the user.
* read the previously saved values from the preferences
* if there aren't any, or if the read ones are no longer present,
launch a configuration application and quit.
* the configuration application application brings up a dialog box
with "choose" buttons that shows the user the currently chosen
program and folder. When the user presses the "OK" button here,
it writes the preferences, and re-launches the first application.

The last point:
mac applications don't usually inform their caller that they have
finished with a document. What, specificly, are you trying to do?

If you are trying to play an audio file, or movie, just call Quicktime
to play it.


To shuffle an array of items length N, you randomly choose one, swap it
with the one at the end of the array, then shuffle the array of N-1 items:

// Shuffle- shuffle an array in the range [lo..hi)
void Shuffle(Item a[], SInt16 lo, SInt16 hi){
SInt16 i, j;
Item t;

for( i = hi - 1; lo <= i; i--){
j = Roll(lo, i);
t = a[j];
a[j] = a[i];
a[i] = t;
}
}

( the above code look better if you have a Swap function:

for( i = hi - 1; lo <= i; i--){
Swap(a, i, Roll(lo, i));
}

)

To randomly choose an item from lo to hi, (like rolling an N sided die)
do:

// NRandom - return a random number in the range [0..n)
// See Accelerated C++, Section 7.4.4 p. 135 for why a simple '%'
// implements loaded dice.
//
// Algorithm: partition the range of Random() into n equal size buckets,
// (plus some leftover). Call Random until you get a number in a bucket.
// return which bucket it was.
//
// Note: this is C++. In C, you'd either have to ignore errors or
// re-write this as:
// OSErr NRandom(SInt16 n, SInt16* outValuep), and return an error code.
//
static SInt16 NRandom(SInt16 n)
{
SInt16 bucketSize;
SInt16 r;
if( ! (0 < n && n <= 32767)){
Throw_(paramErr);
}
bucketSize = 32767 / n;
do{
r = Abs(Random()) / bucketSize;
}while(n <= r);
return r;
}

// Roll - lo must be less than hi, and hi - lo must be <= 32767
SInt16 Roll(SInt16 lo, SInt16 hi)
{
return lo + NRandom(hi - lo + 1);
}

Random and SetQDGlobalsRandomSeed are Mac system calls. Usually,
you do a GetDateTime() to get a value unique to the second, to pass
as an argument, once, to SetQDGlobalsRandomSeed(), to initialize the
random sequence.

PBGetCatInfoSync() is the system call that lets you iterate through
a folder, its return value tells yo uwhether the ith item is a file
or a sub-folder.

A free C compiler for OS 8 can be downloaded from Apple as MPW:
Macintosh Programmer's Workbench.

0 new messages