Need help sorting files to different folders based on file name

Hello MacScripters,
I am very new to Applescript, and I’m trying to do something that seems easy, but I just can’t seem to find the correct solution. I want to seperate files with the prefix of “pola” from other files located inside a collection folder and then duplicate those files to another folder for archiving. The workflow chain includes three folders: A drop folder, where all images are collected, and two folders for archiving, one archive that archives all image files, and a second folder which only includes files with the prefix “pola”.

I have read what I can from this site, including the Scripting for Beginners, and I found a solution to the first part of the script, duplicating all the files to backup, but the code I’ve written for the second part, the seperation of files, does not work. Any help, suggestions, or direction to research would be a great help.

Thanks in advance for your knowledge,
David

My script so far:

on adding folder items to this_folder after receiving these_items
tell application “Finder”
duplicate these_items to “love:users:david:desktop:edit” without replacing
end tell
tell application “Finder”
if these_items begins with “pola” then duplicate to “love:users:david:desktop:pola” without replacing
end tell
end adding folder items to

Model: Mac G5
AppleScript: 2.1
Browser: Firefox 1.5.0.1
Operating System: Mac OS X (10.4)

In your script, “these_items” is not the name of the file.

I rewrote your script using a folder action I have that requires the name of a file. I haven’t tested it (and I’m new to this, too) so I hope it works.

on adding folder items to Files_to_Sort after receiving these_Files
	repeat with this_file in these_Files
		
		tell application "Finder"
			set the_file to this_file as alias
			set named_file to name of the_file
			if named_file begins with "pola" then
				
				duplicate named_file to "love:users:david:desktop:pola" without replacing
			else
				duplicate named_file to "love:users:david:desktop:edit" without replacing
				
			end if
		end tell
	end repeat
end adding folder items to

j

Thanks for the speedy reply Capitalj,

The code didn’t work for me. The code didn’t copy any of the files to either folder.

Also, I’m confused on why I would make an alias. Is there a reason to make an alias for items I’m copying or moving?

Do you think I should be using a sort command instead? Any thoughts would be welcome.

Best,
David

Sorry about that. I wonder what I did wrong. I only spent a couple of minutes on this because it seemed similar to a problem I had earlier with file names.

For that, I used what I learned here - http://bbs.applescript.net/viewtopic.php?id=17441

in this - http://bbs.applescript.net/viewtopic.php?id=17022

If I understand your first post correctly, this part works:

on adding folder items to this_folder after receiving these_items
    tell application "Finder"
        duplicate these_items to "love:users:david:desktop:edit" without replacing
    end tell

but this doesn’t:

tell application "Finder"
        if these_items begins with "pola" then duplicate to "love:users:david:desktop:pola" without replacing
    end tell

The first problem, it seems to me, is that “these_items” as written refers to the group of files dropped. I would think that every file is duplicated in the first section of your script and ignored by the second. Is that correct? I thought the repeat loop would suffice to sort through the files and fix the script. I haven’t used sort commands yet - I am about to read up on them - so I can’t really advise about them.

The second problem I (think I) saw in yor script was that “these_items” needs to be coerced to name, which is a property. I’m trying to explain something that I’m not sure I fully understand, so doublecheck what I say if nobody corrects me.

Try selecting a pola file and running this in the Script Editor and watch the event log. What happens? Try again with a non-pola file.

tell application "Finder"
	
	set the_file to (get selection) as alias
	set named_file to name of the_file
	if named_file begins with "pola" then
		
		duplicate named_file to "love:users:david:desktop:pola" without replacing
	else
		duplicate named_file to "love:users:david:desktop:edit" without replacing
		
	end if
end tell

And try running your script with the tell blocks switched:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if these_items begins with "pola" then duplicate to "love:users:david:desktop:pola" without replacing
	end tell
	tell application "Finder"
		duplicate these_items to "love:users:david:desktop:edit" without replacing
	end tell
end adding folder items to

and without the if:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		 then duplicate to "love:users:david:desktop:pola" without replacing
	end tell
	tell application "Finder"
		duplicate these_items to "love:users:david:desktop:edit" without replacing
	end tell
end adding folder items to

I’m using a bit of a scattershot approach here because it’s faster if you to test these than it is for me to recreate your situation on my computer.

Edited for typos, as usual.

Good afternoon capitalj,
I tried your suggestions, and I still couldn’t get it to work. The second suggestion, just creates a duplicate in the “edit” folder, but will not seperate the “pola” files and save them into the “pola” folder.

