AutoHotkey Community

It is currently May 27th, 2012, 4:01 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Random Screensaver
PostPosted: January 24th, 2006, 11:46 am 
Offline

Joined: May 26th, 2004, 12:20 pm
Posts: 61
Hello,

Just a quick hack to make Windows choose a different, random, screensaver each time it launches one. Compile up the following script, then rename the .exe as something like "Random Screensaver.scr", copy it into your C:\Windows\system32 directory (or equivalent), and select it as your default screensaver.

In order to configure a particular screensaver, you'll need to set it as your default, configure it, then set Random Screensaver back as the default.

Code:
#NoTrayIcon

Loop, %A_WinDir%\system32\*.scr
{
  Random, r, 0.0, %A_Index%
  if (r<1)
    ss=%A_LoopFileFullPath%
}

RunWait, %ss% %1% %2%


NB: I leave it as an exercise for the reader to figure out how the random file picking algorithm works :) (answer here).

Edit: Run should be RunWait


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 12:49 am 
Offline

Joined: January 18th, 2006, 12:37 am
Posts: 290
What does the %1% and the %2% do? Are they inputs to the script?

I'm trying to do something a little simplier. I just want to run a screensaver with a push of a button when I leave my computer. But, whenever I try to run a screensaver an options menu for it comes up instead. Any hints?

Code:
RunWait, "C:\windows\boinc.scr"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 9:16 am 
Offline

Joined: May 26th, 2004, 12:20 pm
Posts: 61
Hi,

Quote:
What does the %1% and the %2% do? Are they inputs to the script?

Exactly.

Quote:
I'm trying to do something a little simplier. I just want to run a screensaver with a push of a button when I leave my computer. But, whenever I try to run a screensaver an options menu for it comes up instead. Any hints?

Its always worth searching these forums, you know :) Take a look at this thread.

Ant.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 12:55 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Good answer...
I use Chris' suggestion with the following lines:
Code:
;--- Ctrl+Alt+S: screensaver launcher
^!s::
   PostMessage 0x0112, 0xF140, 0,, Program Manager ; 0x0112 is WM_SYSCOMMAND -- 0xF140 is SC_SCREENSAVE
return
in my permanent script. It works flawlessly.

antonyb, I didn't knew this algorithm, but it is very interesting and quite smart. I was quite skeptical at first, but the explainations are convincing...
Very good script, that can be tweaked at will.
For example, I can put a list of wanted screen savers, including in other folders, at the start of the script, and perhaps give each one a note to increase the chances to be chosen.


Last edited by PhiLho on June 7th, 2006, 11:28 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 2:23 pm 
Offline

Joined: May 26th, 2004, 12:20 pm
Posts: 61
Thanks PhiLho - glad you like it.

Quote:
For example, I can put a list of wanted screen savers, including in other folders, at the start of the script, and perhaps give each one a note to increase the chances to be chosen.

I actually do something similar to this. I have a list of heavy-duty (OpenGL, CPU intensive) screensavers for when my laptop is on AC, and a list of light screensavers (very low CPU use) for when its on battery, and automatically switch between the lists depending on the power status. Its being able to do stuff like that which makes me absolutely love AutoHotkey :D

Which reminds me, must post my Power Status script...

Ant.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2006, 1:20 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
The link antonyb gave is dead... It was probably an unauthorized copy...
I found another copy of this entry to O'Reilly's Perl Cookbook: Picking a Random Line from a File. You might get warning from your browser for expired SSL certificate, go ahead, that's not your bank! ;-)
In case this page disappears too, here it the explanation of the algorithm:
Quote:
This is a beautiful example of a solution that may not be obvious. We read every line in the file but don't have to store them all in memory. This is great for large files. Each line has a 1 in N (where N is the number of lines read so far) chance of being selected.
[...]
If you know line offsets (for instance, you've created an index) and the number of lines, you can randomly select a line and jump to its offset in the file, but you usually don't have such an index.

Here's a more rigorous explanation of how the algorithm works. The function call rand ($.) picks a random number between 0 and the current line number. Therefore, you have a one in N chance, that is, 1/N, of keeping the Nth line. Therefore you've a 100% chance of keeping the first line, a 50% chance of keeping the second, a 33% chance of keeping the third, and so on. The question is whether this is fair for all N, where N is any positive integer.

First, some concrete examples, then abstract ones.

Obviously, a file with one line (N=1) is fair: you always keep the first line because 1/1 = 100%, making it fair for files of 1 line. For a file with two lines, N=2. You always keep the first line; then when reaching the second line, you have a 50% chance of keeping it. Thus, both lines have an equal chance of being selected, which shows that N=2 is fair. For a file with three lines, N=3. You have a one-third chance, 33%, of keeping that third line. That leaves a two-thirds chance of retaining one of the first two out of the three lines. But we've already shown that for those first two lines there's a 50-50 chance of selecting either one. 50 percent of two-thirds is one-third. Thus, you have a one-third chance of selecting each of the three lines of the file.

In the general case, a file of N+1 lines will choose the last line 1/(N+1) times and one of the previous N lines N/(N+1) times. Dividing N/(N+1) by N leaves us with 1/(N+1) for each the first N lines in our N+1 line file, and also 1/(N+1) for line number N+1. The algorithm is therefore fair for all N, where N is a positive integer.

We've managed to choose fairly a random line from a file with speed directly proportional to the size of the file, but using no more memory than it takes to hold the longest line, even in the worst case.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2006, 4:45 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
PhiLho wrote:
The link antonyb gave is dead...
If you're really interested here is the link: http://web.archive.org/web/200405152013 ... h08_07.htm

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2007, 4:53 pm 
:D this is pretty good,but how do i set it to random "Play-sound" instead of screen savers? :?


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 25 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group