Select files from a sub folder and copy to another volume

Hi Scripters

I’m not sure if I should be looking at applescript or automator for this one. I’ll run through the sequence of events we need to happen - hope it makes sense:

when a new folder (folder “xyz” - the name will change all the time) is added to “/Volumes/HN Server/To Archive” copy the CONTENT of the “h_Collected Job” folder which is inside the newly added folder (folder xyz) to “Volumes/Job Archive/_to be catalogued”

The “h_Collected Job” folder can contain pdfs, mp3s, mpegs, all sorts of files.

Is this possible and how would we go about setting up such a sequence?

Thanks

This isn’t tested, but it’s the standard way to approach it:

on open droppedIn
	set Dest to "alias YourHD:Volumes:HN Server:To Archive" -- take alias out of the quotes.
	tell application "Finder"
		set F to folders of droppedIn -- the contents.
		repeat with aFldr in F
			if name of aFldr is "h_Collected Job" then
				try
					set tContents to files of entire contents of aFldr as alias list
				on error
					set tContents to files of entire contents of aFldr as alias as list
				end try
				move tContents to Dest -- you don't say what to do with same names
			end if
		end repeat
	end tell
end open

Hi Adam

Thanks for posting…I don’t think I’ve explained correctly…sorry

When a folder of a random name is added to “HN Server:To Archive:”
look inside newly ‘droppedIn’ folder and find “h_Collected Job” folder
grab the content of “h_Collected Job” and
copy to “Job Archive: _to be catalogued:”
if that file is already there…replace it (didn’t have this bit before but you’re probably right to suggest dealing with it)

The following does what I want it to do - sort of. It just it only fires when 'h_collected Job" is added to “To Archive”. I need it to fire when any folder is added to “To Archive” then look inside that new folder for the “h_Collected Job”

It is also possible that multiple folders will be added to “To Archive” at once so this would have to be accounted for.

on adding folder items to this_folder after receiving added_items
	set Dest to alias "Job Archive: _to be catalogued:"
	tell application "Finder"
		set F to folders of this_folder -- the contents.
		repeat with aFldr in F
			if name of aFldr is "h_Collected Job" then
				try
					set tContents to files of entire contents of aFldr as alias list
				on error
					set tContents to files of entire contents of aFldr as alias as list
				end try
				move tContents to Dest with replacing
			end if
		end repeat
	end tell
end adding folder items to

I am sure we are close to a solution - but being a newbie…I just can’t see it.

Thanks

HN

Sorry - I understood that you wanted a droplet - an application that acted on files or folders dropped on it. What you want is a Folder Action. At first glance, I don’t see what’s wrong. It should be looking inside whatever is dropped - this_folder is that - so F is the contents of the folder that was dropped no matter it’s name. Then if any folder within this_folder has the right name, we get it’s contents and move them. You can’t have two folders with that name.

What part of that doesn’t work?

Hiya

Anything dropped in to “this_folder” (To Archive) won’t have the right name…we need to get it to look one more level down - inside the folder dropped in this_folder.

So…the folder heirachy is:
“To Archive:Job Folder XYZ:h_Collected Job”

“To Archive” is the one with the folder action attached to it.

Hi Adam

Thank you very much for your help so far. I have played around with your script but am unable to to the final thing to make it work. Do you need any more/better information from me?

Thanks

HN

Can you be a bit more specific about what doesn’t work, hn?

I think this might be closer to what you want:


on adding folder items to this_folder after receiving added_items
	--set Dest to alias "Job Archive: _to be catalogued:" -- commented so the script will compile for me
	tell application "Finder"
		set F to folders of this_folder -- the contents.
		repeat with aFldr in F
			try
				set tContents to folders of entire contents of aFldr as alias list
			on error
				set tContents to folders of entire contents of aFldr as alias as list
			end try
			repeat with inFldr in tContents
				if name of inFldr is "h_Collected Job" then move inFlder to Dest with replacing
				exit repeat -- assuming only one such folder
			end repeat
		end repeat
	end tell
end adding folder items to

Hi

This version that you did works perfectly. Provided that ‘h_Collected Job’ is the name of the folder added to the source folder. However, ‘h_Collected Job’ is inside a folder that is added to the source folder. The script needs to look inside the folder added to the source folder and locate ‘h_Collected Job’ then copy the contents. ‘h_Collected Job’ will be in every folder added to the source folder as it is part of our job folder system here in the agency.

on adding folder items to this_folder after receiving added_items
   set Dest to alias "Job Archive: _to be catalogued:"
   tell application "Finder"
       set F to folders of this_folder -- the contents.
       repeat with aFldr in F
           if name of aFldr is "h_Collected Job" then
               try
                   set tContents to files of entire contents of aFldr as alias list
               on error
                   set tContents to files of entire contents of aFldr as alias as list
               end try
               move tContents to Dest with replacing
           end if
       end repeat
   end tell
