Scripting Finder File Menu Item with variable name

Why did Apple do that!?! File / ‘New Folder with Selection’ would be very usable (and Scriptable) IF the menu item’s name DIDN’T change. But, someone thought it would be a ‘good idea’ to have the menu item add the number of items selected EVERY time. Does anyone know how to work around that?

mllovett. If I understand your request correctly, the script included below should do what you want. You may want to add some error correction to the script, although including the System Events command in a try statement is probably just as good.

--tested on computer with macOS Tahoe 26.0

tell application "Finder" to activate
delay 0.2 --this isn't necessary on my computer.
try
	tell application "System Events" to keystroke "n" using {command down, control down}
end try

FWIW, moving selected files and folders into a newly created folder in a Finder window can also be done with a regular script without GUI scripting. This approach allows you to tailor the operation of the script to work how you want (e.g. the folder name and what to do when the folder already exists).

set folderName to "New Folder with Items" --set to desired folder name

tell application "Finder"
	# 	activate --if desired
	set selectedItems to selection
	if selectedItems is {} then error number -128
	set parentFolder to container of item 1 of selectedItems
	try
		set newFolder to (make new folder at parentFolder with properties {name:folderName})
		move selectedItems to newFolder
	on error
		display alert "A folder with the name \"" & folderName & "\" already exists"
	end try
end tell

I don’t know if the menu item is question is new to Tahoe, so I’ve included a screen shot below just for general information.

1 Like

Here is a very simple script to click the menu item

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "System Events"
	tell application process "Finder"
		set frontmost to true
		delay 1
		tell menu item 3 of menu "File" of menu bar 1
			if enabled then click
		end tell
	end tell
end tell

Why is the name changing a problem for you?

:~) … A simple thank you might do, but wait, there’s more! My stab at using System Events and key codes wasn’t working, but here’s what I did with your simple ~working~ answer, and the reason for the roundabout path… I pasted your code as provided into an Automator Quick Action ‘Run Applescript’ (commenting out the run() bits) … which means that I was able to assign a one-key shortcut… (which BTW shows-up in the Services menu). I’ve moved files into new folders for years using Applescript, but the introduction (when?) of this new Finder function caught my interest. Long story short: one keystroke now does it! I still have to use AS to rename this batch of new folders, but the Finder is doing the moving! Thank you, again. PS: the short code at the top of your list of suggestions is ths one I used…

‘New Folder with Selection’ was introduced on Lion (2011).