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

fightforthefutu

macrumors newbie
Original poster
Nov 12, 2014
7
0
Greetings! I am a sometime Applescript user, but not a programmer. I need to copy pdfs from one folder into their appropriate folders and replace older files of the same name. Our pdfs have numerical names to keep them in order, but with letters to designate book names. Thus folderX will have files such as

1234-A.pdf
1234-B.pdf
1234-C.pdf
5678-A.pdf
5678-D.pdf

and so on.

So basically, everything I drop in folderX, I would like applescript to copy files with "A" on to folder A, files with "B" on to folder B, and on and on. All of these are in "mainfolder".

The exception are files with "Q", which I want to copy to every folder in "mainfolder".

Here is what I came up with from other scripts, but I get the message "Can't make every folder of alias "me-DT:Users:me:Desktop:folderX" into type folder. -1700"

What's going wrong?

Code:
set sourceFolder to alias "me-DT:Users:me:Desktop:folderX"
set destinationFolder to alias "me-DT:Users:me:Desktop:mainfolder"
tell application "Finder"
	set theFiles to every file in sourceFolder as list
	repeat with aFile in theFiles
		set fileName to name of aFile as text
		try
			if fileName contains "A" then
				move aFile to destinationFolder:A with replacing
			else if fileName contains "B" then
				move aFile to destinationFolder:B with replacing
			else if fileName contains "C" then
				move aFile to destinationFolder:C with replacing
			else if fileName contains "D" then
				move aFile to destinationFolder:D with replacing
			else if fileName contains "Q" then
				move aFile to (every folder in destinationFolder) with replacing
			end if
			delay 1
		on error errorMessage number errorNumber
			display alert errorMessage & space & errorNumber message ¬
				"An error occured while trying to move the file " & aFile & "." & return & ¬
				"Check the file exists, or the delay time setting." as warning giving up after 60
		end try
	end repeat
end tell
 
Last edited by a moderator:

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
Hi,

Try something like this:

Code:
set sourceFolder to alias "Your:Path:Here:"
set destinationFolder to alias  "Your:Other:Path:Here:"

tell application "Finder"
	set theFiles to every file in sourceFolder as list
	repeat with aFile in theFiles
		set fileName to name of aFile as text
		try
			if fileName contains "A" then
				move aFile to folder ((destinationFolder & "A") as text) with replacing
			else if fileName contains "B" then
				move aFile to folder ((destinationFolder & "B") as text) with replacing
			else if fileName contains "C" then
				move aFile to folder ((destinationFolder & "C") as text) with replacing
			else if fileName contains "D" then
				move aFile to folder ((destinationFolder & "D") as text) with replacing
			else if fileName contains "Q" then
				move aFile to folder ((destinationFolder & "Q") as text) with replacing
			end if
			delay 1
		on error errorMessage number errorNumber
			display alert errorMessage & space & errorNumber message ¬
				"An error occured while trying to move the file " & aFile & "." & return & ¬
				"Check the file exists, or the delay time setting." as warning giving up after 60
		end try
	end repeat
end tell

You need to be adding the letters A, B, C or whatever to your variable "destinationFolder" as text. That's not what your script's doing!

If you want to be a show off (who doesn't) then you could have a look at shell scripting this. You could probably do it in a couple of lines. ;-)

Hope that helps!
 

fightforthefutu

macrumors newbie
Original poster
Nov 12, 2014
7
0
Thanks, superscape! The files are going where they are supposed to go, so that's some good progress!

As for shell scripting this to show off, well, for non-programmers like myself, a script with a million lines looks way cooler than a short script -- especially when it works! :)

The only snag is the "Q" part of the script. In my first attempt, "Q" files need to be copied into every folder (or rather, half of the folders). My attempt to alter the script to do so gave me the error message "Can't get every folder Your:path:Here: -1728"

Here's the action line that failed me:
Code:
	else if fileName contains "Q" then
	duplicate aFile to every folder in ((destinationFolder) as text) with replacing
	end if

I've had some success with the following script:

Code:
set sourceFolder to alias "mycomputer:Users:mycomputer:Desktop:folderX"
set main_folder to alias "mycomputer:Users:mycomputer:Desktop:mainfolder"

tell application "Finder"
	
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		
		duplicate (every file of sourceFolder whose name contains "Q") to each_folder with replacing
		
	end repeat
	try
		delete every file of sourceFolder
	end try	
end tell


But now that I think about it, "Q" files will ultimately need to go to half of the folders; the other half would all need copies of "X" files. To get "Q" files into all of its associated folders, would I have to do "else if" like this? Or some version of "Repeat" like above?

Code:
else if fileName contains "Q" then
duplicate aFile to folder ((destinationFolder & "A") as text) with replacing
else if fileName contains "Q" then
duplicate aFile to folder ((destinationFolder & "B") as text) with replacing
else if fileName contains "Q" then
duplicate aFile to folder ((destinationFolder & "C") as text) with replacing
end if

It doesn't work, of course, but any further suggestions would be great!

Thanks again!
 
Last edited:

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
You can try something like this :

Code:
tell application "Finder"
	...
	set theFolders to every folder of destinationFolder
	...
	else if fileName contains "Q" then
				repeat with j from 1 to the count of theFolders
				set aFolder to item j of theFolders
				if aFolder's name is not "Q" then
					duplicate aFile to aFolder with replacing
				else
					move aFile to aFolder with replacing
				end if
				end repeat
	end if
end tell

Note : Not tested.
 

