or Connect
AppleInsider › Forums › General › Genius Bar › Automator Help (Creating a folder structure)
New Posts  All Forums:Forum Nav:

Automator Help (Creating a folder structure)

post #1 of 16
Thread Starter 
Once a year, I need to create a folder with a specific number for an internal corporate file structure on OS X (based on product numbering that goes back several years and I have no control over - grrr). Inside a new master folder, I need to create hundreds of nested numbered folders, each folder inheriting a new linear number (based on 1's or 10's usually)but based on its parent's number. I used to create these folders manually (yawn). Time to use Automator for this.

Any ideas on the logic involved in making them? I have the general pieces complete (dialogs that explain the tool and how it works,etc). I don't see any "pre-fab" way to build the "meaty" logic for the incremental folder building, so I will need to hand-code the actual AppleScript part I assume.

Examples:

Phase 1 = "Shallow" skeleton folders with increments of 10 in the folder name. Example:

1) Create "master" root folder named APC0015000

2) Inside the "master" folder, create 10 folders, each 1 level deep with the same name/number as the master folder +10 (i.e.; APC0015010, APC0015020, APC0015030...APC0015090, etc)


Phase 2 = "Deeper" folders with granualar increments of +1 instead of 10 this time. Example:

1) Inside the APC0015010 folder create 9 folders in increments of 1 like this APC0015011, APC0015012, APC0015013...APC0015019, etc)

2) Inside the APC0015020 folder create 9 folders in increments of 1 like this APC0015021, APC0015022, APC0015023...APC0015029 etc)

At the end of this project, I should have 1 master folder with 10 sub-folders (0-9), and each sub-folder contains 10 more folders (ranging from 0 to 9). Thus the folder heairachy looks like this:

APC0015000/
APC0015010/APC0015011/APC0015012...
APC0015020/APC0015021/APC0015022...

...

APC0015090/APC0015091/APC0015092...


I dont need to fill the 100's postion yet, just the 10's and 1's thus far... I might need to modify the code sometime to do a wider/deeper folder structure.


Thanks
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #2 of 16
It's easy with AppleScript.

I have to run some errands but I will post the basics when I get back.

You could then wrap the AppleScript in an Automator action if you want, but this being a once-a-year deal, probably not needed.
--Johnny
Reply
--Johnny
Reply
post #3 of 16
Thread Starter 
Quote:
Originally posted by lundy
It's easy with AppleScript.

I have to run some errands but I will post the basics when I get back.

You could then wrap the AppleScript in an Automator action if you want, but this being a once-a-year deal, probably not needed.

Your help is greatly appreciated my friend. I'm off to lunch and I shall return as well! Thanks!
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #4 of 16
Quote:
Originally posted by dstranathan
Your help is greatly appreciated my friend. I'm off to lunch and I shall return as well! Thanks!

OK I got it working - I have to ask a couple of questions before I post it:

- do you want NINE folders inside the Master Root Folder, or TEN?

- same with the second level: nine, or ten?

It wasn't clear from your post.

EDIT: and that leads to the second issue - for example, inside the "APC0015000" master folder, should the first folder be the same name, i.e. APC0015000", or should the first one be "APC0015010"?

Johnny
--Johnny
Reply
--Johnny
Reply
post #5 of 16
OK I'm going to go ahead and post the script to see if it has the number of folders that you need.

Code:


set root to choose folder with prompt "Select the directory where you want the folder structure built."
tell application "Finder"
set rootName to "APC00150"
set mainfolder to make new folder at root with properties {name:rootName & "00"}
repeat with i from 10 to 100 by 10
set child1 to make new folder at mainfolder with properties {name:rootName & (i as string)}
repeat with j from 1 to 9
make new folder at child1 with properties {name:rootName & ((i + j) as string)}
end repeat
end repeat
end tell



You'll see that the first level has folders APC0015010 ..... APC150100, which I doubt is what you want.
--Johnny
Reply
--Johnny
Reply
post #6 of 16
Thread Starter 
Quote:
Originally posted by lundy
OK I got it working - I have to ask a couple of questions before I post it:

