Create a new subfolder of any selected folder in Finder with AppleScript

Alan Shaw
InstructorAlan Shaw
Share this video with your friends

Social Share Links

Send Tweet

I can make a subfolder of the folder that is open in Finder, but there's no convenient way to make a subfolder of amy other folder that is showing. Let's add a Finder service to take care of that.

Instructor: [00:00] I open Automator, I choose service. The service is going to receive folders in finder. I go to utilities, run AppleScript and drag it into the pane. It contains the outline of a run handler. The thing about AppleScript is that all code must reside in a handler which is a function. You can define a handler with on Name, ending with end Name.

[00:37] When a script runs, what runs is its run handler. If you pass a plain block of code to AppleScript it will have an implicit run handler. We use an explicit run handler here so that we can get hold of the input. To emphasize that, I'm changing its name to argv. We won't concern ourselves with the parameters for now. The first thing we want to do is make sure we're passed exactly one argument. That's the folder we'll be putting the subfolder into.

[01:04] We say if length of argv is not 1 then something, else something else, end if. If length of argv is not 1, then what we want to do is display "Alert!" telling the user to select a single folder. Now we grab that single folder that was passed in and save its path name in a variable. In AppleScript the equal sign is an equality test, not an assignment operator. This is how you do assignments.

[01:34] We say POSIX path, because otherwise we get the path in the old Mac format with colons instead of slashes. I want to show you what went wrong with this line when I first wrote this script, so I'll try to run it. Automator tells me I need to simulate finer, using the Get Specified Finder Items action. So I cancel, and go to files and folders and drag Get Specified Finder Items ahead of my AppleScript, and choose a folder.

[02:10] Now I run my workflow again. I can ignore the warning, hitting OK, and I get this error. Why? Look at parent, it's purple. What else is purple? Length of array, and POSIX path of first item of argv. These are properties, and so is parent. Who knew? Parent is a property of something. I may not know of what, but I do know that I can't use it as a variable name, so I change it to parentPath. Hit run again to get the compiler to notice, and parentPath is green.

[02:53] Now I want to get the name of the subfolder that the user wants to create. Display dialog "New folder name" default answer "New folder". You can imagine what that does. textReturned is a property of the dialog. Now I have the parent folder's path, and the new folder's name, and I construct and run a shell command to make the new folder. The AppleScript command to run a shell script is do shell script followed by a string representing the script.

[03:22] We start the script by saying mkdir and then a space, and then we put quotation marks which we need to escape here, because they're within a string around the path name of the directory we wish to create, in case it contains any white space characters. We have an escaped quote here, and an escaped quote here. AND is the concatenation operator for strings, so we next have parentPath followed by a slash, followed by childName, the name of the new folder.

[03:52] Let's remove our test harness, the ask for finder items action, and save the script as New Subfolder. Now if I go to my finder, I click on my folder and I find under services New Subfolder. It asks for the new folder name and I'll say My Subfolder, and there it is.

[04:30] Of course, it's preferable if I intend to use this thing a lot to give it a keyboard shortcut. I go to finder, services, services preferences. I find my service here, and I add a shortcut. Let's say command backslash. I've selected my folder, I hit command backslash and I enter the name My Other Subfolder, and there it is.