This is my first post here…I’m completely new to all of this and have a few questions if that’s ok.
I’m wondering if it would be possible to use Automator or Apple Script to copy a file from a shared public folder (named Public) to the folder library/webserver/documents
I’m wondering if this can be done automatically, that is as soon as a specifically named file called “calendar.xls” is dropped into the folder Public it is instantly and automatically copied to the library/webserver/documents folder.
I’ve played with Automator and found that I can make an applet that I have to manually run in order to have the file copied.
Is it possible to somehow do this without having to manually initiate the action with a double click? Would anyone be able to provide me an example script that I can look at to start to see the process?
Also are there some good, basic in lay-terms, books or tutorials to get a person started in Apple Script and Automator. I find that Apples docmentation is lacking greatly.
Hi,
not sure how much you know so forgive me if going over stuff you already know. The script below needs to be saved into your Library/Scripts/Folder Action Scripts folder. Once you’ve done this then Control Click on the current Users Public folder, this will present you with a window, select Configure Folder Actions. In the new window select you add your Public folder to the left hand window and add the saved script in the right one. Once you’ve done this the next time you add your “calendar.xls” file to the Public folder it’ll be moved into the library/webserver/documents folder. Any other files will be left alone.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
repeat with this_file in added_items
set filename to the name of this_file
display dialog filename
if filename = "calendar.xls" then
move this_file to "Macintosh HD:library:webserver:documents"
end if
end repeat
end tell
end adding folder items to
This may work for you. As for books, I personally like Applescript - a comprehensive guide to scripting and automation on Mac OS X by Hanaan Rosenthal. It’s a bit of a slog at times but I have found it invaluable.
on adding folder items to this_folder after receiving added_items
repeat
set a to (info for alias this_folder with size) -- Check the folder size
delay 1 -- Wait # second(s)
set b to (info for alias this_folder with size) -- Check again to see if it has changed
if a = b then exit repeat
end repeat
tell application "Finder"
set lItemList to (list folder this_folder without invisibles) -- What files/folders were dropped?
repeat with i from 1 to (count of lItemList)
set lItem to (item i of lItemList as string) -- Pull item i from list
if name extension of (info for alias (this_folder & lItem as Unicode text)) is "xls" then -- If Excel file...
move lItem to "Macintosh HD:library:webserver:documents"
end if
end repeat
end tell
end adding folder items to
Thanks for the replies! I’m going to check into that particular book tonight on Amazon.com…I went to a large book retailer this afternoon and had no luck finding a book on this subject, but I’m sure Amazon will have it and will probably be better priced too.
I’m playing around with your submitted scripts and having fun with them. Thanks so much it your input has been spot on! At the moment I’m playing with the script submitted by Blend3 and find that after modifying it slightly (my HD is named differently) it is trying to work. But what is happening is this:
When the “calendar.xls” file is dropped in to the Public folder and the script runs my Mac is popping up a small window that says “calendar.xls” Cancel or OK. Only after clicking on OK does it move to the desired folder.
Is there a command I can add to by pass this step and have the file just proceed to the desired folder?
To be honest, you can strip this out of my script if you want also. It is really only there in case multiple files get dropped on the folder at once and you don’t want the rest of the script kicking off before they have finished. I’m dealing with large files and a lot of them so I might extend the default timeout just for grins.
repeat
set a to (info for alias this_folder with size) -- Check the folder size
delay 1 -- Wait # second(s)
set b to (info for alias this_folder with size) -- Check again to see if it has changed
if a = b then exit repeat
end repeat
Thanks…I thought that was the case and I tried it earlier today (before posting back to you) but I found that editing that out did not work right…but I might have screwed up. I’ll go back and try that again. Oh, and thanks for being so thorough earlier in your first post. I needed to know everything you posted, from where to put the script, and how to associate it with the Public folder.
Rectal Exam,
I ran the script you suggested and had some trouble with it…it did not work for me. But I’ve not played witih it much yet at all. I plan too though 'cause I want to see how it works as well. I started with Blend’s first because he sumitted it first. I suspect I’m
to learn a good bit from both methods. I’ll play with your script as you originally submitted and then edit it as suggested in your last post too.
I’m off to Amazon.com now to see if there is a copy of the Rosenthal book for sale, maybe some others too.
I’ll keep you two posted on this. It really is interesting to me…a bit confusing at this point…but interesting for sure.
Found it…just picked up the Rosenthal Book (2nd editioin) from an Amazon vendor…got a new one for $10.43 plus $3.99 shipping. While I was at it I found another book on scripting that looked good called “Beginning Apple Script (programmer to programmer)” and bought it too for $11.00 plus $3.99 shipping. Both deliverred for under $30…not too bad.
I hope they help me in my Apple Scripting adventures.
Ok…I’m back to playing with these scripts. I’ve been able to get the script (below) to work up to a point now. I did screw up last night (as I mentioned previously) and upon retrying I was able to get around the popup window on the Mac that asked for a OK or CANCEL by removing that one line of script. So now basically the script is moving the file from the Public Folder to the Library/WebServer/Documents folder. But what I am finding is that if the file calendar.xls already exists within the Library/WebServer/Documents folder the script is not over writing it…which I would need it to do. I’m thinking that it is possible to enter into the script a command that checks for the file and allows it to overwrite if the file is there.
Am I right? Wouldn’t this command go into the script somewhere after the "move this_file to “Macintosh HD:library:webserver:documents” line? What command would this be?
on adding folder items to this_folder after receiving added_items
tell application “Finder”
repeat with this_file in added_items
set filename to the name of this_file
if filename = “calendar.xls” then
move this_file to “Macintosh HD:library:webserver:documents”
end if
end repeat
end tell
end adding folder items to
I can’t wait 'til my books come, but it won’t be for another week.
Thanks for the input so far…it’s been fun and very helpful
Thanks for the reply…makes me feel good to know I was thinking on the right track.
However, your suggestion (which worked by the way) was simplier than I expected it to be. I was imagining some type of IF statement…something a bit more elaborate. I like that it was as simple as adding the “replacing yes” statment…I suppose that maybe there is some truth to the rumors that Apple Script can be simple. However, I’ll find out more about that when I get some decent documentation next week or so.
I’m off now to see why I can’t get the other script that Rectal Exam submitted to work.