script to move item from desktop to folder in separate volume

Hello all,

I am glad to have found this forum, because I am quite interested in learning how to make my own scripts. Up until now, the terminal and script editor have been two things that scare me off from achieving what I really want to do. I’ve decided to attack this problem and start learning. My only problem is that I spend so much time working, and have very little time to actually learn. So, I must take baby steps. I have gotten a lot of help from the apple forums, but it’s time to step it up.

The current item I am working on, is a script to move items from my desktop to a folder in a separate partition on my HD. I actually have a few of these I need to make, but all of them use the same concept of renaming and moving, some also open the file. I can use automator to do all the other things like opening in application and renaming, but would there be any advantage to putting these other commands into the script? A user over at the apple forums was kind enough to work on this with me, well, he actually did it all for me! This script is actually part of a service which names a selected file or files, generally PDFs, with the date created and a description of the document. Then, it moves them into the folder on the other partition of my Mac HD. The reason for making the script and not just using “move finder items” in automator, is because there is often more than one file moved in the same day. Since the file is named with the date, I needed finder to add a -1,-2,-3, etc., after the file name if it was a duplicate. The script is supposed to remove the original file after the move. So far, the script works fine if the target folder is on the same partition as the source; but when the target is on a different partition, the original remains on my desktop. I would like to be able to find out how to modify the script so the original file is not left on the desktop anymore. Any advice would be greatly appreciated, because at this point I don’t really understand any of the script enough to figure it out!

Here is one of the examples, this particular one is for shipping labels. Shipping labels get saved to desktop with the default name of something like
vty3RlsIK.LabelGenerationServlet.part

Then I would change the name to something like
12-31-2009_USPS_Label.pdf

Then, I would move it to the folder in the partition “Storage”, where we keep the shipping labels for records.

So far the service is set up in automator as the following:
service receives selected file or folder
in finder
name single item in finder item names (to: USPS_Label.pdf)
add date or time to finder item names (before name, underscore separator)
run applescript


on run {input, parameters}
	
	tell application "Finder"
		
		set target_folder to folder "Storage:Business:Shipping:USPS:Labels"
		
		set N to number of items in input
		repeat with j from 1 to N
			set this_item to item (N - j + 1) of input
			
			
			set curname to ((name of this_item) as text)
			
			if exists item curname of target_folder then
				
				set length_extension to number of text items in (name extension of this_item as text) -- length of the extension of the current file
				
				set current_name_minus_extension to text 1 thru (-length_extension - 2) of (name of this_item as text) -- current name without extension
				
				set i to 1
				repeat until (not (exists item (current_name_minus_extension & "-" & i & "." & name extension of this_item as text) of target_folder)) -- looking for the first item in the target folder to make sure that no file named "currentname-i" exists
					
					set i to i + 1
					
				end repeat
				
				set new_name to (current_name_minus_extension & "-" & i & "." & name extension of this_item as text) --appending -i to the name
				
				set name of this_item to new_name --add -i at the end of the name before the extension
				
			end if
			move this_item to target_folder
			
		end repeat
	end tell
end run

I hope that explains what I am trying to do. The only thing I want to change is to make sure that the original file is deleted from the desktop after the move.

Thanks for taking the time to help, I’m glad to be a part of the forum.
vegas

Is it something like this you’re after?

on adding folder items to theFolder after receiving theObjects
	repeat with anItem in theObjects
		
		-- if theFile ends in ".LabelGenerationServlet.part"
		tell application "Finder" to if name of anItem ends with ".LabelGenerationServlet.part" then
			
			-- get new name for object as string
			tell (current date) to set newNameString to (((month of it) as integer) & "-" & day & "-" & year & "_" & (hours of it) & "-" & (minutes of it) & "-" & (seconds of it) & "_USPS_Label.pdf") as string
			tell application "Finder"
				-- rename file
				set name of anItem to newNameString
				-- move file
				move (item newNameString of folder heFolder) to INSERTPATHTOVOLUME
				-- delete the original file if still in desktop (you said you had problems)
				if (exists item newNameString of alias (theFolder as string)) then delete (item newNameString of theFolder)
			end tell
		end if
	end repeat
