Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Hey Guys. I'm new to writing applescript, never wrote any code in my life, and I'm trying to learn it on the fly. I've tried to read up in some tutorials, but I'm having a hard time picking up the lingo. I'm trying to do the following basic script, and I was wondering if anyone can help. It will be added to a script I am currently writing with other actions, but this is crucial. Anything would be greatly appreciated. Thanks!

***There will be three folders. Folder "A" will be on the desktop permanently, folder "B" will only be created on the desktop temporarily, and folder "C" will be a sub-folder within folder "A"***

***I want this script to run automatically when any new file is added to folder "A"***

1) A file is added to Folder "A"
2) Finder creates folder "B" on the desktop
3) Finder selects all files in Folder "A" and moves them to Folder "B"
4) Finder selects all files in Folder "B" and moves them to sub-folder "C"
5) Finder Deletes folder B.

*** It sounds basic and pointless, but there will be other actions added later***

Thanks so much for anyone's help.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Why not just create folder C and move the files from A into there? Is there some significance/importance to the temporary folder B?

I don't know if you can have an applescript triggered when a folder's contents change. You might have to have the script always running, sleeping for a few minutes at a time, and waking up and checking if there's anything new. Alternately you could schedule the task to run every minute or something. It wouldn't be immediate, but pretty close.

Does folder C need to be named something like the time it was created? is there just one folder C that every new item in A gets moved to, or one created each time new files are found?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
Why not just create folder C and move the files from A into there? Is there some significance/importance to the temporary folder B?

I don't know if you can have an applescript triggered when a folder's contents change. You might have to have the script always running, sleeping for a few minutes at a time, and waking up and checking if there's anything new. Alternately you could schedule the task to run every minute or something. It wouldn't be immediate, but pretty close.

Does folder C need to be named something like the time it was created? is there just one folder C that every new item in A gets moved to, or one created each time new files are found?
You can have a script run whenever new files are added to a folder. This support is called Folder Actions, though I don't know how you turn it on nor do I know how to attach a script to a folder and schedule it to run when new files are added.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
You can have a script run whenever new files are added to a folder. This support is called Folder Actions, though I don't know how you turn it on nor do I know how to attach a script to a folder and schedule it to run when new files are added.

That's pretty cool, though I wonder what the result of that would be in this case. If you had a bunch of files you were moving in there, it seems like it would grab each one as they were copied in, so they'd all get their own folder.

I'm at work, so I don't have a mac handy to look at applescript options, but the following shell script could be invoked from an applescript and should do the job. Obviously the path ./testscript/A would need to be replaced the 4 times its used with the directory path you're actually interested in.

Code:
count=`find ./testscript/A -type f -maxdepth 1 -print | wc -l`
echo $count
if [ $count != 0 ];
  then
    foldername=`date +%Y%m%d%H%M`
    mkdir ./testscript/A/$foldername
    find ./testscript/A -type f -maxdepth 1 -print | xargs -i mv {} ./testscript/A/$foldername
  else
    echo "No new files"
fi
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Folder B is Significant

Folder B is significant. The explanation just doesn't apply to the script help I'm looking for, and I am just trying to keep it as simple as possible for you guys. It will be used in conjunction with another script I am writing. Folder C is just a place I want all the original files added to folder A to end up. Since the script should be triggered everytime something is added, and I don't want to delete any original files, this is just a place the files can end up. I thought that if I sent them back to Folder A, it would just become a never ending running script. Because the files are constantly being added back to folder A.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Folder B is significant. The explanation just doesn't apply to the script help I'm looking for, and I am just trying to keep it as simple as possible for you guys. It will be used in conjunction with another script I am writing. Folder C is just a place I want all the original files added to folder A to end up. Since the script should be triggered everytime something is added, and I don't want to delete any original files, this is just a place the files can end up. I thought that if I sent them back to Folder A, it would just become a never ending running script. Because the files are constantly being added back to folder A.

That becomes a very different problem. When you said "move" originally, to me that meant they aren't being copied, but actually moved. I'm not sure how you could tell what hasn't been "archived" yet in a very straightforward manner if all of the files are still in the original directory. Maybe some others can chime in on how "folder actions" might help here.

-Lee
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
That becomes a very different problem. When you said "move" originally, to me that meant they aren't being copied, but actually moved. I'm not sure how you could tell what hasn't been "archived" yet in a very straightforward manner if all of the files are still in the original directory. Maybe some others can chime in on how "folder actions" might help here.

