simple applescript q? continuous repeat loops
hi all,
dunno if this is the best place to post this q, but if anyone could help i'm writing a script that will automatically sync my clip drive when i mount it. so far the script works great except that i have set up a master loop to keep the check repeating like this:
set keeprepeating to true
repeat while keeprepeating is true
\t
(main script is here - keeprepeating never changes from true)
\t
end repeat
this keeps repeating the checks, but the problem is that i then can't quite the script without using force quit. is there any less problematic way to put a script in an ever-repeating loop?
thanks
jon
dunno if this is the best place to post this q, but if anyone could help i'm writing a script that will automatically sync my clip drive when i mount it. so far the script works great except that i have set up a master loop to keep the check repeating like this:
set keeprepeating to true
repeat while keeprepeating is true
\t
(main script is here - keeprepeating never changes from true)
\t
end repeat
this keeps repeating the checks, but the problem is that i then can't quite the script without using force quit. is there any less problematic way to put a script in an ever-repeating loop?
thanks
jon
Comments
I presume you want it to check constantly so it can know when the drive is mounted? There is a way to do it elegantly and it isn't documented, but I have some questions first.
Do me a favor and mount the clip drive as you normally would, and then go to Terminal and see what is in the folder /Volumes:
in Terminal, type
cd /Volumes
ls -la
Type those two lines (note the capital "V") with and without the clip drive plugged in and post the output. If we are lucky, I already have written a script that will do what you want.
total 64
drwxrwxrwt 8 root admin 272 Nov 3 15:52 .
drwxrwxr-t 34 root admin 1258 Nov 1 18:58 ..
-rw-rw-rw- 1 studio admin 6148 Oct 15 18:13 .DS_Store
-rwxrwxrwx 1 jB jB 82 Oct 24 15:13 ._CLIP DRIVE
drwxrwxr-t 33 jB jB 1224 Nov 1 18:58 Backup
drwxrwxrwx 1 jB jB 16384 Nov 1 22:14 CLIP DRIVE
drwxrwxrwx 21 jB jB 816 Nov 3 10:42 External
lrwxr-xr-x 1 root admin 1 Nov 1 18:59 jonny's HD -> /
b139182:/Volumes jB$
without:
total 32
drwxrwxrwt 7 root admin 238 Nov 3 15:54 .
drwxrwxr-t 34 root admin 1258 Nov 1 18:58 ..
-rw-rw-rw- 1 studio admin 6148 Oct 15 18:13 .DS_Store
-rwxrwxrwx 1 jB jB 82 Oct 24 15:13 ._CLIP DRIVE
drwxrwxr-t 33 jB jB 1224 Nov 1 18:58 Backup
drwxrwxrwx 21 jB jB 816 Nov 3 10:42 External
lrwxr-xr-x 1 root admin 1 Nov 1 18:59 jonny's HD -> /
b139182:/Volumes jB$
i also want to do the same with the backup & external drives (2 partitions of the same usb drive) but if you can provide me with a script i'm sure i can adapt it
First, a basic plain-English explanation of how to handle this. The way you have it set up could work, but it would loop constantly and chew up all your CPU time, slowing down the machine. You could put in an "idle handler", which would make the system call your script every so many seconds, but that also would not be ideal.
It turns out the AppleScript has a feature called Folder Actions. Now these are usually used to make things happen when a folder gets files added or removed, or the folder is moved. But in their wisdom, the AppleScript team realized that even though you cannot see the /Volumes folder in the Finder, that if an "item" (in this case the CLIP DRIVE) was added to it, then it should behave just like any other folder that had a Folder Action attached - namely it should execute the Folder Action. So this is perfect because it will only run when something is added to the /Volumes folder. So there are 2 steps to solving the problem:
1. Write a script, which will be the Folder Action Script, which is what you want done when any item is added to the /Volumes folder.
2. Run another script to tell the system to "attach" the script you wrote in step 1 to the folder "/Volumes". I already have this script ready.
Now as far as writing the script in step 1, it has to contain what is known as a Folder Action Handler. This is simply the code
on adding folder items to this_folder after receiving these_items
-- insert actions here
end adding folder items to
After you tell the system to attach that script to the folder "/Volumes", the script will be automatically run every time something is added to the folder. The variable "this_folder" tells you the name of the folder that triggered the script, and the variable "these_items" is a list of the items that were added. All you have to do is search the list to see if "CLIP DRIVE" is in the name of any of the added items. Here is a script that I wrote to move the desktop position of the iPod icon whenever the iPod is plugged into the firewire port. It gets added to the /Volumes directory when you plug it in, so that will trigger the script (if there is one attached) that is attached to the /Volumes folder.
on adding folder items to this_folder after receiving these_items
tell application "Finder"
if (these_items as text) contains "iPod" then
set theiPod to (first item of these_items) as text
set the desktop position of item theiPod to {100, 79}
end if
end tell
end adding folder items to
That is the elegant and efficient way to do it - let Cocoa take care of "catching" the event instead of polling for it in your script.
I'm going to bed now but this should give you an idea of how it works. I will help you get the details down tomorrow.
so many many thanks!
really getting into scripting now. just looking for something else to do with it!
Originally posted by jonnyboy
many many thanks! i now have my scripts up and running and attached to volumes folder. it works great! i had previously tried this approach, but attaching to the clipdrive itself, which didn't work.
so many many thanks!
really getting into scripting now. just looking for something else to do with it!
Hey, glad you got it working! Post the scripts if you like - we can always learn something from seeing the finished product.
-- Syncmanager script
on adding folder items to this_folder after receiving these_items
\ttell application "Finder"
\t\tif (these_items as text) contains "CLIP DRIVE" then
\t\t\topen "jonny's HD:Applications:Backups:Clipdrive Batch.snkr"
\t\tend if
\t\tif (these_items as text) contains "External" then
\t\t\topen "jonny's HD:Applications:Backups:External Batch.snkr"
\t\tend if
\t\tif (these_items as text) contains "Backup" then
\t\t\topen "jonny's HD:Applications:Backups:Backup Batch.snkr"
\t\tend if
\tend tell
end adding folder items to
very simple i think!