Point script to File/Folder instead of "Choose Folder" prompt

Hi there,

I have tried several different path iterations to get the script below to point directly to a folder and text file (inside the same folder) on my desktop with no success. Every iteration errors out, except when I manually select the folder/file with the two “choose folder/file” prompts below.


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

Any help would be greatly appreciated.

Thanks!

The choose file and choose folder commands return aliases, and the following also uses aliases.

set destination to "Macintosh HD:Users:Robert:Desktop:Test Folder:" as alias
set textFile to "Macintosh HD:Users:Robert:Desktop:Test Folder:New Text File.txt" as alias

set folderNames to paragraphs of (read textFile)

repeat with oneName in folderNames
	if oneName > "" then
		tell application "Finder" to make new folder at destination with properties {name:oneName}
	end if
end repeat

The following differs in that it uses file/folder specifiers:

set destination to "Macintosh HD:Users:Robert:Desktop:Test Folder:"
set textFile to "Macintosh HD:Users:Robert:Desktop:Test Folder:New Text File.txt"

set folderNames to paragraphs of (read file textFile)

repeat with oneName in folderNames
	if oneName > "" then
		tell application "Finder" to make new folder at folder destination with properties {name:oneName}
	end if
end repeat

You can use the “path to” command to specify the desktop folder if you prefer:

set destination to (path to desktop as text) & "Test Folder:"
set textFile to (path to desktop as text) & "Test Folder:New Text File.txt"

I tested all of the above on my computer without issue.

Thanks for your help peavine,

I had tried similar iterations of these previously with no success, and with the “choose folder/file” prompts, the script works properly.

Your first script returned the Script Error:

Can’t make “Macintosh HD:Users:username:Desktop:Test_Folder:” into type alias.

Your second version, without the “as alias” returned the error:

Can’t make “Macintosh HD:Users:username:Desktop:Test Folder:New Text File.txt” into type file.

And the third version using “path to” returned the same error:

Can’t make “Macintosh HD:Users:username:Desktop:Test Folder:New Text File.txt” into type file.

Rich S. I tested those scripts before posting and they do work. A few thoughts.

You use both “Test Folder” and “Test_Folder” above, and I assume one contains a typo.

Run the following in Script Editor and see what happens. If this throws an error, then Test Folder doesn’t exist and needs to be created.

(path to desktop as text) & "Test Folder" as alias

The following script includes some additional error correction. Please note that the folder “Test Folder” and the file “Test File.txt” have to exist for this script to work.

try
	set destination to ((path to desktop as text) & "Test Folder:") as alias
	set textFile to ((path to desktop as text) & "Test Folder:Test File.txt") as alias
on error errorMessage
	display alert "The following error was reported" message errorMessage
	error number -128
end try

set folderNames to paragraphs of (read textFile)

repeat with oneName in folderNames
	if oneName > "" then
		try
			tell application "Finder" to make new folder at destination with properties {name:oneName}
		on error errorMessage
			display alert "The following error was reported" message errorMessage
			error number -128
		end try
	end if
end repeat

Thanks peavine,

Yep,

That was a typo. I have now changed my local file/folder names to those in your script as well in case I was making more typos.

I ran your snippet above and the folder: Test_Folder is indeed on my desktop, so the path is correct.

Aaaaand, then I ran your script with error reporting and it worked!!!

Thanks so much for your help!

Massively appreciated.

Rich S. You’re welcome. I’m glad that worked.