I am expecting my O’Reilly beginners Apple Script in the mail today or tomorrow, perhaps something will be in there to set this up. I did not check out the link to the other post yet, I will see what I can learn from that as well.

I know this can work, it’s just a matter of time. Thanks for you help and suggestions- I will let you know how it works out.

Best,
David

I forgot to say earlier that you wouldn’t be making an alias, that is just a way to get the name of the file. You can read about it in the first link I posted.

You don’t say what happened in the event log (I use that information all the time.) What happens if you do this after selecting a pola file? Do you hear a beep?

tell application "Finder"
   
   set the_file to (get selection) as alias
   set named_file to name of the_file
   if named_file begins with "pola" then
beep

end tell
       

By second suggestion, do you mean this, with the tell blocks switched? I didn’t expect it to separate the pola files, I wanted verify that switching the tell blocks would still get the same results as with your original script.

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if these_items begins with "pola" then duplicate to "love:users:david:desktop:pola" without replacing
	end tell
	tell application "Finder"
		duplicate these_items to "love:users:david:desktop:edit" without replacing
	end tell
end adding folder items to

if so, how about this, which is closer to your original? (I don’t expect it to work, to be honest.)

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
           set these_items to these_items as alias
           set named_items to name of these_items
           if named_items begins with "pola" then
	       duplicate these_items to "love:users:david:desktop:pola" without replacing
	    else
		duplicate these_items to "love:users:david:desktop:edit" without replacing
           end if
	end tell
end adding folder items to

If I had the time, I’d create folders and files to recreate the situation on my computer. I know you should be able to make this work. Post lots of information about what you are doing. Somebody will recognize the problem.

Hi Jacques.

I’m trying to understand where I’m going wrong.

On seeing your post, I realize that even if my first post had worked, it wouldn’t have done exactly what was required - pola files to pola folder and all files to edit.

But why didn’t it work? I notice that yours says

duplicate these_items to alias "love:users:david:desktop:edit" without replacing

while mine leaves out “alias” - was I incorrect? Did I make any other mistakes? I tried a different way to set the file name because, in other scripts, it was the only thing I could make work. Was that the problem?

Here it is again, and thanks in advance for clearing this up.

j

on adding folder items to Files_to_Sort after receiving these_Files
	repeat with this_file in these_Files
		
		tell application "Finder"
			set the_file to this_file as alias
			set named_file to name of the_file
			
			duplicate named_file to "love:users:david:desktop:pola" without replacing
			
			if named_file begins with "pola" then
				duplicate named_file to "love:users:david:desktop:edit" without replacing
			end if
			
			
		end tell
	end repeat
end adding folder items to

Thank you both, Jacques and Capitalj for helping me out with this problem. The code that Jacques sent worked perfectly.

on adding folder items to this_folder after receiving these_items
tell application “Finder”
duplicate these_items to alias “love:users:david:desktop:edit” without replacing
repeat with this_file in these_items
if name of this_file starts with “pola” then duplicate this_file to alias “love:users:david:desktop:pola” without replacing
end repeat
end tell
end adding folder items to

I’m still confused on why an alias is needed for the folders since they are direct links from the finder, I have read the Apple Script the definitive guide by Matt Neuburg and he discusses alias along with coercion. Does the above code work because of coercion? Is there a another book that discusses this in begininers terms that anyone could recommend?

I appreciate all the time you both put in to help me out, now my workflow is complete.

Best,
David

capitalj,

In your code, where you set named_file to name of the_file, the named_file is a variable containing the text which would be the name of the file and not a reference to the file. In order to use the named_file variable to grab the file you want, you’d have to code it something like this:

tell application "Finder"
duplicate file named_file of folder "aFolderName" of folder "Desktop" of startup disk
end tell

(The above code assumes the rest of the code is in place for the info for named_file). You can reference a file this way or a couple of other ways. To revise your code a little to make it work, when your conditional statement is met then you would say:

tell application "Finder"
duplicate the_file to "love:users:david:desktop:pola" without replacing
end tell

the_file is an alias, a pointer/reference to the file. If the file is moved, the script will be able to find it.

testpilot: I believe that you may be getting confused with the alias thing thinking that making the alias in the script is making a desktop alias pointing to your file, however, it is just a way of making a reference to the file you want to work with within the script only.

PreTech

Thanks PreTech.

I just went back to the script I borrowed set name_file from and realized that I missed the bit further along that made it a reference - oops.

Next time I’ll know better (I hope.)

j