newbie: how copy files to mounted server?

I’m trying to figure out how write a script that will copy an added file to a given folder to a different folder on a server already mounted. It seems quite simple to me but I can’t figure out how to get applescript to do it. I know the name of the folder that will have the added item, and the name of the folder to which the file is going. The only variable is the filename itself, but theoretically I should be able to just grab the file name of the most recently added item and use that. Any help will be appreciated, I’ve tried to figure this out for a couple of weeks now and am no closer after having tried many different scripting additions, syntax variations, experimentation, etc. Thanks.

Jon

Have you tried recording a script while going through the motions? If you can do this, it shouldn’t take much to adapt it to handle changing file names.

Rob,
Yes I have done a bit of recording, but as far as I can tell, that won’t tell me how to get applescript to get the filename, or how to monitor the folder for newly added items. It does solve the issue of what folder to copy from and to which folder to copy, but that’s about it. Any ideas on the rest? Thanks!

Jon

If you post the script, we might be able to turn it into a folder action (script) that will monitor any folder which it is attached to. As a folder action, it will act on the items as they are added to the folder.

Rob,
Here’s what I’ve come up with so far. If you can find a way to get applescript to know the name of the most recently added file, that would be great. Thanks again!

Jon

tell application “Finder”
activate
mount volume “afp://username:password@xxx.xx.xxx.xxx/Luna/”
end tell

tell application “Finder”
activate
select file “sd.range_03-05-28_1.49_PM.txt” of folder “data” of folder “Subjects” of folder “current motion” of folder “Motion Experiments” of disk “Luna”
copy selection to folder “matlab data” of folder “DATA” of startup disk
end tell

This should be close but be warned that I haven’t tested it. We may need to do something differently.

on adding folder items to theFolder after receiving theAddedItems
	tell application "Finder"
		repeat with item_ in theAddedItems
			duplicate item_ to folder "matlab data" of folder "DATA" of startup disk
		end repeat
	end tell
end adding folder items to

The script, which assumes that Luna is mounted, requires that folder actions be enabled and that this script be attached to “Luna” as a folder action. I’m not exactly sure how it will behave if Luna isn’t mounted (you might need another script to make sure that Luna is always mounted). It also requires that Luna’s window be open (just as if you had double-clicked its icon - this issue can be overcome with a control panel if it’s a problem).

Rob,

Thanks a lot! I will try this tomorrow and let you know what happens.

Jon

Rob,

Wow, that works great! There are just two minor issues.

  1. Currently the script copies items to the correct folder only when things are added to the Luna folder. I want it instead to copy items that are added to the folder:
    Luna/Motion Experiments/current motion/Subjects/data
    How do I specify this?

  2. You mentioned I can defeat the requirement that the window be open for the script to work properly. How do I do this?

Thanks again, I really appreciate it!!

Jon

The folder action should work on any folder that it is attached to and it can be attached to more than one folder if desired. Just attach it to: Luna/Motion Experiments/current motion/Subjects/data

You might also want to detach it from Luna.

Folder Actions Plus is what you need. :slight_smile:

Rob,

I’m sorry, one more question. If I want to reverse the action so that rather than monitoring a mounted folder and copying to a local disk, I monitor a local folder and copy to a mounted disk, how do I specify that in the 4th line of your code? I assume something like:

duplicate item_ to folder “different folder” of folder “DATA” of mounted disk

but applescript doesn’t like it. What is the correct syntax? Thanks again, so much!

Jon

Maybe a regular path will work better.

duplicate item_ to folder “Volume name: folder name:nuther folder name:etc:”

Rob,

Thanks so much!! Everything works great with folder actions plus, and using a regular path name. I’m not sure there’s an emoticon to express my gratitude adequately, so a simple thank you will have to do. Thanks again,

Jon

I’m glad it all came together!! :slight_smile:

Rob (or anyone else who wants to help!),

The wonderful folder action script you helped me with doesn’t seem to like interfacing with an OSX machine. I’m running the script on an OS9 machine, with the folder action attached, but when the computer to which the files will be copied is running OSX, I get an error -10006 (finder error while executing a script). It loads the volume fine, and gives no errors when it is first run, but as soon as I add items to the folder in question (which subsequently are attempted to be copied to the OSX machine), it screws up. I tried enabling folder actions on the OSX machine, and allowing remote apple events from OS9, but no luck so far. Am I missing something obvious? Thanks again for all your help!!

Jon

Hmmm. I don’t have any idea why it won’t work. Maybe if you post the script we’ll be able to see if there’s something that needs to be adjusted for OS X.

Rob,

Here’s the script, largely unaltered except for a change in computer name (to which the files are being copied). FU2 is the name of the OSX machine that should receive the files. Thanks!

Jon

tell application “Finder”
activate
mount volume “afp://username:password@xxx.xx.xxx.xxx/FU2”
end tell

on adding folder items to theFolder after receiving theAddedItems
tell application “Finder”
repeat with item_ in theAddedItems
duplicate item_ to folder “FU2:Yarra HD:DATA:matlab data”
end repeat
end tell
end adding folder items to

tell application “MATLAB”
activate
end tell

Ok, I’m sure that I’m quickly becoming confused but I’ll try. :wink:

First, I don’t know what happens to code that is not included within the folder action handler. I’m referring to the first four and last three lines of the script that you posted. Here’s what I suggest.

Create a dropbox folder on your OS 9 Mac. Any files that should be copied to “FU2:Yarra HD:DATA:matlab data” should be placed in this new folder from now on. Then, attach the following folder action script to the new folder.

on adding folder items to thefolder after receiving theAddedItems
	tell application "Finder"
		activate
		mount volume "afp://username:password@xxx.xx.xxx.xxx/FU2"
		repeat with item_ in theAddedItems
			duplicate item_ to folder "FU2:Yarra HD:DATA:matlab data"
		end repeat
	end tell
	
	tell application "MATLAB"  to activate 
end adding folder items to

I think that this will do what you need it to do but it wouldn’t surprise me to learn that I’m wrong. One thing that I’m unsure of is why MATLAB needs to be activated. Does this load the new data?

Rob,

Sorry if the different username confuses you, I’m having password issues…

Yes, I have created a dropbox folder on my OS9 machine, and the script I listed is attached to it. Matlab is just a program I use, its activation is of no consequence to the present issue. So as far as I can tell, I’m doing it exactly how you suggest. Perhaps this is a more general issue with my OSX machine, I never had a problem doing the exact same thing with OS9…

Jon

Did you try the last script that I posted? I can see no reason why the script would fail in OS X unless the path to the data folder is incorrect. Is this correct?

FU2:Yarra HD:DATA:matlab data

Or should it be:

FU2:Yarra HD:DATA:matlab data:

You can run this scrippet (script snippet) on the OS X machine to get the correct path. Just choose the matlab data folder. The correct path should be available in the result window in Script Editor.

set foo to (choose folder) as text
display dialog foo
foo

Rob,

I did try your version of the script, I got the same problem (error -10006). The only reason I had the two parts of the code separated is because there might be multiple files that are added to the dropbox sequentially, thus I only need load FU2 once, and then the copy should work. I’ll mess with the folder name snippet you posted tomorrow, I don’t have access to the OSX machine right now. Thanks for all your help!!!

Jon