fightforthefutu

macrumors newbie
Original poster
Nov 12, 2014
7
0
Thanks, I'm in the process of trying to adapt that to an older (4 hours older!) script.

Here's the script:

Code:
set sourceFolder to alias "mycomputer:Users:mycomputer:Desktop:folderX"
set destinationFolder to alias "mycomputer:Users:mycomputer:Desktop:mainfolder"

tell application "Finder"
	set theFiles to every file in sourceFolder as list
	set theFolders to every folder of destinationFolder
	repeat with aFile in theFiles
		set fileName to name of aFile as text
		try
			if fileName contains "A" then
				move aFile to folder ((destinationFolder & "A") as text) with replacing
			else if fileName contains "Q" then
				repeat with j from 1 to the count of theFolders
					set aFolder to item j of theFolders
					if aFolder's name is not "Q" then
						duplicate aFile to aFolder with replacing
					else
						move aFile to aFolder with replacing
					end if
				end repeat
			end if
			delay 2
		end try
	end repeat
end tell

And here's the message I got:

Can’t make document file "0391-Q.pdf" of folder "folderX" of folder "Desktop" of folder "mycomputer" of folder "Users" of startup disk into type Unicode text.

Right. So this script does not like my source folder for some reason.

My last attempt on the newer version did not pan out:

Code:
tell application "Finder"
	
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		
		duplicate (every file of sourceFolder whose name contains "Q") to (each_folder whose name contains "q**") with replacing
		duplicate (every file of sourceFolder whose name contains "X") to (each_folder whose name contains "x**") with replacing
		
	end repeat

Do wildcards like ** work in applescript?
 
Last edited:

fightforthefutu

macrumors newbie
Original poster
Nov 12, 2014
7
0
Final applescript for copying/replacing files into multiple folders

Thanks to help from here and other sources, I have a working script that replaces a program that's being retired by our MIS Dept. Here is the whole script for the sake of posterity. The first part truncates files to a given size. The second part moves the files to the appropriate folders.

Code:
set sourceFolder to alias "mycomputer:Users:mycomputer:Desktop:folderX"
set main_folder to alias "mycomputer:Users:mycomputer:Desktop:mainfolder"

tell application "Finder"
	set all_files to every file in sourceFolder
	repeat with a_file in all_files
		set f_name to a_file's name
		set f_ext to a_file's name extension
		set p_off to offset of f_ext in f_name
		if p_off is greater than 11 then
			set f_name to (characters 1 thru 8 of f_name & "." & f_ext) as string
			my CheckName(f_name, sourceFolder, f_ext, a_file)
		end if
	end repeat
end tell

on CheckName(fn, fld, ex, fl)
	set next_let to 97
	tell application "Finder"
		set all_names to the name of every file in folder fld
		if all_names contains fn then
			repeat until all_names does not contain fn
				set fn to (characters 1 thru 8 of fn & (ASCII character next_let) & "." & ex) as string
				set next_let to next_let + 1
			end repeat
		end if
		set name of fl to fn
	end tell
end CheckName

tell application "Finder"
	set theFiles to every file in sourceFolder as list
	repeat with aFile in theFiles
		set fileName to name of aFile as text
		try
			if fileName contains "aAK" then
				duplicate aFile to folder ((destinationFolder & "aAK:book") as text) with replacing
				move aFile to folder ((destinationFolder & "aAK:supplement") as text) with replacing
			else if fileName contains "aAL" then
				duplicate aFile to folder ((destinationFolder & "aAL:book") as text) with replacing
				move aFile to folder ((destinationFolder & "aAL:supplement") as text) with replacing
				
				
			else if fileName contains "qAA" then
				duplicate aFile to folder ((destinationFolder & "aAL:book") as text) with replacing
				duplicate aFile to folder ((destinationFolder & "aAL:supplement") as text) with replacing
				
				
			else if fileName contains "mAL" then
				duplicate aFile to folder ((destinationFolder & "mAL:book") as text) with replacing
				move aFile to folder ((destinationFolder & "mAL:supplement") as text) with replacing
				
				
			else if fileName contains "xAA" then
				duplicate aFile to folder ((destinationFolder & "mAL:book") as text) with replacing
				duplicate aFile to folder ((destinationFolder & "mAL:supplement") as text) with replacing
				delete aFile
				
			end if
			delay 2
			
		end try
	end repeat
end tell
 

fightforthefutu

macrumors newbie
Original poster
Nov 12, 2014
7
0
Greetings,

After a long period of success with this script, I now have to adjust it so that only the last six characters before the period and suffix are removed. So file 3-AA-00-01.doc should be truncated to 3-AA.doc and 3-AAB-00-09.doc should become 3-AAB.doc, and so on.

Fortunately, I found a working solution to the problem. It doesn't truncate randomly, but to specific name lengths. Still, for my own purposes, its just fine.
Code:
tell application "Finder"
   set all_files to every file in folder sourceFolder
   repeat with a_file in all_files
     set f_name to a_file's name
     set f_ext to a_file's name extension
     set p_off to offset of f_ext in f_name
     if p_off is greater than 16 then
       set f_name to (characters 1 thru 11 of f_name & "." & f_ext) as string
       my CheckName(f_name, sourceFolder, f_ext, a_file)
     else if p_off is greater than 13 then
       set f_name to (characters 1 thru 8 of f_name & "." & f_ext) as string
       my CheckName(f_name, sourceFolder, f_ext, a_file)
     end if
   end repeat
end tell
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.