Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A file-gathering AppleScript Desktop
Often times, I like to gather a group of files that I have downloaded or created in their own folder, and often I would like that folder's name to be based on the name of one of the files being gathered.

This AppleScript, which I usually invoke from the Applesripts menu, does this by offering me a choice of alphabetically-sorted names that I can further modify, or create my own name. It is based on snippets and ideas I have gleaned elsewhere.

[robg adds: Copy and paste the text into a new script editor document, and save it as a script in your user's Library -> Scripts folder (create this if necessary). Then select a number of files in the Finder, and choose the script name from the AppleScript menu. You will be given a list of the selected filenames, from which you can choose one as the basis for a new folder name. After naming the folder, the chosen files will be moved into that folder. I tested on a duplicated folder, just to be sure things worked!]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[7,885 views]  

A file-gathering AppleScript | 13 comments | Create New Account
Click here to return to the 'A file-gathering AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A file-gathering AppleScript
Authored by: MtnBiker on Oct 15, '04 01:50:28PM

Nice script.

I would add to Rob's comments the following two additions/changes:

"The folder will be at the same level as the original files."

and change "save it as a script in your user's Library -> Scripts folder (create this if necessary)"

to "save it as a script in your user's Library -> Scripts -> Finder folder (create these if necessary)"

---------------

Can someone add the ability to create the folder elsewhere?

---
Hermosa Beach, CA USA



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: brandonium on Oct 15, '04 04:39:53PM
To change where the folder gets created, change line 34 of the script from:
set theLocation to the target of Finder window 1
to:
set theLocation to choose folder with prompt "Please select a destination for your new folder:"
or you could hard-code a path for theLocation variable.

[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ceffe on Oct 15, '04 05:45:35PM

I love this script. However, it does not seem to work for files on my desktop. Does anyone know how to make it work for files on the desktop, without having to open a finder window for doing the operation? For me, the desktop is the place where I most often could have great use for this kind of tidying script.
/ceffe



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ramonf on Oct 15, '04 09:12:49PM

Change set theLocation to the target of Finder window 1 to

try
set theLocation to the target of Finder window 1
on error
set the theLocation to desktop
end try

and delete select newFolder (this was there to return focus to the finder window on System 9, if I remember right)



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ceffe on Oct 16, '04 07:19:03AM

Thanks. It works better with these adjustments. However, the new code does not work as intended if a finder window is open, albeit inactive. In those cases the new folder is created within the folder of the inactive finder window rather than on the desktop.
/ceffe



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ramonf on Oct 16, '04 01:33:07PM

I see the problem, sorry. Now, the solution I propose makes the behaviour somewhat inconsistent. There are now two cases:

  1. If the files to be gathered are in different folders, which can happen in a selection made on a window in list view, then the new folder will be made in the Finder's frontmost window.
    Notice that in this case I have handled idiosyncratically (or idiotically) the situation in which more than one file or folder has the same name. If this happens the script moves some files but not others, the alternative (which would require some more coding) is to alert the user and do nothing.
  2. If the files to be gathered are in the same folder then the new folder will be created in that folder. This will cover the cases in which the selection is on the desktop or in a window in icon view.
    Notice that in this case the script will not handle well the situation in which the new folder has the same name as one of the items being gathered (a fix would be to give it initially a temporary name, move the files and then rename the new folder). Right now it will tell you that the name is already taken.

The changes to be made to the original script are:

  1. Change
    
    set listNames to {}
    repeat with listItem in theList
    	set listNames to listNames & {basename(the name of listItem as string)}
    end repeat
    
    set listNames to sort(listNames)
    
    to
    
    set listNames to {}
    set listParents to {}
    repeat with listItem in theList
    	set listNames to listNames & {baseName(the name of listItem as string)}
    	set listParents to listParents & {the parent of listItem as string}
    end repeat
    
    set listNames to sort(listNames)
    set listParents to sort(listParents)
    
    set soleParent to (count listParents) = 1
    
  2. Change set theLocation to the target of Finder window 1 to
     
    if soleParent then
    	set theLocation to the parent of item 1 of theList
    else
    	set theLocation to the target of Finder window 1
    end if
    
  3. Delete select newFolder.
  4. Change " | sort -f" to " | sort -f -u" in the sort procedure.

A much more elegant fix would be possible if I knew a way of knowing if the selection is on the desktop proper or on a window for the desktop.



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ceffe on Oct 17, '04 07:17:11AM

Impressing! Thanks, this fully sorts the problem I was having. Now it is a nifty time-saver for tidying my desktop (as well as messy folders). I even dragged an app-version of this script to the finder toolbar for easy access when in folder windows.
Thanks again,
ceffe



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: szabesz on Oct 18, '04 12:11:02PM

Ooops. This version has created a folder and moved my Desktop folder into it when running the script from the Desktop folder and clicking on the "Enter your own" button. Then nothing happened. I had to move back my Desktop folder to its place as root, since the Finder allowed me only to copy it back, but it was using the original one in the folder created by the script!

I do not have time to check the code why it does so, but I want to warn everybody!



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: ramonf on Oct 15, '04 08:48:04PM
I have made one small change since I posted this script. So that the name choices it gives contains no duplicates change
" | sort -f"
to
" | sort -f -u"
in the sort procedure.

[ Reply to This | # ]
A file-gathering AppleScript
Authored by: loren_ryter on Oct 17, '04 02:27:53PM

It seems like everyone got this to work. But the .txt file contains several garbage characters. (like where one is supposed to have opt-return at the end of the line, and curly quotes).

Can anyone repost a fixed version or tell me what I'm doing wrong?

I'm opening the link in Safari.



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: loren_ryter on Oct 17, '04 02:32:26PM

never mind. downloading it rather than opening it in Safari and copying that into SE worked fine. Thanks.



[ Reply to This | # ]
A file-gathering AppleScript
Authored by: adrianm on Oct 18, '04 01:22:09PM
Alternatively, from Safari's View menu, choose Text Encoding -> Western (Mac OS Roman). The characters look fine then.

[ Reply to This | # ]
A file-gathering AppleScript
Authored by: dogboy on Oct 18, '04 07:38:30AM

Great hint. Thanks. It's very useful.



[ Reply to This | # ]