Dumb question on making an Applescript that creates folders

Hi.
The dumb question is: Where do I begin?
I want to make an Applescript that:

  1. askes for a path
  2. askes for input seperatet by carrige returns
  3. makes folders in given path with the name of each line given in step 2

So, anyone?

PS: I have read a couple of beginner guides and one on the library of applescripts, but still feel blank on how to attack this.

Hi,

try this


set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return
repeat with aLine in (get paragraphs of textReturned)
	if contents of aLine is not "" then
		tell application "Finder" to make new folder at destinationFolder with properties {name:aLine}
	end if
end repeat

Perfect!
Got any tips for a good place to start reading so I can do that myself?

There are a lot of helpful tutorials here on MacScripter in the UnScripted section
This is a starting point

Thanks a lot. I have something to do this weekend now :wink:

You’re welcome.

Happy scripting. It’s worth it.

I hope so :slight_smile:
I next move is to find out how I can get the script to skip the line if the folder exits.

Use the shell script equivalent, it skips existing folders automatically with the -p switch


set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return
repeat with aLine in (get paragraphs of textReturned)
	if contents of aLine is not "" then
		do shell script "/bin/mkdir -p " & quoted form of (POSIX path of destinationFolder & aLine)
	end if
end repeat

Trying to expand the script. Adding the posebility to fill the folders with files, but not getting it right :frowning:
I have have tried every file in a folder, cause I couldn’t manage to make a list of files choosen by the user. That was my first thought though. Asking the user to input multiple files and adding them all. Could only get it to work with one file.


set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return
set folderWithFiles to (choose folder with prompt "Where are the files at?")
repeat with aLine in (get paragraphs of textReturned)
	if contents of aLine is not "" then
		do shell script "/bin/mkdir -p " & quoted form of (POSIX path of destinationFolder & aLine)
		set folderNew to aLine & ":" as alias
		set dest to folderNew on destinationFolder -- as alias
		tell application "Finder"
			duplicate every file of folderWithFiles to dest
		end tell
	end if
end repeat

If you want to ask the files for each new folder, try this


set destinationFolder to choose folder
set {text returned:textReturned} to display dialog "enter folder names" default answer return & return & return
if textReturned is "" then return

repeat with aLine in (get paragraphs of textReturned)
	if contents of aLine is not "" then
		set newFolder to (destinationFolder as text) & aLine
		do shell script "/bin/mkdir -p " & quoted form of POSIX path of newFolder
		set theFiles to (choose file with prompt "Where are the files at?" with multiple selections allowed)
		tell application "Finder" to duplicate theFiles to folder newFolder
	end if
end repeat


Wow. That was fast :slight_smile:
Thanks a lot. Works like a charm. Wanted the same files in all folder so I moved the “choose files” out of the loop.
Coding is weird. Small changes make all the difference :wink: