It’s a long time since i’ve dabbled with scripting and need some major advice.
Is it possible to use an Applescript to move a file to a location based on the filename?
Here is the explaination;
I have a filesystem and its structure is as below
Clients/ClientA/jobnumber/SCANS
Clients/ClientB/jobnumber/SCANS
Clients/ClientC/jobnumber/SCANS
Can I name a file CA_1234_S001
the first part of the filename “CA” is a code that defines ClientA
the second part of the file “1234” is the jobnumber
the “S” in the file will tell the script that the file is a scan and will need to go into the subfolder SCAN.
“001” in my example is just an image number and can be ignored by the script
As an aboslute beginner what should be my best way to work this idea?
Would I need to create some sort of look-up table to reference the client codes?
Yes. It is possible. I can’t find the post what was about it, but with help of the Macscripters I created the following script (Folder action).
It moves a pdf-file from the PDF-out folder to a corresponding subfolder of the projectfolder. It checks the first 6 characters (project number) and finds the right folder on the same volume.
E.g. the pdf file named
043104-1 Macscripter.pdf
will be moved to
043104 PDF proofings
of project folder
043104 Macscripter
on adding folder items to Sfolder after receiving addedItems
with timeout of 30 seconds
try
tell application "Finder"
--move (every file of (Sfolder) whose name ends with ".ps") to trash with replacing
set Tvolume to "PS 6.1"
set Tfolderlist to list folder (Tvolume as alias) without invisibles
repeat until (count of files of folder Sfolder) is 0
set remainingFiles to every file of Sfolder
set Sitem to item 1 of remainingFiles as alias
set Sname to name of Sitem
set SprojectNumber to (text 1 thru 6 of (get name of Sitem)) as string
repeat with n from 1 to (count of Tfolderlist)
set Titem to item n of Tfolderlist
set TprojectNumber to (text 1 thru 6 of Titem) as string
set Tfolder to (Tvolume & ":" & Titem & ":" & TprojectNumber & " PDF Proeven")
if TprojectNumber = SprojectNumber then
set tlist to list folder (Tfolder as alias) without invisibles
set numFolders to (count of folders of folder (Tfolder as alias))
set folderNum to (numFolders + 1)
if tlist contains Sname then
set newTFolder to (make new folder of (Tfolder as alias) with properties {name:"PDF Proeven " & folderNum}) as alias
move (every file of Sfolder whose name begins with SprojectNumber) to folder newTFolder
else
move (every file of Sfolder whose name begins with SprojectNumber) to folder Tfolder
exit repeat
end if
end if
end repeat
end repeat
end tell
on error the_error
display dialog the_error buttons {"OK"} default button 1 with icon 0 giving up after 10
end try
end timeout
end adding folder items to