Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
Thread Tools Rate Thread Display Modes
Unread 11-12-2014, 10:18 AM   #1
fightforthefutur
Prospect
 
Join Date: Nov 2014
Posts: 5
Copying files to multiple folders using Applescript

Greetings,

I have very little experience in Applescripting but the few scripts I use are very helpful. My new task is to get a script to replace our firm's old and infirmed PlanetWatch program, which automatically copies files dropped into one folder and distributes them to many other folders. I don't need the automatic part, but an applescript can do the task of distribution.

The key is, our files are pdfs whose numerical names keep them in order as books. The actual folder names are five characters into the file name. So, currently we drop into folder "X":

1234-A.pdf
1234-B.pdf
5678-A.pdf
5678-B.pdf

and then PlanetWatch send them on to folders "A" or "B".

So, 1:

My applescript requires an "if/then" line telling all files in folder "X" whose fifth character is A to send those files to folder "A". And, all files whose fifth character is "B" to send those files to folder "B".

The clincher is folder "Q". I need a line that says, if the fifth character is Q, then send those files to all folders: that is, folders "A" and "B" and "Q".

That way, Q is archived separately, and merged with A and B as well.

And 2:

Is there a line to delete the files in folder "X" after everything is copied over to A, B, and Q?

And finally:

Does this simply copy over similarly named files, or will it stop applescript when it encounters a similarly named file? I would like the new 1234-A.pdf to replace an older 1234-A.pdf.

Thanks for your thoughts on the matter!
fightforthefutur is offline   Reply With Quote
Unread 11-12-2014, 01:21 PM   #2
fightforthefutur
Prospect
 
Join Date: Nov 2014
Posts: 5
Concerning the above post, here is a code I attempted to work with:

set sourceFolder to alias "me-DT:Users:meesktop:folderX"
set destinationFolder to alias "me-DT:Users:meesktop: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 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
fightforthefutur is offline   Reply With Quote
Unread Yesterday, 10:56 AM   #3
fightforthefutur
Prospect
 
Join Date: Nov 2014
Posts: 5
Further to this topic, I have a working script for the main part of this process:

Code:
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
I still haven't figure out how to get "Q" files into folders "A" and "B".

What I'd also like is some syntax to copy files into subfolders of "A" and "B". The syntax ((destinationFolder & "A") moves "A" files into folder "A" but how to copy "A" files into two subfolders of folder "A", such as folder "a1" and folder "a2"?

Thanks for any thoughts.
fightforthefutur is offline   Reply With Quote
Unread Yesterday, 11:25 AM   #4
dfass
Prospect
 
Join Date: Aug 2012
Posts: 20
I'm not very AppleScript savy...
but wouldn't something like:
else if filename contains "Q" then
copy aFile to folder ((destinationFolder & "A") as text) with replacing
move aFile to folder ((destinationFolder & "B") as text) with replacing

work??

I mean instead of moving it, copy it then move it
or, in the previous if's, copy it, then delete it...ok, moving it works too

As for SUBFOLDERS, I would assume it's just a matter of converting the path to something like a POSIX path and
appending the subdirectories

Last edited by dfass; Yesterday at 11:31 AM.
dfass is offline   Reply With Quote
Unread Yesterday, 11:41 AM   #5
fightforthefutur
Prospect
 
Join Date: Nov 2014
Posts: 5
Quote:
Originally Posted by dfass
I'm not very AppleScript savy...
but wouldn't something like:
else if filename contains "Q" then
copy aFile to folder ((destinationFolder & "A") as text) with replacing
move aFile to folder ((destinationFolder & "B") as text) with replacing

work??

Unfortunately not. It successfully copies over to destinationFolder & "A" but not to "B" or any other folder in the list:

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
Quote:
As for SUBFOLDERS, I would assume it's just a matter of converting the path to something like a POSIX path and
appending the subdirectories

Hmm, its all Greek to me. Would you have an example script?

Last edited by fightforthefutur; Yesterday at 11:46 AM.
fightforthefutur is offline   Reply With Quote
Unread Yesterday, 01:12 PM   #6
fightforthefutur
Prospect
 
Join Date: Nov 2014
Posts: 5
Regardling subfolders, perhaps its better to have two entirely different paths instead of destinationFolder:

Code:
if fileName contains "A" then
duplicate aFile to folder (("mycomputer-DT"Users:mycomputer:desktop:folder1"  & "A") as text) with replacing
else if fileName contains "A" then
move aFile to folder (("mycomputer-DT"Users:mycomputer:desktop:folder2" & "A") as text) with replacing
But maybe not, as my attempt did not bear anything more than a message like "Can't set folder mycomputer-DT"Users:mycomputer:desktop:folder1A to folder mycomputer-DT"Users:mycomputer:desktop:folder1A"

Seems like putting a whole path in the script does not work.

Last edited by fightforthefutur; Yesterday at 01:14 PM.
fightforthefutur is offline   Reply With Quote
Unread Yesterday, 05:37 PM   #7
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,204
Don't rely on fileName contains ... . All of your filenames contain "D" (in ".pdf"). If you want to dispatch on the sixth character of the filename, get the sixth character.

Code:
set sourceFolder to alias "me-DT:Users:meesktop:folderX"
set destinationFolder to alias "me-DT:Users:meesktop:mainfolder"
tell application "Finder"
	set destinationSubfolders to (get every folder of destinationFolder as list)
	set theFiles to every file in sourceFolder as list
	repeat with aFile in theFiles
		try
			set selectorCode to text 6 of (name of aFile as text)
			if selectorCode is "Q" then
				repeat with destSubfolder in destinationSubfolders
					duplicate aFile to destSubfolder with replacing
				end repeat
				delete aFile
			else
				move aFile to folder selectorCode of destinationFolder with replacing
			end if
		on error errorMessage number errorNumber
			display alert errorMessage & space & errorNumber message ¬
				"An error occured while trying to move the file " & aFile & "." as warning giving up after 60
		end try
	end repeat
end tell
ganbustein is offline   Reply With Quote
Reply

Tags
copy replace pdfs remove


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:28 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.