-Lee

You are right. Nothing is being copied. The files should literally be moving folder to folder. I'm sorry I'm being confusing. I'm using folder C as a way to ensure that nothing is being duplicated, or archived twice. If they end up in folder C, I know those files have already been added to folder A once, and the script has already run it's course.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
You are right. Nothing is being copied. The files should literally be moving folder to folder. I'm sorry I'm being confusing. I'm using folder C as a way to ensure that nothing is being duplicated, or archived twice. If they end up in folder C, I know those files have already been added to folder A once, and the script has already run it's course.
I would suggest that you do something different, since your script will still run endlessly unless it excludes folders that it created... I suggest the following setup instead:
  • Folder B is a drop folder that can be stored wherever you like. This folder will activate the script.
  • Folder A will live on the desktop, and contain other folders that the script creates.
  • Folder C, a subfolder of folder A, will be created by the script.
Then, you drop a file into folder B, and the script automatically files it into a subfolder of folder A.
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
I would suggest that you do something different, since your script will still run endlessly unless it excludes folders that it created... I suggest the following setup instead:
  • Folder B is a drop folder that can be stored wherever you like. This folder will activate the script.
  • Folder A will live on the desktop, and contain other folders that the script creates.
  • Folder C, a subfolder of folder A, will be created by the script.
Then, you drop a file into folder B, and the script automatically files it into a subfolder of folder A.

wouldn't that make a new folder "C" every time a file was dropped into "B"then? ) Meaning duplicate "C" folders?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
wouldn't that make a new folder "C" every time a file was dropped into "B"then? ) Meaning duplicate "C" folders?
Not necessarily. If the script is written right, it should determine that the destination already exists, and not create a new folder - just move the file where it needs to go.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Is "C" a fixed folder, or do you want a new one created with, say, a datestamp each time files are copied? The script provided above will create a new one, but it is pretty easy to modify to have everything just copied into one archive directory. It would also be pretty easy to have a different folder that is checked for things to move, there's only a few lines that would need to be modified.
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Is "C" a fixed folder, or do you want a new one created with, say, a datestamp each time files are copied? The script provided above will create a new one, but it is pretty easy to modify to have everything just copied into one archive directory. It would also be pretty easy to have a different folder that is checked for things to move, there's only a few lines that would need to be modified.

I'm looking for it to be a fixed folder if possible.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I'm looking for it to be a fixed folder if possible.

This, while shell and not applescript, should do the job:
Code:
count=`find ./testscript/B -type f -maxdepth 1 -print | wc -l`
if [ $count != 0 ];
  then
    foldername="./testscript/A/archive"
    if [ ! -d $foldername ];
      then
        mkdir $foldername
    fi
    find ./testscript/B -type f -maxdepth 1 -print | xargs -i mv {} $foldername
  else
    echo "No new files"
fi

Replace ./testscript/B in both instances with the "drop" folder you want to use. Note that this currently excludes directories because the original goal was to have the archive be a subdirectory of the drop directory. This can be pretty easily changed.

Replace ./testscript/A/archive with the directory you want to archive to. If it doesn't exist, the script will create it.

It should be easy to run this script from an applescript if you need the script to be triggered in some way. I would personally stick it in cron every 5 min. or so, but if the need is more immediate folder actions might be better.

I'm sure there's a pretty easy way to do this in applescript, but I couldn't test that here, and wouldn't want to post something that I haven't tested.
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
This, while shell and not applescript, should do the job:
Code:
count=`find ./testscript/B -type f -maxdepth 1 -print | wc -l`
if [ $count != 0 ];
  then
    foldername="./testscript/A/archive"
    if [ ! -d $foldername ];
      then
        mkdir $foldername
    fi
    find ./testscript/B -type f -maxdepth 1 -print | xargs -i mv {} $foldername
  else
    echo "No new files"
fi

Replace ./testscript/B in both instances with the "drop" folder you want to use. Note that this currently excludes directories because the original goal was to have the archive be a subdirectory of the drop directory. This can be pretty easily changed.

Replace ./testscript/A/archive with the directory you want to archive to. If it doesn't exist, the script will create it.

It should be easy to run this script from an applescript if you need the script to be triggered in some way. I would personally stick it in cron every 5 min. or so, but if the need is more immediate folder actions might be better.

I'm sure there's a pretty easy way to do this in applescript, but I couldn't test that here, and wouldn't want to post something that I haven't tested.

