Creating new folders named from a text file

I need to create folders which will contain later some files.
I am still not to good at Applescript and I really need help.

I found a script here which should be doing exactly what I want:


set x to 1
display dialog " --Make Lotsa' Folders-- 
This AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "TextEdit"
	activate
	open (choose file with prompt "Select the text file you wish to use")
end tell

repeat
	tell application "TextEdit"
		activate
		if (paragraph x of window 1 exists) then
			select paragraph x of window 1
			copy selection to folderName
		else
			beep
			display dialog "Your folders are done." buttons {"OK"}
			return
		end if
	end tell
	tell application "Finder"
		activate
		make folder in destination with properties {name:folderName}
		--select folder "untitled folder" in the destination 
		--set the name of the selection to folderName 
	end tell
	set x to x + 1
end repeat
end


I am using currently Tiger and Script Editor 2.1.2

After choosing the folder and the text file I always receive this error:

“TextEdit got an error: Can’t make paragraph 1 of window 1 into type reference.”

And stops.

The tect file has no spaces as I replaced the spaces with “_” this because the text file is a Name date and time which I need in the folders.
Example: nicauri_26_09_2009_20_07_46
where Nicauri is the name, then the first set of numbers if the date and the second the time.

What is wrong?

Thanks a lot

AppleScript: 2.1.2
Browser: Safari 531.22.7
Operating System: Mac OS X (10.4)

Hello danwan!

There has evidently been some changes to TextEdit, this works for me, and I’m on SL. I think it should work for you as well.



set x to 1

display dialog " --Make Lotsa' Folders-- \nThis AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "TextEdit"
	activate
	open (choose file with prompt "Select the text file you wish to use")
end tell

repeat
	tell application "TextEdit"
		activate
		if (paragraph x of text of document 1 exists) then
			set folderName to paragraph x of text of document 1
			-- select paragraph x of text of document 
		else
			beep
			display dialog "Your folders are done." buttons {"OK"}
			return
		end if
	end tell
	tell application "Finder"
		activate
		make folder in destination with properties {name:folderName}
		--select folder "untitled folder" in the destination 
		--set the name of the selection to folderName 
	end tell
	set x to x + 1
end repeat
end

Hi,

if the text file is a plain text file, TextEdit is not needed


display dialog " --Make Lotsa' Folders-- 
This AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
set textFile to (choose file with prompt "Select the text file you wish to use")
set folderNames to paragraphs of (read textFile)
repeat with oneName in folderNames
	tell application "Finder" to make new folder at destination with properties {name:oneName}
end repeat
display dialog "Your folders are done." buttons {"OK"}


Actually this is exactly the script I used from the same source perhaps it works on Snow but not in Tiger.

I have found another script which works googling around It works also in Tiger, I don’t know about snow as for now I am not using it. Here it is for however is interested:

on run -- make new folders from names in a text file
	(*
	makes new folders at the destinationFolder using names from the inputFile text file
	if useExistingFolders is set to false, a number suffix is added to the name if the folder exists
	the colon (:) is illegal for Finder names, but can be used to specify subfolders
	*)
	set useExistingFolders to false -- use folders that already exist?
	
	set inputFile to missing value -- set the source text file path here
	try
		set inputFile to inputFile as alias
	on error
		set inputFile to (choose file with prompt "Choose a text file containing folder names:")
	end try
	
	set destinationFolder to missing value -- set the destination folder path here
	try
		set destinationFolder to destinationFolder as alias
	on error
		set destinationFolder to (choose folder with prompt "Choose a destination folder for the new sub folders:")
	end try
	
	repeat with anItem in paragraphs of (read inputFile)
		set anItem to anItem as text
		if anItem is not "" then -- skip blank items
			set targetFolder to destinationFolder
			set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set {nameList, AppleScript's text item delimiters} to {text items of anItem, tempTID}
			
			repeat with theName in nameList -- deal with sub folders
				if not useExistingFolders then set theName to (getUniqueName for theName from targetFolder) -- avoid duplicate names
				try
					tell application "Finder"
						make new folder at targetFolder with properties {name:theName}
						set targetFolder to (the result as alias)
					end tell
				on error number -48 -- folder already exists
					set targetFolder to ((targetFolder as text) & theName) as alias
				end try
			end repeat
		end if
	end repeat
	
end run


to getUniqueName for someName from someFolder
	(*
	check if someName exists in someFolder, creating a new unique name if needed
		parameters -		someName [text]: a name.extension to check for
					someFolder [mixed]: a folder to check
		returns [text]:		a unique name
	*)
	set {counter, divider} to {"00", "_"}
	
	set here to -(offset of "." in ((reverse of text items of someName) as text)) - 1
	set theName to text 1 thru here of someName
	if here is -1 then -- no extension
		set theExtension to ""
	else
		set theExtension to text (here + 1) thru -1 of someName
	end if
	
	set newName to theName & theExtension
	tell application "System Events" to tell (get name of items of folder (someFolder as text))
		repeat while it contains newName
			set counter to text -2 thru -1 of ((100 + counter + 1) as text) -- leading zero
			set newName to theName & divider & counter & theExtension
		end repeat
	end tell
	
	return newName
end getUniqueName

Thanks

AppleScript: 2.1.2
Browser: Safari 531.22.7
Operating System: Mac OS X (10.4)

Thank Stephan …

This worked perfectly for me on 10.5.8, thanks Stefan! Created 63 folders based on names listed in a plain text file, just as I’d hoped. I also got the paragraph 1 error with the original script posted by danwan.