Automator (move finder items)

I have a service that moves finder items to a particular folder. Since I started using this I noticed I occasionally will need to move an item with the same name into this folder. Is there a way I can get the service to move the second item with the same name, but instead of overwriting the first item, have it add a -1 or something? If I open something like a zip file multiple times in finder, it creates the second one with a 1 after the name. That's exactly what I need. Any way to do that with a service like this?

Macbook Pro, Mac OS X (10.6.1)

Posted on Dec 31, 2009 10:57 AM

Reply
19 replies

Dec 31, 2009 4:03 PM in response to sterlingfive

you can do it but you'll have to use "run shell script" or "run applescript" Automator actions for that. for example. the following "run applescript" action will copy contents of folder 1 on the desktop to folder 2 on the desktop and if any item in the target folder already exists its name will be prepended with 1- (that's slightly easier to script that putting it at the end of the name). you can adjust this in many ways as needed.
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #ADD8E6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run {input, parameters}
tell application "Finder"
set source_folder to folder "1" of (path to desktop)
set target_folder to folder "2" of (path to desktop)

repeat with this_item in source_folder

set curname to ((name of this_item) as text)

if exists item curname of target_folder then
set name of item curname of target_folder to "1-" & curname
end if
duplicate this_item to target_folder

end repeat
end tell
end run</pre>

Jan 1, 2010 11:36 AM in response to V.K.

V.K.

I think this is along the lines of what I need, except I really do need the number to go after the name, because these files go into the folder in chronological order by naming the document with the date created as the first part of the name. Adding the 1 before the name, would throw off the whole order of things.

I worked with the script you gave me, and I couldn't even get it to work because I didn't know exactly which items I needed to modify. I'll do a little reading up on applescript to see if I can figure this out. If it's going to be much more complicated to add the number after, I very well may not be able to do this. Thanks for the tips though, you gave me something to work with.

Happy new year

Jan 1, 2010 11:54 AM in response to sterlingfive

use this then
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #ADD8E6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run {input, parameters}
tell application "Finder"
set source_folder to folder "1" of (path to desktop)
set target_folder to folder "2" of (path to desktop)

repeat with this_item in source_folder

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)
set name of this_item to current_name_minus_extension & "-1." & name extension of this_item --add -1 at the end of the name before the extension
end if
duplicate this_item to target_folder

end repeat
end tell
end run</pre>

Jan 1, 2010 1:02 PM in response to V.K.

I'm looking all over applescript tutorials and examples, and I can't find anything that looks really close to this to sort of go off of. I keep trying different paths and folder names in the places I think I'm supposed to be putting them in, and it isn't working! Obviously, I'm doing something wrong, probably many things wrong. 😟

Would it be possible for you to post a script with similar commands so I can see how this should look after I plug in my paths and whatever else i need to change? I just don't have any experience with applescript, so I'm not sure which items I'm supposed to be changing.

Thanks again

Jan 1, 2010 1:32 PM in response to V.K.

I still can't figure it out, but it's okay. The solution is just a little more complex than what I thought the problem would need. Thank you for putting up both of these scripts for me, and for making the comments in the second one. Unfortunately, since I'm not familiar with applescript, I can't tell which parts are your comments that need to be deleted, and which parts need to stay or be modified, like parenthesis and other things in the script. I can just continue to name and move these files manually, I've been doing it for weeks already anyway. If anything, I can just uncheck the "overwrite files with same name" box and add the -1 manually if the file doesn't move because of a same named file already in the target.

If you say the script works, I believe you. I'll go ahead and mark the question as answered so if someone else comes along with the same questions, they can at least see the solution is here. I'll read up on applescript when I have more time and revisit this after I understand it a little more.

thanks again,
sterlingfive

Message was edited by: sterlingfive

Jan 2, 2010 12:54 PM in response to sterlingfive

So, I have been fiddling with this, and I found why I couldn't get it to work. The script does indeed work the way it is, by simply filling in the names of the source and target folders. My problem came in because I am moving the file from the desktop. I have tried all sorts of combinations, and nothing works to set the source as the desktop. Is there a major change that needs to be made to set desktop as the source? Also, I was confused because I kept filling in (path to desktop) as (Users/usernam/desktop) I thought that was a note to fill in the path. I didn't know that I was suppose to leave that the way it was! So, it took a couple days, but I understand now. Oh, also, this script seems to be copying the file to, not moving it to the target. Do I just need to add a delete command for the original file after the copy to is done?

Thanks again,
sterling

Jan 2, 2010 1:02 PM in response to sterlingfive

One more thing I forgot. I couldn't figure out how to use a target folder that's not on the desktop, does that take a big change too?