Thanks, I will try it out ASAP. BTW. Do you know any book or website that spells out the basics of writing applescript in crayon? With no programming backround, this is a bit confusing. I just want to be able to look at these scripts and understand what each commands does. As opposed to having someone else write a script for me and I just do the 'ol copy, paste. : )
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Thanks, I will try it out ASAP. BTW. Do you know any book or website that spells out the basics of writing applescript in crayon? With no programming backround, this is a bit confusing. I just want to be able to look at these scripts and understand what each commands does. As opposed to having someone else write a script for me and I just do the 'ol copy, paste. : )

I copied the script into script editor and tried to run it and I got the following error:

"Syntax Error, Expected Expression but found unknown token."

Any Ideas?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Since it's shell and not applescript, you will have to, unfortunately, get your hands a bit dirtier than you're used to.

You can paste the script into textedit, make the necessary modifications to the folders and save it as plaintext with the name archive.sh. Put it on your desktop for now.

Go to Applications->Utilities and open Terminal.
Type 'cd Desktop' and press enter
Type 'chmod 711 archive.sh' and press enter
Type './archive.sh' to test the script. It should move any files in the "drop" folder to the archive folder

In your applescript, you can then run it with:
do shell script "~/Desktop/archive.sh"

Hopefully someone with more applescript experience knows a way to do it in all applescript. I can check it out when I get home if no one has been able to help on that front and you can't get the shell script going.

-Lee
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Since it's shell and not applescript, you will have to, unfortunately, get your hands a bit dirtier than you're used to.

You can paste the script into textedit, make the necessary modifications to the folders and save it as plaintext with the name archive.sh. Put it on your desktop for now.

Go to Applications->Utilities and open Terminal.
Type 'cd Desktop' and press enter
Type 'chmod 711 archive.sh' and press enter
Type './archive.sh' to test the script. It should move any files in the "drop" folder to the archive folder

In your applescript, you can then run it with:
do shell script "~/Desktop/archive.sh"

Hopefully someone with more applescript experience knows a way to do it in all applescript. I can check it out when I get home if no one has been able to help on that front and you can't get the shell script going.

-Lee

Thanks, I'd really appreciate it!
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
Thanks, I'd really appreciate it!

I followed your directions, and this is the response I got from both the Terminal App and Script editor:

"xargs: illegal option -- i
usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
[-L number] [-n number [-x] [-s size] [utility [argument ...]]"


Does that mean anything to you? Might as well be German to me. : )
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I followed your directions, and this is the response I got from both the Terminal App and Script editor:

"xargs: illegal option -- i
usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
[-L number] [-n number [-x] [-s size] [utility [argument ...]]"


Does that mean anything to you? Might as well be German to me. : )

I guess our versions of xargs are different... but try switching xargs -i to xargs -I and see if that helps.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I'll see if i can craft it into something workable when i get home in a bit. The parameters for the version of xargs on OS X is clearly a bit different than on linux, but I'm sure it can be reconciled.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Code:
count=`find ./testscript/B -type f -maxdepth 1 -print | wc -l`
if [ $count != 0 ];
  then
    foldername="./testscript/A/archive"
    if [ ! -d $foldername ];
      then
        mkdir $foldername
    fi
    find ./testscript/B -type f -maxdepth 1 -exec mv '{}' $foldername \;
  else
    echo "No new files"
fi

Got rid of the xargs and switched to just using exec with find instead.

This should do it.

-Lee
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
One more reply...

This page has a script that you should be able to modify to copy/move files in applescript, and how to activate it with folder actions:

http://www.bradrice.com/wposx/archives/68

I'd just be copying or modifying that to try to get it going with applescript, so I thought it would be better to just give you the link.

-Lee
 

thriftinkid

macrumors regular
Original poster
Mar 24, 2008
119
0
One more reply...

This page has a script that you should be able to modify to copy/move files in applescript, and how to activate it with folder actions:

http://www.bradrice.com/wposx/archives/68

I'd just be copying or modifying that to try to get it going with applescript, so I thought it would be better to just give you the link.

-Lee

Thanks Man. I really can't thank you enough for all your help.
 

macfaninpdx

macrumors regular
Mar 6, 2007
198
0
Also, have you looked at Automator? I am not in front of my mac right now, but IIRC Automator should be able to handle all of that pretty easily. And you can attach it to a folder to use as a Folder Action script.

Also there are some example AppleScripts in the AppleScript folder which show some finder/folder examples. This may give you some help too.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.