Imitate how WINDOWS let me make new folder while saving with a script?

I’m a Mac user who, as of recently, switched from using a WINDOWS computer, and I confess that I miss the way in which you could right-click > new folder while saving. (I’m constantly deciding to make new folders on the spot; in short, I stay organized ONLY by being organized, NOT by employing the cute compensatory techniques of the spotlight, ⌘F, and Smart Folders, provided for you by the OS.

Recently I stumbled upon this blog post (; one of the scripts the poster includes is one that opens a box that asks you to type in the number of which space (referring to leopard’s spaces) you want the front window to be assigned to.

It got me thinking, what if I could make a script that could (and this is the holy grail for me):

¢ pop-up a text box that asked you what you wanted to call the new folder: you would NOT have to specify where you want the folder to be located, as that would be set to the current location of the save-dialogue-box (if that’s what it’s called). Then, I would have a keyboard-shortcut in place to activate the script.

Of course, if the keyboard shortcut were not available, it would be somewhat to my satisfaction, which is better than not at all to my satisfaction. Likewise with obvious other less-nice potential alternatives.

Credit goes to Jesse Newland and here’s the aforementioned script:

set app_identifier to bundle identifier of (info for (path to frontmost application))
set app_identifier to do shell script "echo " & quoted form of app_identifier & " | /usr/bin/perl -pe 'use encoding utf8; s/(\\w)/\\L$1/gi'"

set displayString to "Assign " & app_identifier & " to Space:"
set defaultAnswer to 0
	set response to display dialog displayString default answer defaultAnswer
		set theNumber to (text returned of response) as number
		exit repeat
	on error errstr
		set displayString to errstr & return & "Please try again."
		set defaultAnswer to text returned of response
	end try
end repeat
set app_construct to (run script "{|" & app_identifier & "|: " & theNumber & "}")

tell application "System Events"
	tell spaces preferences of expose preferences
		set app_layout to application bindings
	end tell
end tell
set app_identifier to bundle identifier of (info for (path to frontmost application))
	app_layout as number -- causes an error anyway
on error errstr
	set app_locations to {}
	set {TID, text item delimiters} to {text item delimiters, "{"}
	set errstr to text item 2 of errstr
	set text item delimiters to "}"
	set errstr to text item 1 of errstr
	set text item delimiters to ", "
	set errstr to text items of errstr
	set text item delimiters to TID
	repeat with i in errstr
		set o to offset of "|:" in i
		tell i
			set app_location to {}
			set end of app_location to text 2 thru (o - 1)
			set end of app_location to text (o + 2) thru -1
			set end of app_locations to app_location
		end tell
	end repeat
end try
set spaces_bindings to {}
repeat with i from 1 to count app_locations
	if app_identifier = item 1 of (item i of app_locations) then
		--skip it
		set spaces_bindings to spaces_bindings & (run script "{|" & item 1 of (item i of app_locations) & "|: " & item 2 of (item i of app_locations) & "}")
	end if
end repeat
tell application "System Events"
	tell expose preferences
		tell spaces preferences
			set application bindings to spaces_bindings
			set spaces_bindings to app_construct & spaces_bindings
			set application bindings to spaces_bindings
		end tell
	end tell
end tell

Hi, solfrejazz, and welcome.

The standard Mac OS X “Save As.” dialog has a “New Folder” button which does exactly what you seem to be saying.

A script can make use of this dialog by calling the ‘choose file name’ command. If the user clicks the “New Folder” button and specifies a new folder, the folder’s created within this process without the need for any action by the script. The dialog returns a file specification for the chosen location/file name and it’s then up to the script invoke the necessary ‘save’ process from the scripted application or from itself.

tell application (path to frontmost application as text)
	-- The 'with prompt', 'default name', and 'default location' parameters are optional.
	set saveFile to (choose file name with prompt "Save this document as .?" default name "My doc name.txt" default location alias "path:to:root folder for this purpose:")
end tell

-- Scripted application:
tell application "Blah" to save document 1 in saveFile

-- or vanilla AppleScript:
set fRef to (open for access saveFile with write permission)
	set eof fRef to 0
	write myData to fRef
end try
close access fRef

Thanks a bunch. It’s funny how I’m a better than average computer user that can teach myself a little basic applescript, but wasn’t I able to realize the “New Folder” button all those times I wanted to save & create a new folder in one step.

It has to do with what you expect, and sometimes you forget to explore what’s right in front of your eyes.