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