do i need to learn applescript for this?

hello all,

i have a repetitive task i need to perform on a folder full of items (around 400 files) and i was wondering if i need to learn applescript or if this can be done with automator (i haven’t managed to do it yet, obviously). i’ve looked at some folder/subfolder scripts on this site but didn’t find anything that suits the problem i have.

imagine a folder full of files which look like this :

project a.data.txt
project a.dates.txt
project a.psd
project a.client.xls
project b.psd
project c.psd
deadlines.data.txt
deadlines.dates.txt

etc…

now i need, for every group of files (i.e. “project a”, “deadlines” etc), to create a folder based on the common part of the filename (i.e. before the period), and then move the files into a subfolder within that folder which has a fixed name (such as “original files”), for example:

/project a/original files/project a.data.txt
/project a/original files/project a.dates.txt
/project a/original files/project a.psd
/deadlines/original files/deadlines.data.txt
/deadlines/original files/deadlines.dates.txt

i could do this by hand but with 400 files it’s a major pain. i tried creating an automator action but couldn’t get the variables to work.

so, do i need to reserve some time for an applescript crash course or can this be done with automator? or maybe a script that does this exists already ?

thanks a lot.

Hi scotty,

welcome to MacScripter

Try this


set baseFolder to choose folder with prompt "Choose base folder"
set POSIXbaseFolder to POSIX path of baseFolder
set fileList to list folder baseFolder without invisibles

set TID to text item delimiters
repeat with oneItem in fileList
	set text item delimiters to "."
	set subFolder to 1st text item of oneItem
	set destination to quoted form of (POSIXbaseFolder & subFolder & "/" & oneItem)
	do shell script "ditto " & quoted form of (POSIXbaseFolder & oneItem) & " " & destination
	do shell script "rm -r " & quoted form of (POSIXbaseFolder & oneItem)
end repeat
set text item delimiters to TID


whoa, i didn’t expect a fully-fledged script! and a working one, too :slight_smile: apparently this can’t be done with automator…

thanks a lot for your time.

if i might be so bold, the problem i had was moving the files into a subfolder which would have an identical name (e.g. “original files”) inside each folder created by the script, so that:

file1.txt
file1.info.txt
file2.txt

become:

/file1/original files/file1.txt
/file1/original files/file1.info.txt
/file2/original files/file2.txt

etc

i have some experience with php and objective-c but i’m afraid of trying to modify your script (i don’t know anything about POSIXbaseFolder and ditto). could you please show me the necessary modifications? your script is so close to what i’m looking for that learning applescript in one afternoon seems pointless…

whether you have the time to do this or not, thanks a lot!

Automator is just a GUI for AppleScript, but pure AppleScript is much more flexible.

Sorry, I did’t get the “original files” folder


set baseFolder to choose folder with prompt "Choose base folder"
set POSIXbaseFolder to POSIX path of baseFolder
set fileList to list folder baseFolder without invisibles

set {TID, text item delimiters} to {text item delimiters, "."}
repeat with oneItem in fileList
	set subFolder to 1st text item of oneItem
	set destination to quoted form of (POSIXbaseFolder & subFolder & "/original files/" & oneItem)
	do shell script "ditto " & quoted form of (POSIXbaseFolder & oneItem) & " " & destination
	do shell script "rm -r " & quoted form of (POSIXbaseFolder & oneItem)
end repeat
set text item delimiters to TID

Notes:
POSIXbaseFolder - shell commands expect a POSIX (slash separated) path, so you have coerce the HFS (colon separated) path to a POSIX path
ditto - is a command which copies directory hierarchies. It also creates missing intermediate directories automatically

stefank, you are a precious individual…

thanks a million. if there’s anything i can help you out with, don’t hesitate.

one last thing: some psd files are quite large. wouldn’t it be simpler to do:

mv " & quoted form of (POSIXbaseFolder & oneItem) & " " & destination

and remove the rm line?

good thought. Then you first have to create the intermediate dictionaries


set baseFolder to choose folder with prompt "Choose base folder"
set POSIXbaseFolder to POSIX path of baseFolder
set fileList to list folder baseFolder without invisibles

set {TID, text item delimiters} to {text item delimiters, "."}
repeat with oneItem in fileList
	set subFolder to 1st text item of oneItem
	set destinationFolder to POSIXbaseFolder & subFolder & "/original files/"
	do shell script "/bin/mkdir -p " & quoted form of destinationFolder & ";/bin/mv " & quoted form of (POSIXbaseFolder & oneItem) & " " & quoted form of (destinationFolder & oneItem)
end repeat
set text item delimiters to TID

again, thank you.