Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript random on startup

Hello, I have been trying to open a random file from a select folder on startup (by adding the applescript app to the login items). I have found a way to get some kind of "pseudo random" selection using the code:

tell application "Finder"
set random_file to some file of folder "CFolder" of folder "BFolder" of disk "Storage"
open random_file
end tell

With this setup though, the "random" selection is always the same on startup. Any ideas or suggestions?

-Brett

G4 Quicksilver, Mac OS X (10.4.8)

Posted on Jul 27, 2009 9:34 PM

Reply
Question marked as Best reply

Posted on Jul 28, 2009 4:45 AM

Hi Mr. Zzzz. I'm surprised that the pseudo-random selection is always the same on startup. Maybe you could try this:

tell application "Finder"
set the_folder to folder "CFolder" of folder "BFolder" of disk "Storage"
set RN to random number from 1 to (count the_folder) with seed (time of (current date))
set random_file to file RN of the_folder
open random_file
end tell

Ref.: “random number” in the [AppleScript Language Guide|http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScri ptLangGuide/reference/ASLR cmds.html#//appleref/doc/uid/TP40000983-CH216-SW42] (p. 146 of the PDF version).
6 replies
Question marked as Best reply

Jul 28, 2009 4:45 AM in response to Mr. Zzzz

Hi Mr. Zzzz. I'm surprised that the pseudo-random selection is always the same on startup. Maybe you could try this:

tell application "Finder"
set the_folder to folder "CFolder" of folder "BFolder" of disk "Storage"
set RN to random number from 1 to (count the_folder) with seed (time of (current date))
set random_file to file RN of the_folder
open random_file
end tell

Ref.: “random number” in the [AppleScript Language Guide|http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScri ptLangGuide/reference/ASLR cmds.html#//appleref/doc/uid/TP40000983-CH216-SW42] (p. 146 of the PDF version).

Aug 4, 2009 2:09 PM in response to Mr. Zzzz

Never too late to do better.

For a computer that would restart at exactly the same time each day, it's possible (I didn't test it) that the preceding solution wouldn't work. In such a case, a better solution would be to use a number such as the following integer as a seed: (current date) - (date "Monday, January 1, 2001 12:00:00 AM").

applescript random on startup

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.