end adding folder items to

You’ll have to connect the applescript with your desktop using the folder-actions.

Thanks for the response!

Were you thinking that instead of adding the -1, -2, etc., I could just use the date with minutes and seconds to keep them in order? That’s a great idea! However, I think it would be better if it was the date created instead of current date, so I could select multiple files at once and they wouldn’t all have the same name. Does that make sense?

I wanted to try the script, but I couldn’t quite get it to work. I created the service with get selected files, then and run applescript, but it didn’t work. I really am a beginner with this stuff, so I don’t know exactly what to look for. I didn’t get an error, it just didn’t work.

I used this syntax for the target directory

  move (item newNameString of folder heFolder) to folder "Mac HD:Users:username:documents"

Should that work?

Edit to add:
I forgot one other thing here. It might be better that it is any selected file, not just one ending in that extension. For this particular script, it’s okay, but some others that I want to use it for, they don’t always have the same suffix. This might work, I’m just not 100% sure. It seems that any selected file leaves it open to more possibilities, right?

If you save this as an application you can either drop items to it, or double click and run it. If you double click it, the script asks what you want to do. It can be “Rename and move selected items in the Finder” or “Choose items”. Then it asks a last time for your permission and starts renaming and moving the files. If you just drop files on it, it doesn’t ask for what you want to do, off course. It just asks for permission and processes all the dropped items.

The script:

-- this is called when you save the script as an application an drop items on it thru the Finder
on open objectsToMove
	-- ask for permission
	set filesToMoveString to {}
	repeat with i in objectsToMove
		tell application "Finder" to set end of filesToMoveString to name of i
		set end of filesToMoveString to return
	end repeat
	display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1
	-- get container of items
	tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
	-- call renameAndMoveFiles-handler
	renameAndMoveFiles(objectsToMove, myContainer)
end open

-- this is called when you double click the application or run it thru Script Editor
on run
	-- prompt for action
	set myChoice to (choose from list {"Rename and move selected items in the Finder", "Choose items"} with prompt "Make a choice:")
	set myChoice to myChoice as string
	
	-- if "Rename and move selected items in the Finder"
	if myChoice is "Rename and move selected items in the Finder" then
		-- get the selection in the frontmost Finder window
		tell application "Finder" to set objectsToMove to selection
		-- ask for permission
		set filesToMoveString to {}
		repeat with i in objectsToMove
			tell application "Finder" to set end of filesToMoveString to name of i
			set end of filesToMoveString to return
		end repeat
		display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1
		-- get container of items
		tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
		-- call renameAndMoveFiles-handler
		renameAndMoveFiles(objectsToMove, myContainer)
	end if
	
	-- if "Choose items"
	if myChoice is "Choose items" then
		-- get files
		set objectsToMove to (choose file with prompt "Please choose the files you want to move:" with multiple selections allowed)
		-- ask for permission
		set filesToMoveString to {}
		repeat with i in objectsToMove
			tell application "Finder" to set end of filesToMoveString to name of i
			set end of filesToMoveString to return
		end repeat
		display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1
		-- get container of items
		tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
		-- call renameAndMoveFiles-handler
		renameAndMoveFiles(objectsToMove, myContainer)
	end if
end run

-- the handler to rename and move files
on renameAndMoveFiles(theObjects, theFolder)
	repeat with anItem in theObjects
		tell application "Finder"
			-- get new name for object as string
			set creationDate to (creation date of anItem)
			tell (creationDate) to set newNameString to (((month of it) as integer) & "-" & day & "-" & year & "_" & (hours of it) & "-" & (minutes of it) & "-" & (seconds of it) & "_USPS_Label.pdf") as string
			-- rename file
			set name of anItem to newNameString
			-- move file
			move (item newNameString of folder theFolder) to INSERTPATHTOVOLUME
			-- delete the original file if still in desktop (you said you had problems)
			if (exists item newNameString of alias (theFolder as string)) then delete (item newNameString of theFolder)
		end tell
	end repeat
