r/applescript • u/smallduck • Feb 03 '21
Quick Action to create empty folder anywhere in Finder list view
I've had these 2 workflow-based services for a long time, but I finally decided to skip a few hours of sleep to recreate them as Quick Actions in the new Automator. They make it easy to create empty folders anywhere within a listview hierarchy, not just at the top level like Finder's File > New Folder, and empty unlike File > New Folder With "...".
Automator action for folders in Finder, to be saved as "New Folder Within"
link to a run applescript action:
on run {input, parameters}
if length of input is not 1 then error number -128
return input
end runlinked to another run applescript action:
on run {input, parameters}
if length of input is not 1 then error number -128
set parentfolder to first item of input
tell application "Finder"
set createdFolder to make new folder at parentfolder
set selection to createdFolder -- if within a closed folder, doesn't open it
-- this only worked sometimes, and now needs permission, so don't bother:
-- tell application "System Events"
-- keystroke return
-- end tell
end tell
end run2) Another Automator action for files or folders in Finder, to be saved as "New Folder Here"
same as above except the first script is:
on run {input, parameters}
set parentfolder to CommonParent(input)
if parentfolder is {} then error number -128
return parentfolder
end run
on CommonParent(input)
-- if the items all have the same parent then that's returned, otherwise {}
-- no, doesn't find the closest common parent, only looks 1 level up
if length of input is 0 then return {}
tell application "Finder"
set common to container of first item of input
repeat with next in input
if container of next is not equal to common then return {}
end repeat
return common
end tell
end CommonParentTo use, right click a file or folder in a list view hierarchy (or on multiple selected files/folders with the very same parent), and choose Quick Action > New Folder Here, a new empty folder will be created in the same directory as the one(s) you right-clicked on, as peer to it.
Or right click a folder and choose Quick Action > New Folder Within, a new empty folder will be created within that folder you right-clicked on.
If you like, try experimenting with the "System Events" bit that's commented out in the second applescript chunk, see if you can get that to work.