My 1st Script - Folder w subfolders named, labeled and dated.

I finally did it – created my first script that is actually handy. Well, handy to me anyway.

I do confess that I pretty much copied other’s scripts and spent much time in trail and error.

I created this script for my design jobs. For each job, I assign a job number in Excel, create a folder with that name and then various subfolders. This script begins by displaying a dialog box for inserting the job name and then asks where you want to save the folder and subfolders.

Since my subfolder names can get long, I set the script to label each folder to a different color so I could tell which was which w/in “save” dialog boxes. I also set a shell script to add the current date to the end of each folder except one, Final Files. Once a job is done, I will set the date on this folder.

Eventually, I hope to set spotlight comments for each folder with the name of the folder. I would also love to have the root folder’s and Final Files folder’s names added to iCal. It would be great if I could get those dates into iCal as one event starting with the root folder date and ending with Final Files date). I hope to be able to glance at iCal and see which months are the busiest and when to expect a yearly job to becoming my way. Someday… :slight_smile:

If anyone sees tweaks I should make to this script or have any advice, send them on. I am still learning.

display dialog "Job Name:" default answer ""
set j_name to text returned of result

set myPath to (choose folder with prompt "Choose Folder") as text

tell application "Finder"
	set main_folder to (make new folder at folder myPath with properties {name:j_name & space & (do shell script "date -n +%m%d%y")})
	set root_folder2 to make new folder at main_folder with properties {name:j_name & space & "Design" & space & (do shell script "date -n +%m%d%y")}
	set the label index of root_folder2 to 5
	set root_folder3 to make new folder at main_folder with properties {name:j_name & space & "PDFs" & space & (do shell script "date -n +%m%d%y")}
	set the label index of root_folder3 to 1
	set root_folder4 to make new folder at main_folder with properties {name:j_name & space & "Art" & space & (do shell script "date -n +%m%d%y")}
	set the label index of root_folder4 to 4
	set root_folder5 to make new folder at main_folder with properties {name:j_name & space & "Text" & space & (do shell script "date -n +%m%d%y")}
	set the label index of root_folder5 to 3
	set root_folder6 to make new folder at main_folder with properties {name:j_name & space & "Final Files"}
	set the label index of root_folder6 to 7
	set root_folder7 to make new folder at main_folder with properties {name:j_name & space & "Quote Invoice" & space & (do shell script "date -n +%m%d%y")}
	set the label index of root_folder7 to 6
		
	set root_folder to ({name:"temp_name"})
end tell
end

cindy in indy

Congratulations on your first useful script! It runs well. Once you get more practice try to identify parts of your code that are repeats of other lines. The making of folders repeats throughout the script. That should indicate to you that you have a good opportunity of using a repeat loop and less lines of code. So with that in mind I did the following to streamline your code.

  1. you only need to calculate the date variable once and use that result every time instead of recalculating the same thing a bunch of times. This makes your script run faster.

  2. I setup a list of records at the top of the script. Each list contains a record of the folder name and its label index. Then in the code I can repeat over that list and create all of the folders and label them with just use 2 lines of code. By doing this you accomplish 2 things, first you shorten the code which is always a good thing and second you have that list right at the top of your script. This allows you to easily add or remove folders without having to touch the rest of your script. It gives you flexibility and ease of managing your code. And it makes your code more reusable.

Obviously you’re just starting so my comments aren’t meant as a criticism. My comments are to give you things to think about so you can expand your skills going forward. Good luck! :slight_smile:

set folderParameters to {{theName:"Design", labelIndex:5}, {theName:"PDFs", labelIndex:1}, {theName:"Art", labelIndex:4}, {theName:"Text", labelIndex:3}, {theName:"Final Files", labelIndex:7}, {theName:"Quote Invoice", labelIndex:6}}

display dialog "Job Name:" default answer ""
set j_name to text returned of result

set myPath to choose folder with prompt "Choose Folder"

set dateString to do shell script "date -n +%m%d%y"
tell application "Finder"
	set main_folder to make new folder at myPath with properties {name:j_name & space & dateString}
	repeat with aParam in folderParameters
		set aFolder to make new folder at main_folder with properties {name:j_name & space & theName of aParam & space & dateString}
		set label index of aFolder to labelIndex of aParam
	end repeat
end tell

Thanks Hank for the suggestions and tips. I knew there had to be better way to write my script. From browsing this site, I have seen guys like you help out a lot of others with making their scripts better. I welcome all advice I can get.

I have downloaded, googled and bookmarked any resources I can find to learn Applescript but I have a long way to go. It can be time consuming and challenging to learn this way, but fun and exciting too. I have quite a list of my scripts on my to do list and it keeps growing. With my combination of a little logic, creativity and laziness, I am sure it will never end.

You have a great attitude Cindy. I started back in 2006 just like you by trying things myself and looking for any information I could find. This website is the best resource. First you can find many tutorials. You can find a list of them here. I did the ones titled “Applescript Tutorial for Beginners” and they’ll give you a strong understanding of the tools you need to create scripts. Then you can post questions. As I learned more I even started answering questions for people. I found that by trying to help others I was learning myself how to solve problems, so I encourage you to do the same. The other tool you’ll want to become familiar with is the “Applescript Language Guide”. It’s a great resource and I referred to it often. You can find that under the help menu in ApplescriptEditor.

The only skills you need to learn applescript are the ones you have… enthusiasm for it and persistence to keep at it. So good luck!

Amen, Hank! :slight_smile:

And, go, Cindy, GO! :smiley: Keep up the great attitude and don’t stop asking, “Hmm. I wonder if I could.”. (I’m one of the rarer individuals who actually makes a living writing Applescripts (with shell, Javascript, etc. thrown in for good measure - lol) and have for well over a decade. So it’s not only a great brain workout but it can have financial benefits as well, if you’re so inclined.)

Keep posting and asking! Cheers!

Jim
BLUEFROG