script to move selected files to a new folder?

I’m a total novice at the scripting thing.

After moving to SL I’m mourning the loss of Fruitmenu, in particular the contextual menu option which would gather selected Finder files in a new folder, named after the time and date this action was performed.
It seems like such an obvious file management function, it’s hard to believe that after more than two decades of personal computing this kind of thing isn’t part of the OS (or is it?).
Has anyone compiled a script to perform such an action (or even better offer the option to name the new folder)?

I would be very grateful if anyone can point me in the right direction, or advise how to write my own…

cheers

It isn’t that complicated:

(* 1. GATHERING FILES *)
tell application "Finder" to set theObjects to selection

(* 2. RENAME THE FILES *)
repeat with i in theObjects
	-- Get current date and time (MM-DD-YYYY**HHh MMm SSs
	tell (current date) to set theCurrentDate to (((month of it) as integer) & "-" & day & "-" & year & "**" & (hours of it) & "h " & (minutes of it) & "m " & (seconds of it) & "s") as string
	
	-- Get name extension of original file
	tell application "Finder" to set fileNameExtension to name extension of i
	
	-- Rename file
	tell application "Finder" to set name of i to (theCurrentDate & "." & fileNameExtension) as string
end repeat

Hope it works,
ief2

You should check out Automator actions too

Hi. My example presents a text edit box, letting you name the destination folder and, concurrently, relocate your selections.

tell application "Finder" to move selection to (make folder with properties {name:((display dialog "" default answer "")'s text returned)})

Ow sorry, I read your question wrong, this should be right:

(* 1. GET SELECTED FILES *)
tell application "Finder" to set theObjects to selection

(*2. CHOOSE DESTINATION TO MAKE NEW FOLDER *)
set myContainer to choose folder with prompt "Choose a destination to make a new folder"

(* 3. MAKE NEW FOLDER *)
-- Get current date and time (MM-DD-YYYY**HHh MMm SSs
tell (current date) to set theCurrentDate to (((month of it) as integer) & "-" & day & "-" & year & "**" & (hours of it) & "h " & (minutes of it) & "m " & (seconds of it) & "s") as string
-- make new folder
tell application "Finder"
	make new folder at myContainer with properties {name:theCurrentDate}
	set myNewFolder to folder theCurrentDate of myContainer
end tell

(* 4. MOVE THE FILES *)
repeat with i in theObjects
	tell application "Finder" to move i to myNewFolder
end repeat

(* 5. OPTIONAL OUTPUT *)
set myMsg to (((count theObjects) as string) & " where moved to " & ((POSIX path of myNewFolder) as string)) as string
display dialog myMsg buttons {"OK", "Reveal New Folder"} default button 2 cancel button 1
-- if "Reveal New Folder"
tell application "Finder" to reveal myNewFolder

Hope it works,
ief2

Thanks so much for your replies,

both solutions work, but ief2 I’ve tweaked your script to better suit my needs:
(my preferred date format is DD MM YYYY)

(* 1. GET SELECTED FILES *)
tell application "Finder" to set theObjects to selection

(*2. CHOOSE DESTINATION TO MAKE NEW FOLDER *)
set myContainer to choose folder with prompt "Choose destination for selected items"

(* 3. MAKE NEW FOLDER *)
-- Get current date and time (DD Month YYYY HHh MMm
tell (current date) to set theCurrentDate to ("Gathered " & (day as integer) & " " & (month of it) & " " & year & " " & (hours of it) & "." & (minutes of it) & "") as string
-- make new folder
tell application "Finder"
	make new folder at myContainer with properties {name:theCurrentDate}
	set myNewFolder to folder theCurrentDate of myContainer
end tell

(* 4. MOVE THE FILES *)
repeat with i in theObjects
	tell application "Finder" to move i to myNewFolder
end repeat

tell application "Finder" to reveal myNewFolder

I really have very little understanding of applescript syntax but I guess I need to start somewhere!

A modification I’d love to make is to have the time stamp in 12hr format - with AM/PM suffix (I don’t care about seconds) and also keep leading zeros in there…
I can’t see any reference to time stamps in the AppleScript Dictionary, is there somewhere else I should be looking?

Also how would I include a (unselected) button in the “Choose Destination” dialog to simply create the “Gathered Items” folder in situ (ie.within the folder the selected items are already in)?

Marc,
your suggestion is elegantly simple but it defaults to place the new folder on the Desktop.
What needs to be added to create the new folder within the folder the selected items are already in?
Also where in the script should I place a descriptive title for the dialog that comes up?

Thanks again for your times!!!

Hi,

a timestamp can be created easily with the date shell command

set timestamp to do shell script "/bin/date +%d' '%b' '%Y' '%l' '%M' '%p"
--> dd mon yyyy HH MM AM

the %b parameter prints the 3 letter abbreviation of the month’s name

The appearance of the choose folder dialog box cannot be changed,
but there is a “New Folder” button at bottom left

What needs to be added to create the new folder within the folder the selected items are already in?
Change (*2. CHOOSE DESTINATION TO MAKE NEW FOLDER *) into

(*2. CHOOSE DESTINATION TO MAKE NEW FOLDER *)
set buttonReturned to button returned of (display dialog "In which folder do you want to gather the files?" buttons {"Custom Folder", "New Folder In Folder Of Chosen Files"} default button 2)
-- if  "New Folder In Folder Of Chosen Files"
if buttonReturned is "New Folder In Folder Of Chosen Files" then
	-- get name for new folder
	set nameForNewFolder to text returned of (display dialog "Choose a name for the new folder:" default answer "Gathered")
	if nameForNewFolder is "" then set nameForNewFolder to "Gathered"
	-- make new folder
	tell application "Finder"
		set myOrFilesContainer to (container of item 1 of theObjects) as alias
		try
			make new folder at myOrFilesContainer with properties {name:nameForNewFolder}
			set myContainer to (folder nameForNewFolder of myOrFilesContainer) as alias
		on error theMsg
			display dialog theMsg buttons "Cancel" cancel button 1
			return
		end try
	end tell
end if
-- if "Custom Folder"
if buttonReturned is "Custom Folder" then
	set myContainer to choose folder with prompt "Choose destination for selected items"
end if

Also where in the script should I place a descriptive title for the dialog that comes up?
Which dialog do you mean? The Choose File-dialog or the dialog which displays the result of your script?