# A script. A script for fun I say. Help me write it.



## Jasoco (May 13, 2003)

I would like to write a script. Either AppleScript or a Shell Script. Whichever is more convenient. That will be run at login.

What it will do is randomly pick a file from a specified folder, copy it to a certain directory and rename it to a certain name.

To clarify, it will pick a JPG file from a folder of JPG's at random, and copy it to the "Desktop Pictures" folder and rename it "Aqua Blue.jpg". What this will do is every time I log out, I will have a different login desktop picture.

I'd probably use AppleScript but I'd be calling Shell commands from it for stuff like the copy and rename part. All I need to know basically is how do I randomly pick a file via AppleScript or the such?

This Script will only run once per login then quit when it's done obviously. I just need the one critical part. The file selection.

I wanna do it all without having to have the script open folders and select files. I want it all in the background at the beginning of my session. One time thing.

Thanks.


----------



## goynang (May 13, 2003)

Erm - what version of OS X are you running?

You can do this in the prefs on Jaguar - you can set how often the bg image rotates - from once every 5secs (!) to once per login.

Is that what you want?


----------



## dlloyd (May 13, 2003)

If you want to use AppleScript, that would be simple, wouldn't it? Just record it! Oooohh, I forgot about that 'random' part... hmmmm


----------



## scruffy (May 13, 2003)

well, from a shell script point of view:

set num_files = `ls | wc -l`
set the_file = `jot -r 1 1 ${num_files}`
set the_file_name = `ls | sed -n ${the_file}p`
ln -s ${the_file_name} "Desktop Pictures/Aqua Blue.jpg"

would do it


----------



## Jasoco (May 13, 2003)

NO NO NO. Goynag. The LOGIN screen. Not the Desktop! I already have my desktop rotating. I want the Login to do it too and I wanted to do it in a fun way. So I wanna make a script. Hone my scripting skillz.

I just need to be able to randomly choose a file at random. Randomly. That's the only part I need to know.

How do I create a Shell Script, scruffy? Just make a file with the text? What do I do? You can give me code, but I need to know where to put it. And can I call it from AppleScript? Thanks.


----------



## Darkshadow (May 13, 2003)

Well, the shell scripts are easy to make, but you can't have them running automatically at login via the login items, so an Applescript would probably be easier to do.

Well, you _could_ make the shell script then write an applescript to run that shell script, then you could have that applescript run when you log in, but that seems overkill to me.


----------



## wiz (May 13, 2003)

no you can make it run automatically at login via login items!!

i read it somewhere.  it is possible.


----------



## Jasoco (May 13, 2003)

Either way, what do I use to create the shell script? Where do I save it and how do I call it from AppleScript, if it can't be run at login? If it can, even better.

I just need to know how to create the script. Pico or something?


----------



## binaryDigit (May 14, 2003)

You just create a simple text file.  Make the first line:

#! /bin/sh

This will force the script to be run by the sh shell.

After you save the file (with a .sh extension is common, but not necessary), chmod it executable.  Modify your /etc/rc script (it runs when the system is booting) to call this new script (you will need an explicit path if you didn't put your script someplace that is in the PATH).

That is all you need, no need for any applescript.

Oh, and be very careful when editing your 'rc' file, if you hose it up somehow OSX won't boot properly


----------



## Darkshadow (May 14, 2003)

Just a note on editing the /etc/rc script - whenever you update OS X (actual system update), the rc script is usually re-written, so you'd have to remember to edit it again after that.

Yes, *pico* will work fine to create a shell script.  You can also use TextEdit, or BBEdit, or whatever text editing program you want - just make sure it's saved as plain text and not rtf (in TextEdit's case).

Here, I'll even help ya out:


```
[b][color=red]#!/bin/sh

#script to change the login pic

LOG_PIC="/Library/Desktop Pictures/Aqua Blue.jpg"
PIC_DIR=~/Pictures
PIC_COUNT=`ls "${PIC_DIR}" | wc -l`
RAND_PIC=`jot -r 1 1 "${PIC_COUNT}"`
PIC=`ls "${PIC_DIR}" | sed -n "${RAND_PIC}"p`
PIC_PATH="${PIC_DIR}/${PIC}"

if [ -h "${LOG_PIC}" ]; then
   rm "${LOG_PIC}"
   ln -s "${PIC_PATH}" "${LOG_PIC}"
else
   #First run - rename Aqua Blue.jpg
   mv "${LOG_PIC}" "/Library/Desktop Pictures/Aqua Blue.old.jpg"
   ln -s "${PIC_PATH}" "${LOG_PIC}"
fi
[/color][/b]
```

(Replace ~/Pictures with the path to wherever the pictures you want to use are - if you're not using the tilde (*~* - that expands to /Users/username, in case you don't know), then put quotes around the path.)

Then, save that to whatever you want to name it, and remember where you saved it at. 

In the terminal, type *chmod +x path_to_shell_script*.

If you want to edit the /etc/rc script and put it there, you'll need to be root or use *sudo*.

*sudo pico /etc/rc*

Put _full_path_to_shell_script_ before the line that says *exit* at the end.

Edit note: Editing the /etc/rc script to add this in will only change the login pic when you reboot, not when you log in.  If you want a different picture everytime you log out, you will need to add it to the login items.  Erm, I still haven't found a way to start a shell script via the login items, I'd guess that you'd need to make an apple script to do it.  I forget the command in applescript that will run a shell script at the moment - someone want to jump in with that?


----------



## scruffy (May 14, 2003)

If you don't want to mess about with the rc scripts (not the "Apple Way", and all that), you could create a startup item in /Library/StartupItems.  You could check the /System/Library/StartupItems folder for the format, maybe use one of them as a starting point


----------



## Jasoco (May 14, 2003)

Thanks, guys. I'll try this and let ya know.


----------



## Darkshadow (May 15, 2003)

Ooh, I just thought of a gotcha with that shell script, Jasoco.  If you do decide to use it with the /etc/rc script, *don't* use a tilde.  Since the rc script runs as root, it would assume you mean _root's_ Picture folder (or whatever else you might have chosen if you used the tilde).  So if you do that, make sure to use the full path to the folder with the pics.   Remember to put quotes around it when you do that! (Note: you really only have to put quotes around it when the path has folders with spaces in it, but it never hurts to be careful.)

Same goes if you decide to make a startup item out of this, those run as root as well.


----------



## Jasoco (May 15, 2003)

I don't keep my Desktop Pictures in my Home, why would I use a Tilde?


----------