- do you want NINE folders inside the Master Root Folder, or TEN?

- same with the second level: nine, or ten?

It wasn't clear from your post.

EDIT: and that leads to the second issue - for example, inside the "APC0015000" master folder, should the first folder be the same name, i.e. APC0015000", or should the first one be "APC0015010"?

Johnny

10 will work (0-9)

Thanks!
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #7 of 16
Thread Starter 
I will play with your script code now! Thanks!
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #8 of 16
Quote:
Originally posted by dstranathan
I will play with your script code now! Thanks!

Great! As you will see, there are two contradictory results:

1. If the suffixes start at 10, and there are 10 of them, then the last one will be "APC0015100", using up the hundreds place.

2. If they start at 00, then the first inner folder has the same name as the Master Folder. This may or may not be what you want.

Just let me know how you want it and we can tweak it to match the data you have.
--Johnny
Reply
--Johnny
Reply
post #9 of 16
Thread Starter 
Quote:
Originally posted by lundy
Great! As you will see, there are two contradictory results:

1. If the suffixes start at 10, and there are 10 of them, then the last one will be "APC0015100", using up the hundreds place.

2. If they start at 00, then the first inner folder has the same name as the Master Folder. This may or may not be what you want.

Just let me know how you want it and we can tweak it to match the data you have.

I see what you mean. No, I dont want there to be a sub folder with the same name/number

The 100's folder should contain the 10s folders with no dupes
The 10's folders should contain the 1s folders with no dupes
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #10 of 16
Quote:
Originally posted by dstranathan
I see what you mean. No, I dont want there to be a sub folder with the same name/number

The 100's folder should contain the 10s folders with no dupes
The 10's folders should contain the 1s folders with no dupes

So how exactly do you want them numbered?

- The master folder is named APC0015000.
- The master folder contains ten subfolders named APC0015010 through APC00150100?? Or does it just have 9 subfolders named APC0015010 through APC0015090???
- Each of these subfolders contains ten subfolders named APC0015011 through APC0015020??? I don't think that is what you want, as it duplicates. If you want just 9 subfolders, that is in the code already.

The problem is that you can't put ten subfolders in there without either the first or last subfolder name being a duplicate of its enclosing folder.
--Johnny
Reply
--Johnny
Reply
post #11 of 16
Thread Starter 
Quote:
Originally posted by lundy
So how exactly do you want them numbered?

- The master folder is named APC0015000.
- The master folder contains ten subfolders named APC0015010 through APC00150100?? Or does it just have 9 subfolders named APC0015010 through APC0015090???
- Each of these subfolders contains ten subfolders named APC0015011 through APC0015020??? I don't think that is what you want, as it duplicates. If you want just 9 subfolders, that is in the code already.

The problem is that you can't put ten subfolders in there without either the first or last subfolder name being a duplicate of its enclosing folder.

To clarify:

- The master folder is named APC0015000. Yes, this is correct.

- The master folder contains ten subfolders named APC0015010 through APC0015090 (then inside the ACP001590 folder for example, there are the 1's range of sub folders numbered like ACP001591 through ACP001599)
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #12 of 16
Quote:
Originally posted by dstranathan
To clarify:
- The master folder contains ten subfolders named APC0015010 through APC0015090


that's only nine subfolders, not ten. That is where I am confused.
Quote:
(then inside the ACP001590 folder for example, there are the 1's range of sub folders numbered like ACP001591 through ACP001599)


I'll redo it so there are only nine subfolders in each level:

Code:


set root to choose folder with prompt "Select the directory where you want the folder structure built."
tell application "Finder"
set rootName to "APC00150"
set mainfolder to make new folder at root with properties {name:rootName & "00"}
repeat with i from 10 to 90 by 10
set child1 to make new folder at mainfolder with properties {name:rootName & (i as string)}
repeat with j from 1 to 9
make new folder at child1 with properties {name:rootName & ((i + j) as string)}
end repeat
end repeat
end tell



That's it. If there do in fact need to be ten subfolders, we will have to have a different numbering scheme.
--Johnny
Reply
--Johnny
Reply
post #13 of 16
Thread Starter 
I played with it for a while, nice job man!

Ok, here's an idea to extend the current functions:

How can I make it interactive to prompt the user for the folowing 3 variables:

1) What is the starting (top folder) value?

2) What range of increments for the first layer of subfolders? (i.e. 100's, 10's, etc)