end renameAndMoveFiles

This just raced way past my comprehension! I really liked the concept of adding the date and time as the name of the file insead of the -1, -2, -3. I still haven’t gotten either of these to work though. I tried making an application with nothing but run applescript, and I tried making it with “get specified files” first. I think I just don’t know what I am doing here.

Is this what this is supposed to look like?

http://yfrog.com/5kscreenshot20100103at441g

And in the path for the target folder, I just tried it like this

  set name of anItem to newNameString
           -- move file
           move (item newNameString of folder theFolder) to "Mac HD"
           -- delete the original file if still in desktop (you said you had problems)
           if (exists item newNameString of alias (theFolder as stri

Should that work? I don’t need to delete the comments, right?

Sorry for being so helpless, but I just barely started using automator, so this is way over my head, I think.

No, I’m meant something like this:
1) Compile this script in ScriptEditor

2) Click the menu item save as … in the file menu (Screenshot is Dutch)
http://img253.imageshack.us/img253/7933/afbeelding1d.png

2) Choose the option save as application (Screenshot is Dutch)
http://img689.imageshack.us/img689/2927/afbeelding2bd.png

Now you can open the app from your dock, drop files on it .

Hope it works,
ief2

Thanks you very much for explaining everything, I completely understand now! No problems getting it to work now. While this does work, I’m afraid that it’s not the best solution because I have about 6-10 of these that I need. To have all those in my dock will be kind of annoying. I could make a service to call up the application itself, but that is still a lot more steps than the original script I posted which only needed to select the files and select the service.

Is it possible to use your script in a way that it would function the same as my original service? What I mean is, can I make it so that I can just select a group of files on the desktop and run run a service to process the files without any prompt? I prefer to be able to select the file or files, and run the action after selecting them. The drag and drop would work okay for this, but if I have to add so many of these to my dock, it’s just not ideal. Is that even possible?

All right. I don’t know exactly what you’re after, but I commented a few thinks out in my script. (No more prompts)

Now when you save it as an application, and you drop files on it it just moves them, without any user-interaction. When you run the program by double clicking it, it gets the selected Finder objects and moves them to your path (variable: INSERTPATHTOVOLUME).

Script:

-- this is called when you save the script as an application an drop items on it thru the Finder
on open objectsToMove
	(* -- ask for permission
	set filesToMoveString to {}
	repeat with i in objectsToMove
		tell application "Finder" to set end of filesToMoveString to name of i
		set end of filesToMoveString to return
	end repeat
	display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1 *)
	-- get container of items
	tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
	-- call renameAndMoveFiles-handler
	renameAndMoveFiles(objectsToMove, myContainer)
end open

-- this is called when you double click the application or run it thru Script Editor
on run
	(* -- prompt for action
	set myChoice to (choose from list {"Rename and move selected items in the Finder", "Choose items"} with prompt "Make a choice:")
	set myChoice to myChoice as string *)
	
	set myChoice to "Rename and move selected items in the Finder"
	-- if "Rename and move selected items in the Finder"
	if myChoice is "Rename and move selected items in the Finder" then
		-- get the selection in the frontmost Finder window
		tell application "Finder" to set objectsToMove to selection
		(* -- ask for permission
		set filesToMoveString to {}
		repeat with i in objectsToMove
			tell application "Finder" to set end of filesToMoveString to name of i
			set end of filesToMoveString to return
		end repeat
		display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1 *)
		-- get container of items
		tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
		-- call renameAndMoveFiles-handler
		renameAndMoveFiles(objectsToMove, myContainer)
	end if
	
	(* -- if "Choose items"
	if myChoice is "Choose items" then
		-- get files
		set objectsToMove to (choose file with prompt "Please choose the files you want to move:" with multiple selections allowed)
		-- ask for permission
		set filesToMoveString to {}
		repeat with i in objectsToMove
			tell application "Finder" to set end of filesToMoveString to name of i
			set end of filesToMoveString to return
		end repeat
		display dialog ("Are you sure you want to rename and move the following files?" & return & return & (filesToMoveString as string)) as string buttons {"No", "Yes"} default button 2 cancel button 1
		-- get container of items
		tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
		-- call renameAndMoveFiles-handler
		renameAndMoveFiles(objectsToMove, myContainer)
	end if *)
