I am trying to wean myself from numerous utilities that add System and Finder functionality and replace them with Services wherever possible. One indispensable (for me) feature of Unsanity's FruitMenu is the 'Gather Items in New Folder' Contextual Menu Item.
I tried to replace this with vanilla AppleScript and Automator, but AppleScript doesn't like letting you play around in certain directories, especially the Trash (.Trash) folders.
Other hints here approach this task with varied techniques; i found none that played well in trash or named folders automatically and uniquely to my tastes.
One example of why I need to these unique abilities is, after updating iOS apps, they all land in the Trash, and, in case I wish to revert to these older versions (c.e.: Twitter v3.3), I make a practice of opening the trash, gathering all .IPA files into a new folder, and moving that folder to a safety archive for easy retrieval.
This service is also exceptionally useful for cleaning up the Downloads folder, Pictures, Desktop, etc.; anytime you have a selection of Finder objects (files and folders) you wish to gather into a new folder.
The script below can be run as a plain AppleScript from something like FastScripts or AppleScript Menu, but is best run as a Service item.
To make the Service:
- Open Automator; make new file of type 'Services.'
- Set 'Service receives selected' to 'files or folders' in 'Finder.'
- Select Utilities » Run AppleScript (double-click to insert in workflow).
- Replace "(* Your script goes here *)" with the AppleScript below; be sure to not overwrite the 'on run', 'return input' and 'end run' parameters.
- Save your new Service as 'Gather Items in New Folder' or similar name.
- Select multiple items (they need not be contiguous or even in the same folder).
- Right-click (Control+Click) to access the above-named Service; and wait for completion.
- Press 'Return' to immediately Rename the newly created folder as desired.
tell application "Finder"
(* what files do you want to gather into a new folder? *)
set selectedItems to selection
if selectedItems is not {} then
(* topLevelName assures the uppermost file-level-directory will be selected in case of multiple selections in list or column view. *)
set topLevelName to name of item 1 of selectedItems
(* silly set of workarounds to override vanilla AppleScript's lack of permission when playing in the .Trash folders *)
set selectionPath to POSIX path of file ((item 1 of selectedItems) as string)
set selectionPath to ((characters 1 thru ((offset of topLevelName in selectionPath) - 1)) in selectionPath) as string
(* Modify path string to create a unique target folder using date and time *)
set dateString to do shell script "date \"+%Y.%m.%d %a %I.%M.%S %p\"" -- this line broken out for easy editing
set newFolder to selectionPath & (dateString & " Gathered Items") -- rearrange as you please; is set this way for proper name sorting
(* more workarounds to allow playing in the .Trash *)
do shell script "mkdir " & quoted form of newFolder
set newFolder to (POSIX file newFolder) as alias
move selectedItems to newFolder
reveal newFolder
end if
end tell
[crarko adds: I tested this, and it works as described. I've mirrored a copy of the final working Service here. Just open the workflow in Automator and re-save it as a Service.]

