PDA

View Full Version : Wrapping a selection with a folder


peta
Dec 05, 2009, 02:35 PM
Hi guys (and lads),


I love Path Finder and its advanced features for working with files and folders. But there was one feature I wished myself almost a thousand times ...

Wrapping a selection of files/folders by another folder!

And finally, I came up with short and simple solution for my demand. A AppleScript that shows a input dialog where the user can enter a folder name or a path, and the selected items are moved into this folder, and, for the case the directory doesn't exist yet, it will be created. As a gimmick I added Growl support, where the user gets displayed a short summary about how many files were wrapped, or, for the case an error occured he will be informed about what happened. here are some pictures where you can see the script in action. (Compiled and called via Quicksilver)

http://img204.imageshack.us/img204/2871/bild12z.png
http://img204.imageshack.us/img204/1514/bild13r.png
http://img708.imageshack.us/img708/2263/bild14.png


Source code:

-- PF.WrapSelectionWithFolder.applescript
--

registerWithGrowl()

tell application "Path Finder"

set pfSelectionList to selection

if length of pfSelectionList = 0 then
return
end if

set cwd to (POSIX path of container of first item of pfSelectionList)
set folderName to text returned of ¬
(display dialog cwd ¬
default answer ¬
"Untitled folder" with title "Enter folder name")
set joinedPath to (cwd & "/" & folderName)

if not (exists joinedPath) then
if (do shell script "mkdir " & quoted form of joinedPath) is not equal to "" then
displayGrowlMessage("Error occured", "Folder couldn't be created") of me
return
end if
end if

set listOfWrappedFiles to ""


try
repeat with i in pfSelectionList
set listOfWrappedFiles to (listOfWrappedFiles & name of i & return)
if (do shell script "mv " & quoted form of (POSIX path of i) & " " & quoted form of (joinedPath & "/" & name of i)) is not equal to "" then
error "File '" & (POSIX path of i) & "' couldn't be moved"
end if
end repeat
on error
displayGrowlMessage("Error occured", "Files couldn't be move to target folder") of me
return
end try


displayGrowlMessage("Wrapped " & length of pfSelectionList & " file(s)", listOfWrappedFiles) of me
end tell

on displayGrowlMessage(headline, msg)
tell application "GrowlHelperApp"
notify with name ¬
"DefaultMsg" title ¬
(headline) description ¬
(msg) application name "Path Finder"
end tell
end displayGrowlMessage

on registerWithGrowl()
tell application "GrowlHelperApp"
set the allNotificationsList to ¬
{"DefaultMsg"}

set the enabledNotificationsList to ¬
{"DefaultMsg"}

register as application ¬
"Path Finder" all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Path Finder"
end tell
end registerWithGrowl

spnyc
Dec 06, 2009, 09:05 AM
Works as advertised and rocks.

Thanks so much for this.

---updated---
just found a bug in the script: it pukes on apostrophes in either the selected file(s) or containing folder names

peta
Dec 10, 2009, 04:48 AM
Updated the code.

Frankly, I didn't test my script with folder names that contain special characters. But fortunately AppleScript offers a handy expression called quoted form of which does all the character conversion stuff for us. :-)

Ugimom
Dec 14, 2009, 05:16 PM
I so want this to work, but I keep getting an error. Growl reports it as "Files couldn't be move to target folder" . Looking at the script events, I'm getting an error on creation of the target folder (but it *does* create the folder). Then another error on "get quoted form of POSIX path of fsFile". Any thoughts or help?

peta
Dec 16, 2009, 05:34 AM
I so want this to work, but I keep getting an error. Growl reports it as "Files couldn't be move to target folder" . Looking at the script events, I'm getting an error on creation of the target folder (but it *does* create the folder). Then another error on "get quoted form of POSIX path of fsFile". Any thoughts or help?

Sorry dude, I'll check this issue this evening when I am at home. After the bug issued by spnyc I changed some lines and had been sure to have updated the code from my first post.

straycat
Dec 18, 2009, 04:36 AM
nice tip peta.

if i may suggest a different approach, if you wanna do it with by the contextual menu, first download and install omc (http://free.abracode.com/cmworkshop/on_my_command.html), then download and unzip these examples.

fire up omcedit and import the example plist files (they are included in the builtin command library too but i modified them a little).

after omc contextual plugin is installed, these examples will show up in pf's contextual menu under "on my command/workflow" (the "gatheritems" command might appear just under "on my command"). this submenu hierarchy can be changed in each command settings.

examples:

Fold Item: select one or more items, invoke command and all items will be automatically and individually "foldered" with the same name of each file, minus the extension.

Put Item In New Folder: same as above but will ask for a folder name, a bit like peta's applescript.

Gather Items: will put selected items in a new folder called "gathered items" with the current date appended.

these commands were not written by me, i just tweak them for my own purposes and so can you.
provided growl has a cli tool, you can include it too in these commands.

additionally, you can also create a droplet in omcedit application, and drag it to your toolbar if you prefer this approach instead of the contextual menu (having both seems very flexible).
you can also write applescripts, shellscripts or simple unix commands in omcedit which will be available through the contextual menu.

if you need help on setting up omc, drop me a line.

[edit] examples link removed for the time being. "Fold Item" work but in 2 occasions, at least one file dissapeared. still have to examine if it's related to pf missing files thread.