3) What range of increments for the second layer of subfolders? (i.e. 1's, etc)

Then have it report a "All Done!" statement to verify, with an OK button at the end.
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #14 of 16
Quote:
Originally posted by dstranathan
I played with it for a while, nice job man!

Ok, here's an idea to extend the current functions:

How can I make it interactive to prompt the user for the folowing 3 variables:

1) What is the starting (top folder) value?
[/bquote]
This is simple - but I added a routine to make sure that the entered value does not contain a colon, as that would blow up the script. Also if the user clicks Cancel, that is an error and it just quits.
[bquote]
2) What range of increments for the first layer of subfolders? (i.e. 100's, 10's, etc)


I put in the dialog code - but there is only rudimentary error checking. If somebody enters crazy stuff like start at 1 and increment by 10000 then who knows what will happen. Also it does not check to make sure the entered values are numbers, so entering anything except numbers will cause the script to quit.
Quote:
3) What range of increments for the second layer of subfolders? (i.e. 1's, etc)

Then have it report a "All Done!" statement to verify, with an OK button at the end.

Here is the script. I tested it with 10 by 10 and 1 by 1.

Code:


try
set root to choose folder with prompt "Select the directory where you want the folder structure built."
on error
return
end try

try
set level1Base to text returned of (display dialog "Enter Level 1 Start Number" default answer 10)
on error
return
end try

try
set level1Incr to text returned of (display dialog "Enter Level 1 Increment" default answer 10)
on error
return
end try

try
set level2Base to text returned of (display dialog "Enter Level 2 Start Number" default answer 1)
on error
return
end try

try
set level2Incr to text returned of (display dialog "Enter Level 2 Increment" default answer 1)
on error
return
end try

tell application "Finder"
try
set rootName to text returned of (display dialog "Enter Name of Root Folder" default answer "")
on error
return
end try
if ":" is in rootName then
set AppleScript's text item delimiters to ":"
set wordList to words of (rootName as text)
set AppleScript's text item delimiters to ""
set rootName to (wordList as text)
end if
set mainfolder to make new folder at root with properties {name:rootName}
repeat with i from level1Base to (10 * level1Base) by level1Incr
set child1 to make new folder at mainfolder with properties {name:rootName & (i as string)}
repeat with j from level2Base to (9 * level2Base) by level2Incr
make new folder at child1 with properties {name:rootName & ((i + j) as string)}
end repeat
end repeat
end tell
display dialog "All done."
--Johnny
Reply
--Johnny
Reply
post #15 of 16
Thread Starter 
Can the root name be set first (display dialog "Enter Name of Root Folder") instead of last so that users are prompted for the top folder name first before being prompted for subfolder info?
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
A friend will help you move, but a REAL FRIEND will help you move a body.
Reply
post #16 of 16
Quote:
Originally posted by dstranathan
Can the root name be set first (display dialog "Enter Name of Root Folder") instead of last so that users are prompted for the top folder name first before being prompted for subfolder info?

Sure. Just move the whole try .. on error .. end try block to the top, as well as the colon-remover lines.

Actually, if this is going to be used by a lot of people, the repeated dialogs are kind of clumsy and have the drawback of having to enter all the data again if someone makes a mistake on the last one. That is the only way to do it in pure AppleScript however.

We would normally move to AppleScript Studio at this point so that we could have a dialog box with several entry areas, and let the user set them all at the same time. That involves moving to Xcode, and a bit more complexity in hooking up the interface with Interface Builder. For something that is used once every year, it isn't worth the extra debugging.

One advantage is that in Xcode, checking the input is a bit more straightforward, as it has formatters that can be used to filter the characters entered.
--Johnny
Reply
--Johnny
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Genius Bar
AppleInsider › Forums › General › Genius Bar › Automator Help (Creating a folder structure)