end run

-- the handler to rename and move files
on renameAndMoveFiles(theObjects, theFolder)
	repeat with anItem in theObjects
		tell application "Finder"
			-- get new name for object as string
			set creationDate to (creation date of anItem)
			tell (creationDate) to set newNameString to (((month of it) as integer) & "-" & day & "-" & year & "_" & (hours of it) & "-" & (minutes of it) & "-" & (seconds of it) & "_USPS_Label.pdf") as string
			-- rename file
			set name of anItem to newNameString
			-- move file
			move (item newNameString of folder theFolder) to INSERTPATHTOVOLUME
			-- delete the original file if still in desktop (you said you had problems)
			if (exists item newNameString of alias (theFolder as string)) then delete (item newNameString of theFolder)
		end tell
	end repeat
end renameAndMoveFiles

Note: I didn’t remove a single line in the script. I just them like comments by placing them between (* & *).

Hope it’s good now,
ief2

EDIT: For the people that were wondering, here’s is the script that really solved this man’s problem:

property destinationFolder : choose folder with prompt "Choose the destination folder" -- or insert the path here

on open objectsToMove
	tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
	renameAndMoveFiles(objectsToMove, myContainer)
end open

on run
	tell application "Finder" to set objectsToMove to selection
	if objectsToMove is {} then display dialog "No items selected in the Finder" buttons "Quit" default button 1 cancel button 1
	tell application "Finder" to set myContainer to (container of (item 1 of objectsToMove)) as alias
	renameAndMoveFiles(objectsToMove, myContainer)
end run

-- the handler to rename and move files
on renameAndMoveFiles(theObjects, theFolder)
	repeat with anItem in theObjects
		set anItem to anItem as alias
		tell application "Finder"
			set creationDate to (creation date of anItem)
			set myDays to my twoCharacterDate(creationDate, "day")
			set myMonth to my twoCharacterDate(creationDate, "month")
			set newNameString to (myMonth & "-" & myDays & "-" & (year of creationDate) & "_USPS_Label.pdf") as string
			set name of anItem to newNameString
			set anItem to (item newNameString of theFolder) as alias
			
			if exists item newNameString of destinationFolder then
				set extLength to (count characters of (name extension of (info for anItem)))
				set nameWithoutExt to (characters 1 thru -(extLength + 2) of (name of (info for anItem))) as string
				set i to 1
				set nameToCheck to (nameWithoutExt & "-" & my twoDigitsInteger(i) & "." & (name extension of (info for anItem))) as string
				repeat
					if not (exists (item nameToCheck of destinationFolder)) then
						exit repeat
					else
						set i to i + 1
						set nameToCheck to (nameWithoutExt & "-" & my twoDigitsInteger(i) & "." & (name extension of (info for anItem))) as string
					end if
				end repeat
				set newName to (nameWithoutExt & "-" & my twoDigitsInteger(i) & "." & name extension of (info for anItem)) as string
				set name of anItem to newName
				set anItem to (item newName of theFolder) as alias
			end if
			
			set itemToDelete to name of anItem
			move anItem to destinationFolder
			if (exists item itemToDelete of alias (theFolder as string)) then delete (item itemToDelete of theFolder)
		end tell
	end repeat
end renameAndMoveFiles

on twoCharacterDate(aDate, aType)
	set myScript to ("get (" & aType & " of (date (\"" & (aDate as string) & "\"))) as integer") as string
	set myResult to (run script myScript)
	if (count every character of (myResult as string)) is 1 then set myResult to (0 & myResult) as string
	return myResult
end twoCharacterDate

on twoDigitsInteger(anInt)
	set anInt to anInt as string
	if (count every character of anInt) is 1 then set anInt to (0 & anInt) as string
	return anInt
end twoDigitsInteger