|
|
#1 |
|
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! |
|
|
|
|
|
#2 |
|
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:me esktop:folderX"set destinationFolder to alias "me-DT:Users:me esktop: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 replacingelse 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 |
|
|
|
|
|
#3 |
|
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 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. |
|
|
|
|
|
#4 |
|
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. |
|
|
|
|
|
#5 | ||||||||||||||||||||||||||||||||||||||||||
|
Prospect
Join Date: Nov 2014
Posts: 5
|
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
Hmm, its all Greek to me. Would you have an example script? Last edited by fightforthefutur; Yesterday at 11:46 AM. |
||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#6 |
|
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
Seems like putting a whole path in the script does not work. Last edited by fightforthefutur; Yesterday at 01:14 PM. |
|
|
|
|
|
#7 |
|
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 |
|
|
|
![]() |
| Tags |
| copy replace pdfs remove |
|
|