10.6: Gather Finder Items with a new Service

Mar 10, '11 07:30:00AM

Contributed by: Frederico

Snow Leopard only hintI 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:

To use this Service: Please Note: I am a bit rusty at AppleScript; I had to fool around with shell to coerce and workaround Error -1700 permissions issues; please feel free to comment and offer cleaner solutions.
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.]

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=201103091457227