end adding folder items to

Thanks

Your comments apply to a version earlier than the one in my last post. What about it - it’s different. Did you test that?

Hi

Your post of 2007-02-22 06:56:08 does not produce any results. And as I am not sure what is happening at each line of the code I am not sure where it is not working.

However, my post of 2007-02-22 07:19:43 still stands.

OK hn, I’ll try to go thru each line with you,

this_folder is the folder you set the folder action to
added_items are your “XYZ” folders that contain “h_Collected Job”

uncomment this line and replace “Job Archive: _to be catalogued:” with the path to the destination

You may want to change this line to:

set F to folders of added_items

But as you are always moving things out, it’s fine the way Adam put it.

This line starts a loop where aFldr is one of your “XYZ” folders

now we have all the folders in “XYZ” in tContents

start a loop thru the folders in “XYZ” where inFldr is potentialy your folder “h_Collected Job”

Now we found “h_Collected Job” and move it with it’s entire contents to “Job Archive: _to be catalogued:” replacing any existing folder “h_Collected Job”
All You have to do is change inFlder to aFldr so that the “XYZ” folder is moved

As we have found and moved, better exit the “inner” loop

Continue “outer” loop to see if there are any other “XYZ”'s

Thanks Adam, you do a great job on this forum.
hn, if you still don’t get it to work, try explaining more precisely what the script is doing for you and what it is still lacking. Try using other wording (except for the “vars”), Sometimes this can help a lot.

Thanks for taking the time to explain the script SwissalpS.

I too appreciate the time Adam has taken in creating a script that will ultimately save us time - not him.

The last script Adam has done does not yield any results. That is, with the folder action applied nothing gets copied to the destination folder. That is why I went back the the older version of his script and said that it works - because it copies the contents of the ‘h_Collected Job’ folder to the destination - but it would only do that if the folder added to ‘this_folder’ was named ‘h_Collected Job’.

This is the latest script - but it does not provide a result or any errors.


on adding folder items to this_folder after receiving added_items
   set Dest to alias "Job Archive: _to be catalogued:"
   tell application "Finder"
       set F to folders of this_folder -- the contents.
       repeat with aFldr in F
           try
               set tContents to folders of entire contents of aFldr as alias list
           on error
               set tContents to folders of entire contents of aFldr as alias as list
           end try
           repeat with inFldr in tContents
               if name of inFldr is "h_Collected Job" then move inFlder to Dest with replacing
               exit repeat -- assuming only one such folder
           end repeat
       end repeat
   end tell
end adding folder items to

But folders added to ‘this_folder’ won’t be named ‘h_Collected Job’ they will be all named different things but they all will have a folder inside called ‘h_Collected Job’. We place MPEGS, PDF, MP3 etc inside this folder to show what the final job our ad agency produced looks like. We have a server volume called ‘Job Archive’ to archive this to so that we have ready access to the material for reference.

I will try and play with the script that works and see if I can take elements from the latest script to make it look inside newly added folder for ‘h_Collected Job’ and copy it’s content.

This is the script that works provided new folders added to ‘this_folder’ are named ‘h_Collected Job’. It just needs to look inside newly added folders for the ‘h_Collected Job’ folder and copy its content to the destination volume


on adding folder items to this_folder after receiving added_items
	set Dest to alias "Job Archive: _to be catalogued:"
	tell application "Finder"
		set F to folders of this_folder -- the contents.
		repeat with aFldr in F
			if name of aFldr is "h_Collected Job" then
				try
					set tContents to files of entire contents of aFldr as alias list
				on error
					set tContents to files of entire contents of aFldr as alias as list
				end try
				move tContents to Dest with replacing
			end if
		end repeat
	end tell
end adding folder items to

As i said, i will try and have a play - but if anyone can see where to tweak the script in the meantime I would appreciate hearing from you.

Again, thank you both for very much for your time.

HN

Hi hn,
is there a specific reason to use folder action instead of a droplet?

regards
Luke SwissalpS

My inexperience in using both might tell here…

I thought that if we have a folder action, if anyone puts a new folder into ‘this_folder’ then it it will trigger the script rather than having them drag the folder over a droplet. I am trying to have the least amount of human interaction as possible.

I think I have found the typo that caused the problem.
The original line was:

if name of inFldr is "h_Collected Job" then move inFlder to Dest with replacing

As I had suggested when going thru the lines, replace inFlder with aFldr if you want to move the folder containing “h_Collected Job”
The line would look like this:

if name of inFldr is "h_Collected Job" then move aFldr to Dest with replacing

On the other hand, if you just want to move “h_Collected Job”, then replace inFlder with inFldr.