Sorry for the beginner questions. The more I read about scripting, the more I realize I need to learn how to do this. So, I am ordering a book about applescript and am starting to find online sources to learn more about scripting. Hopefully, in a little time I'll be plugging these scripts together on my own!!! In the meantime, thanks for the help.

Jan 2, 2010 4:42 PM in response to sterlingfive

sorry, i thought you gave up on this thread. please clarify what is the exact situation and we'll adjust the script accordingly. do you want to use the script on some fixed folder? on all files in that folder? only on selected files?

also, i realize that the script will need adjustment anyway if you use it more than once. for example if a file 15.jpg exists in the target folder and we try to move another one named 15.jpg we want to rename the file we move to 15-1,jpg. but if we need to do this again then we'll have to rename it to 15-2.jpg to avoid name conflicts. all of this can be easily handled. but I need to know what's the exact setup. to access the desktop folder you simply say

path to desktop


so file 15.jpg sitting on the desktop would be

item "15.jpg" of (path to desktop)


if you want to use the unix path you'd say

POSIX file "/users/username/desktop/15.jpg" as alias

Jan 2, 2010 6:06 PM in response to V.K.

No, I didn't give up on the thread, I just went to work with what you gave me to see what I could do with it. It's just that you've been nothing but extremely helpful and patient with me in the past; and I started to feel like I was coming to the board, putting up my problems, and you do everything for me. I know you're here to contribute to the community, but I don't like to take advantage. I am making an effort to learn this stuff now, I just have to make baby steps!

Okay, here is the deal with this item. I actually want to apply this with more than one operation, but the basic script or concept would remain pretty much the same. We create lots of documents like invoices, receipts, shipping labels, etc., that usually get saved as pdf onto the desktop. Then, they are named, often with the date and particular file name and finally moved into the proper folders. For example, 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 USPSLabel.pdf

Then, I would move it to the folder where we keep the shipping labels for records.

So, the service I created actually does more than just moving the file. It goes like this:
service receives selected pdf
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)
move finder items (to target folder)
open finder item (with default application)

The other ones I need would be similar, except they don't usually need to be opened afterward, just named and moved, for the most part. I figured I could continue to just use the name change function of automator before running the script, but if would run faster or more efficiently if that were also part of the script, I suppose that would be better. And I would like to be able to have the numbers go up through as many as I need. If it's a matter of just adding in all the possibilities of existing names up to the number I need, that's tedious work I don't mind doing now to save on having to name all these files in the future!

Also, these files are always named differently, so the source file would need to just be selected file, not the actual name.

Finally, I tried to play with the script to name the desktop like you said, but would it be something like
set source_folder to folder (path to desktop)
or
set source_folder to (path to desktop)

I still need to learn a little more before I understand the way that works. I am so used to only using the unix path, I like the ability to use (path to desktop) that's really interesting.

I think I may have rambled a bit, but that should explain the details of what I'm trying to accomplish.

thanks

Jan 2, 2010 6:47 PM in response to sterlingfive

ok then. then make your service as follows

1. rename finder items
keep it as you have set it up already
2. run applescript
<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 335px;
color: #000000;
background-color: #ADD8E6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run {input, parameters}

tell application "Finder"

set target_folder to folder "2" of (path to desktop)

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</pre>

the only thing you need to change in that script is the path to the target folder where the items are being moved to.

Jan 2, 2010 7:31 PM in response to V.K.

Yes, that seems to do exactly what I wanted. I want to try and dissect the script to figure out how it works now! Or maybe I should stick with the basics for just a little while. HAHA!

At least I can use this now! I really wanted to start off the new year with a little better record keeping, and this is precisely what I needed for this task! What a time saver.

Thanks V.K.
sterling

Jan 2, 2010 7:41 PM in response to sterlingfive

sterlingfive wrote:
Yes, that seems to do exactly what I wanted. I want to try and dissect the script to figure out how it works now!

of course. i put a few comments in the script just for that purpose.
Or maybe I should stick with the basics for just a little while. HAHA!

At least I can use this now! I really wanted to start off the new year with a little better record keeping, and this is precisely what I needed for this task! What a time saver.

Thanks V.K.
sterling

you are welcome!

Jan 2, 2010 7:56 PM in response to sterlingfive

Oh, one more thing. Now that I'm getting this going....

How do I change the target destination if its not within the desktop? I know you said,


to access the desktop folder you simply say

path to desktop




if you want to use the unix path you'd say

POSIX file "/users/username/desktop/15.jpg" as alias


but what would the line need to say if it was somewhere within the hard drive inside a hierarchy of folders?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator (move finder items)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.