![]() |
Using cron to change desktop
Been trying to work this out for a while now, but need some help. I loved using Sundial in OS9, check out www.jna.com if you don't know what it does. The files it uses are quicktime movies made up of 288 pictures taken 5 minutes apart to make 24 hours of pictures. It will not work at all in OSX, and I get the feeling they will never update it. I can convert the movies to 288 individual pictures and use any one of a number of desktop changers to have it update at whatever interval I want, but the pictures won't sync with the time of day, and they start again from the beginning when the computer is rebooted. My idea is to use cron, tell it to use some program to load the desired image at the desired time, and set it as the desktop picture, thus keeping it in sync. As an example, at noon cron would call some program to load picture#100, then five minutes later cron would have the program load picture#101, and so on cron calling this program to load a certain picture based on the time of day. I need a program for cron to call, or an applescript, or a perl script, or something to change the desktop on command. The actual cron settings will be easy, but quite tedious to set up. Thanks in advance.
|
get changeDesktop...
http://www.classicalguitar.net/brian...changedesktop/ cron is better suited to run administrative scripts that don't have dependencies to such notions of 'desktop' and 'user'. |
I am currently using changedesktop, and it works well, but I cannot sync a certain picture to a specific time of day with it, and it restarts at picture#1 upon reboot, or program restart. I agree with you about cron's usage to a point, but it is well suited to run any task, not just system type tasks. Cron is great for running anything one needs to have scheduled.
|
Wayne,
Let's start off with this Perl script :- #!/usr/bin/perl # This is freeware, do whatever you want with it apart from passing it # off as your own work. # Use it at your own risk. It has been tested on Mac OSX Public Beta. # Rob Hartill, 19/March/2001 # changes to fix file handling and update for OS X release # by Tony Williams 5/July/2001 use strict; use URI::file; my $pic_dir = "/Volumes/Big/created_desks"; # change for local dir my $prefs = `defaults read com.apple.desktop`; # read existing finder prefs # read all the jpeg images in the local dir my $dirhandle; opendir($dirhandle, $pic_dir) || die "Failed to open $pic_dir, $!"; my @jpegs = grep(/jpe?g$/i, readdir($dirhandle)); closedir($dirhandle); # pick a random jpeg image my $new_pic = $jpegs[rand(@jpegs)]; my $uri = URI::file->new($new_pic); my $pic_path = "$pic_dir/$uri"; # insert the URL for the image $prefs =~ s|(ImageFilePath = )"?([^;]+)"?;|$1"$pic_path";|; # remove old finder prefs and replace with new prefs print STDERR `defaults delete com.apple.desktop`; print STDERR `defaults write com.apple.desktop '$prefs'`; # find the process id of the 'Desktop' process my $pid = `ps x | grep Finder`; $pid =~ s/^ ?(d+) .*/$1/; # if we have a process id for the Desktop, send it a restart signal if ($pid >0) { kill(1, $pid); } else { die "Failed to find pid of Desktop processn"; } Now all you need to do is change the code that selects the file name so it gives you the right picture for the time. Give me a day and I'll figure out the code for you. I assume you are going to create pictures named 'something001' to 'something288' Tony |
Thank you Tony, I'll give this a try as soon as I can when I get home tonight. I don't know how to script with perl, but I can see what the script does, and I could probably figure out how to modify it to a small degree, but I would appreciate your help in adding the code to sync the picture with the time. Your assumption is correct, my pictures will be titled as you were thinking, and will have the extension jpg. Also, please assume something001.jpg will start at noon, then increment the pictures by one every five minutes.
|
How could i modify this to handle two monitors?
I tried using a number of apps that could change the desktop picture, but they would only change the primary monitor. |
Quote:
Tony |
Wayne,
Herewith the modified script. For it to run properly you will have to be owned by the user whose desktop will be chnaged and then run setuid. To do this just do a 'chmod 4755 timepictures.pl' The other caveat is that this assumes that $pic_dir is a URI safe directory path. Oh, and if $file_prefix is also a URI safe name (like all alphabetic characters) then you can dispose of the line 'use URI::file;' and '$uri = URI::file->new($new_pic);' then change '$pic_path = "$pic_dir/$uri";' to $pic_path = "$pic_dir/$new_pic"; #!/usr/bin/perl # This is freeware, do whatever you want with it apart from passing it # off as your own work. # Use it at your own risk. It has been tested on Mac OSX Public Beta. # Rob Hartill, 19/March/2001 # changes to fix file handling and update for OS X release # by Tony Williams 5/July/2001 # changes to support time coded pictures # by Tony Williams use URI::file; $pic_dir = "/Volumes/Big/created_desks"; # change for local dir $prefs = `defaults read com.apple.desktop`; # read existing finder prefs $prefix = "file_prefix"; # get time @time_bits = localtime(time); $num = ($time_bits[2] * 12) + (int($time_bits[1]/5)); # change for 12 noon start $num = ($num + 144) % 144; $new_pic = sprintf("%s%03d.jpg", $prefix, $num); $uri = URI::file->new($new_pic); $pic_path = "$pic_dir/$uri"; # insert the URL for the image $prefs =~ s|(ImageFilePath = )"?([^;]+)"?;|$1"$pic_path";|; # remove old finder prefs and replace with new prefs print STDERR `defaults delete com.apple.desktop`; print STDERR `defaults write com.apple.desktop '$prefs'`; # find the process id of the 'Desktop' process $pid = `ps x | grep Finder`; $pid =~ s/^ ?(d+) .*/$1/; # if we have a process id for the Desktop, send it a restart signal if ($pid >0) { # kill(1, $pid); } else { die "Failed to find pid of Desktop process\n"; } Enjoy! |
Re: Using cron to change desktop
Wayne,
Oh, I forgot. You said :- Quote:
*/5 * * * * root /path/to/desktop_changer so at the command prompt 'sudo crontab -e' and you will be dumped into vi with the file loaded ready to insert the single line. Tony |
Well, it didn't work. Following are the changes I made, comment out URI and set directory:
#!/usr/bin/perl # This is freeware, do whatever you want with it apart from passing it # off as your own work. # Use it at your own risk. It has been tested on Mac OSX Public Beta. # Rob Hartill, 19/March/2001 # changes to fix file handling and update for OS X release # by Tony Williams 5/July/2001 # changes to support time coded pictures # by Tony Williams # use URI::file; $pic_dir = "~/movies/WaikikiBeach"; # change for local dir $prefs = `defaults read com.apple.desktop`; # read existing finder prefs $prefix = "file_prefix"; # get time @time_bits = localtime(time); $num = ($time_bits[2] * 12) + (int($time_bits[1]/5)); # change for 12 noon start $num = ($num + 144) % 144; $new_pic = sprintf("%s%03d.jpg", $prefix, $num); # $uri = URI::file->new($new_pic); # $pic_path = "$pic_dir/$uri"; $pic_path = "$pic_dir/$new_pic"; # insert the URL for the image $prefs =~ s|(ImageFilePath = )"?([^;]+)"?;|$1"$pic_path";|; # remove old finder prefs and replace with new prefs print STDERR `defaults delete com.apple.desktop`; print STDERR `defaults write com.apple.desktop '$prefs'`; # find the process id of the 'Desktop' process $pid = `ps x | grep Finder`; $pid =~ s/^ ?(d+) .*/$1/; # if we have a process id for the Desktop, send it a restart signal if ($pid >0) { # kill(1, $pid); } else { die "Failed to find pid of Desktop process\n"; } I did the chmod you said to do, then typed ./timepictures.pl , nothing happened. I then tried it with a sudo, and got this - YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET! FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP! Any ideas? Thank you for all your help, Wayne |
Wayne,
A couple of thoughts. First - set $pic_dir to an absolute path, not using tilde. Second - if you are the only person using your computer then you can take off the setuid (that would be 'chmod 755') and use your user name (instead of 'root') in the crontab entry. Third, and more important. Some idiot sent you his perl code with some debug stuff left in. Flog him immediately and remove the hash in front of the line '# kill(1, $pid);' since it kills the finder and allows it to reload with the new picture. BTW - If you find having your finder reload itself every 5 minutes is onerous I'll leave changing it to every 15 mminutes as an exercise for the reader. You shouldn't need (and it may not work with) sudo since you own your own desktop preferences file. Tony |
scripts are not setuid-able in this rev of the OS, and that's a good thing.
isn't killing the finder to change the desktop pic kinda brutal? i wonder what mechanism changeDesktop or the pref pane uses to ping finder to load a new pic? perhaps watching fs_usage would illuminate? |
Tony,
OK, consider yourself flogged! I should also be flogged as I neglected to set the proper name for the $prefix variable. I corrected those two errors, made an absolute path, did chmod 755, typed ./timepictures.pl, and then got the desired result! Now I just need to add it to crontab and I'll have a working fix for the problem. I agree with mervTormel, it is brutal to kill the finder every time for this, guess I'll start looking at that now; input from others would be greatly appreciated. Perhaps the developer of ChangeDesktop will give me some ideas. Again, thanks a bunch, and I'll let you know if I get a solution to the killing the finder problem. Wayne |
switchPic, too
I'd check out SwitchPic, as well, as the 5.0.1 update claims to have fixed the "stuck desktop after a certain number of switches" problem.
|
Hi all,
Well no hints from fs__usage looking at the way the system preferences and SwitchPic do things and changeDesktop builds and sends an AppleEvent (I checked his source code), which we can't do from Perl. I tested all the signals and nothing seems to get the Finder to not die and just reread it's preferences. Hmmmm. Tony |
Quote:
Code:
osascript -e 'tell application "Finder" to set the desktop picture to "HD:Users:<yourlogin>:Pictures:<whatever>"' |
OK, after spending a LONG time trying to coerce AppleScript to get the finder to accept some variant of a POSX path I gave up and cheated a little.
Here is my new random desktop changer which does not require killing the Finder. Wayne, I'm sure from my previous Perl script and this one you can figure out what you need. #!/usr/bin/perl # random desktop picture changer # by Tony Williams # (C) GPL Tony Williams 26/4/02 # my $pic_dir = "/Volumes/Big/created_desks"; # change for local dir my $pic_path = "Big:created_desks"; # Apple path for above directory # read all the jpeg images in the local dir my $dirhandle; opendir($dirhandle, $pic_dir) || die "Failed to open $pic_dir, $!"; my @jpegs = grep(/jpe?g$/i, readdir($dirhandle)); closedir($dirhandle); # pick a random jpeg image my $new_pic = $jpegs[rand(@jpegs)]; my $new_pic_path = $pic_path . ":" . $new_pic; # tell the Finder $command = "osascript -e \'tell application \"Finder\" to set the desktop picture to \"$new_pic_path\"\'"; system ($command); #ENDS I hope you enjoy. I gotta say I love working with AppleScript - it's so forgiving of quotation marks in the wrong spot. It only took me 5 attempts to get that string right. (Beware of the $command string being folded when you copy and paste) Tony |
has anyone had success with this as a cron job?
over here... http://forums.macosxhints.com/showth...&threadid=2471 we are having trouble getting the cron environment to successfully call the osascript command. result: finder error: application isn't running (-600) |
For all those interested I've posted a solution on how to run a shellscript containg an 'osascript' call to the thread mentioned above by MervTormel
Tony |
Again thanks. But there is a problem, I may have missed something. This is the error I got when running from terminal:
execution error: Finder got an error: Can't set desktop picture to "/users/wayne/movies/WaikikiBeach/WaikikiBeach142.jpg". (-10006) Here is my new script: #!/usr/bin/perl # This is freeware, do whatever you want with it apart from passing it # off as your own work. # Use it at your own risk. It has been tested on Mac OSX Public Beta. # Rob Hartill, 19/March/2001 # changes to fix file handling and update for OS X release # by Tony Williams 5/July/2001 # changes to support time coded pictures # by Tony Williams # use URI::file; $pic_dir = "/users/wayne/movies/WaikikiBeach"; # change for local dir $prefs = `defaults read com.apple.desktop`; # read existing finder prefs $prefix = "WaikikiBeach"; # get time @time_bits = localtime(time); $num = ($time_bits[2] * 12) + (int($time_bits[1]/5)); # change for 12 noon start $num = ($num + 144) % 144; $new_pic = sprintf("%s%03d.jpg", $prefix, $num); # $uri = URI::file->new($new_pic); # $pic_path = "$pic_dir/$uri"; $pic_path = "$pic_dir/$new_pic"; # kept for audit trail # insert the URL for the image # $prefs =~ s|(ImageFilePath = )"?([^;]+)"?;|$1"$pic_path";|; # remove old finder prefs and replace with new prefs # print STDERR `defaults delete com.apple.desktop`; # print STDERR `defaults write com.apple.desktop '$prefs'`; # tell the Finder $command = "osascript -e 'tell application \"Finder\" to set the desktop picture to \"$pic_path\"'"; system ($command); #ENDS # kept for audit trail # find the process id of the 'Desktop' process # $pid = `ps x | grep Finder`; # $pid =~ s/^ ?(d+) .*/$1/; # if we have a process id for the Desktop, send it a restart signal # if ($pid >0) { # kill(1, $pid); # } else { # die "Failed to find pid of Desktop process\n"; # } I left the old code in until it works properly. I can't see anything that would cause the problem, but then again I am not real adept at scripting. Anybody see anything wrong? Wayne |
Wayne,
Change $pic_dir. It's :- $pic_dir = "Boot Drive:Users:Wayne:Movies:WaikikiBeach"; replacing 'Boot Drive' with the name of your OS X boot volume. Then change '$pic_path = "$pic_dir/$new_pic"; ' by changing the '/' to ':' and you should be hunkey dorey. AppleScript requires an Apple colon separated file path and while it has "as POSIX file" to translate the Apple path to a Unix path it doesn't have "as Apple file"(or similar) to translate back the other way. Tony |
Tony, got it. I feel a bit stupid as I knew Applescript requires : , but it did not click with me for this problem. What I did not realize, though, is that the path would be case sensitive. After fixing both problems, it works fine from the shell. I've added it to my crontab, and don't expect any problems. I will most likely need to adjust the syncronization as the pictures are a bit off, but that should be fairly easy. Following is the new code for anyone interested:
#!/usr/bin/perl # This is freeware, do whatever you want with it apart from passing it # off as your own work. # Use it at your own risk. It has been tested on Mac OSX Public Beta. # Rob Hartill, 19/March/2001 # changes to fix file handling and update for OS X release # by Tony Williams 5/July/2001 # changes to support time coded pictures # by Tony Williams # changes to use osascript to avoid killing finder to set new pic # by Tony Williams 26/April/2002 $pic_dir = "Boot Disk:Users:username:picturedir"; # change for local dir $prefs = `defaults read com.apple.desktop`; # read existing finder prefs $prefix = "picture_prefix_name"; # get time @time_bits = localtime(time); $num = ($time_bits[2] * 12) + (int($time_bits[1]/5)); # change for 12 noon start $num = ($num + 144) % 144; $new_pic = sprintf("%s%03d.jpg", $prefix, $num); $pic_path = "$pic_dir:$new_pic"; # tell the Finder $command = "osascript -e 'tell application \"Finder\" to set the desktop picture to \"$pic_path\"'"; system ($command); #ENDS Thank you, Wayne |
| All times are GMT -5. The time now is 06:24 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.