Testing folder actions or droplets doesn’t give me comfortable error-reports, so I make hybrids to test them in Script Editor.
In this case I Came up with this:


property Dest : alias "Job Archive: _to be catalogued:"

-- the folder action part untested by myself as hn will be doing that
on adding folder items to this_folder after receiving added_items
	carryOn(this_folder, added_items)
end adding folder items to

-- applet part. tested and worked on my setup
on run
	set this_folder to alias "HN Server:To Archive:" -- source folder
	tell application "Finder"
		try
			set added_items to folders of entire contents of this_folder as alias list
		on error
			set added_items to folders of entire contents of this_folder as list
		end try
	end tell
	carryOn(this_folder, added_items)
end run

-- droplet part tested and worked fine on my setup
on open added_items
	set this_folder to alias "HN Server:To Archive:" -- source folder
	tell application "Finder"
		move added_items to this_folder with replacing
	end tell
	carryOn(this_folder, added_items)
end open

-- the actual work is done here
to carryOn(this_folder, added_items)
	tell application "Finder"
		set F to folders of this_folder -- the contents.
		repeat with aFldr in F
			log aFldr
			try
				set tContents to folders of entire contents of aFldr as alias list
			on error
				set tContents to folders of entire contents of aFldr as alias as list
			end try
			repeat with inFldr in tContents
				if name of inFldr is "h_Collected Job" then move aFldr to Dest with replacing
				exit repeat -- assuming only one such folder
			end repeat
		end repeat
	end tell
end carryOn

please let us know if it’s doing what you wanted it to perform. :smiley:

I tested your latest version - and made a droplet. It copies the folder dragged over the droplet to “HN Server:To Archive” but doesn’t then look in side the folder dragged over the droplet for the ‘h_Collected Job’ folder and copy its contents to ‘Job Archive: _to be catalogued’

so you want only the files in ‘h_Collected Job’ folder to be moved to Dest?

Did you run my last version in Script Editor?
Did you get any errors?

added (moments later…):
Did you change anything in the script?

Yes, i used the latest script. I did not change anything. It all goes through fine. I had a look at the even log and it produces this:

tell application “Finder”
get every folder of entire contents of alias “HN Server:To Archive:”
{alias “HN Server:To Archive:ztest:”, alias “HN Server:To Archive:ztest:a_Print Files:”, alias “HN Server:To Archive:ztest:b_EPS Vector Files:”, alias “HN Server:To Archive:ztest:c_Images:”, alias “HN Server:To Archive:ztest:d_Other:”, alias “HN Server:To Archive:ztest:e_Text Files:”, alias “HN Server:To Archive:ztest:f_pdf Files to Client:”, alias “HN Server:To Archive:ztest:g_pdf Files From Client:”, alias “HN Server:To Archive:ztest:h_Collected Job:”, alias “HN Server:To Archive:ztest:e_Text Files:_approved copy:”, alias “HN Server:To Archive:ztest:g_pdf Files From Client:_approved/signed PDF:”}
get every folder of alias “HN Server:To Archive:”
{folder “ztest” of folder “To Archive” of disk “HN Server”}
(folder ztest)
get every folder of entire contents of folder “ztest” of folder “To Archive” of disk “HN Server”
{alias “HN Server:To Archive:ztest:a_Print Files:”, alias “HN Server:To Archive:ztest:b_EPS Vector Files:”, alias “HN Server:To Archive:ztest:c_Images:”, alias “HN Server:To Archive:ztest:d_Other:”, alias “HN Server:To Archive:ztest:e_Text Files:”, alias “HN Server:To Archive:ztest:f_pdf Files to Client:”, alias “HN Server:To Archive:ztest:g_pdf Files From Client:”, alias “HN Server:To Archive:ztest:h_Collected Job:”, alias “HN Server:To Archive:ztest:e_Text Files:_approved copy:”, alias “HN Server:To Archive:ztest:g_pdf Files From Client:_approved/signed PDF:”}
get name of alias “HN Server:To Archive:ztest:a_Print Files:”
“a_Print Files”
end tell

Your situation is now much clearer to me :cool:
The magic is in the if-block of the inner loop:

repeat with inFldr in tContents
	if name of inFldr is "h_Collected Job" then move aFldr to Dest with replacing
	exit repeat -- assuming only one such folder
end repeat

this way, only 1 folder’s name is looked at and then the loop is quited anyway
Change it to this syntax:

if [query true] then
    	[statements]
end if

another approach might also be to replace the inner loop with something like this:


try
 	   set thegreatfolder to ((added_folder as string) & "h_Collected Job") as alias
	    move entire contents of thegreatfolder to Dest
on error
 	   -- "h_Collected Job" doesn't exist or  there  was  an  error while copying
end try

Just an idea… I don’t really like using the error trigger this way…

i am a total dummy. Could you show me a complete script of what you mean?