Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Batch convert names list into folder set?

Does anyone know how to batch convert lists of names in text files into sets of new empty folders?

iMac G4, Mac OS X (10.4.11)

Posted on Jun 7, 2010 11:15 PM

Reply
Question marked as Best reply

Posted on Jun 8, 2010 10:48 AM

I have an action for Leopard, but for Tiger you can use the AppleScript it is based on. The names in the plain text file(s) need to be separated by returns.

1) *Ask for Finder Items* -- get the text file(s)
2) *Combine Text Files* -- get the text
3) *Filter paragraphs* { Return paragraphs that are not empty }
4) *Run AppleScript* -- paste the following script:
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into an Automator 'Run AppleScript' action">
on run {input, parameters}
(*
make new sub folders in the specified folder from a list of names
names that contain colons (:) will create intermediate sub folders as required
input: a list of names (text items)
output: a list of Finder items (aliases) created
*)

set output to {} -- this will be a list of the created folders (for each name item)
set useExistingFolders to true -- use folders that already exist?

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 the input
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
set the end of output to targetFolder
end if
end repeat

return output
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
</pre>
3 replies
Question marked as Best reply

Jun 8, 2010 10:48 AM in response to GWR10

I have an action for Leopard, but for Tiger you can use the AppleScript it is based on. The names in the plain text file(s) need to be separated by returns.

1) *Ask for Finder Items* -- get the text file(s)
2) *Combine Text Files* -- get the text
3) *Filter paragraphs* { Return paragraphs that are not empty }
4) *Run AppleScript* -- paste the following script:
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into an Automator 'Run AppleScript' action">
on run {input, parameters}
(*
make new sub folders in the specified folder from a list of names
names that contain colons (:) will create intermediate sub folders as required
input: a list of names (text items)
output: a list of Finder items (aliases) created
*)

set output to {} -- this will be a list of the created folders (for each name item)
set useExistingFolders to true -- use folders that already exist?

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 the input
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
set the end of output to targetFolder
end if
end repeat

return output
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
</pre>

Jun 21, 2010 12:53 AM in response to red_menace

Thanks for your detailed reply - unfortunately I don't have the programming skills or AppleScript experience to get your information to work without further step-by-step instructions, and even that may be an unrealistic expectation!

Thanks also for letting me know that this is something Leopard can already do, since I'm planning a new Intel iMac with Snow Leopard fairly soon. (btw do you happen to know if SL also needs a special Action/AppleScript to do this?)

At this stage, I was really just exploring ways I might save some time while still using Tiger.

I was hoping I might find a Mac App with a simple operation window something like filename editor apps, eg:

1) Select source list/file for desired new folder names
2) Select destination location/folder for desired new folders
3) Create new folder set with names from selected source list/file

Thanks again for your contribution - G

Message was edited by: GWR10

Jun 21, 2010 7:41 PM in response to GWR10

The actions I used in my example are included with Automator - there are some examples and tutorials about Automator at http://www.macosxautomation.com/automator/

The first 2 actions in my example workflow ask for the file(s) containing the names and get the text - these can be replaced by something like the *Get Specified Text* action (or anything that returns text), although there isn't an included action that you can type in multiple lines of text at run time.

However you decide to get the names, these text items are then passed on to the *Run AppleScript* action which will create the folders (it will ask for the destination folder, if you don't specify one) - you can just copy/paste the posted script into the action, replacing the example text.

These actions will be the same if you wind up using my Make New Folders from Text Items action, which does the same thing as the *Run AppleScript* action (although it is a little less confusing).

Batch convert names list into folder set?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.