I am totally new to Apple Script. I want to create a script that takes several files with the word “copy” and remove the word copy such as “Filename-copy” becomes “Filename”. I am sure this is easy but I don’t know where to start. Thanks
Here is your droplet:
-- TRIM FILE NAMES
-- ©1998 Sal Soghoian, Apple Computer
tell application "Finder"
activate
repeat
display dialog "Text to trim from every file name:" default answer "" buttons {"Cancel", "Trim Start", "Trim End"}
copy the result as list to {the text_to_trim, the button_pressed}
if the text_to_trim is not "" then exit repeat
end repeat
set the character_count to the number of characters of the text_to_trim
set the source_folder to choose folder with prompt "Folder of files to trim:"
if the button_pressed is "Trim Start" then
set the matching_items_list to (every file of the source_folder whose name begins with the text_to_trim) as list
else
set the matching_items_list to (every file of the source_folder whose name ends with the text_to_trim) as list
end if
repeat with this_file in the matching_items_list
set the current_name to name of this_file
if the button_pressed is "Trim Start" then
set the new_name to (characters (the character_count + 1) thru -1 of the current_name) as string
else
set the new_name to (characters 1 thru -(the character_count + 1) of the current_name) as string
end if
my set_file_name(this_file, the new_name)
end repeat
end tell
beep 2
on set_file_name(this_file, new_file_name)
tell application "Finder"
activate
set the parent_container_path to (the container of this_file) as text
if not (exists file (the parent_container_path & new_file_name)) then
try
set the name of this_file to new_file_name
on error the error_message number the error_number
if the error_number is -59 then
set the error_message to "This name contains improper characters, such as a colon (:)."
else --the suggested name is too long
set the error_message to "The name is more than 31 characters long."
end if
beep
display dialog the error_message default answer new_file_name buttons {"Cancel", "Skip", "OK"} default button 3
copy the result as list to {new_file_name, button_pressed}
if the button_pressed is "Skip" then return 0
my set_file_name(this_file, new_file_name)
end try
else --the name already exists
beep
display dialog "This name is already taken, please rename." default answer new_file_name buttons {"Cancel", "Skip", "OK"} default button 3
copy the result as list to {new_file_name, button_pressed}
if the button_pressed is "Skip" then return 0
my set_file_name(this_file, new_file_name)
end if
end tell
end set_file_name
This does what you want: it will ask you if you want to trim off the start or end of the filename; the text to remove; and your files are done in secs!
If you want to go a step further and completely rename the files, try “Filename1.9” @ http://scriptbuilders.net/category.php?id=2120. This folder contains more “droplets”. “Droplets” are scripts that a selection of files can be “dragged onto”. When the files are dragged on the droplet, all files process in the order of ‘the first one you made to the last one’ in these files. They are renumbered starting with the number you give, and named whatever name you give. It’s a way of trimming out unwanted text, and then customizing if you wish.
SiTCoM
I’m going to resurrect this thread.
Is there anyway this can be modified to trim the first four characters off a string of files? I have an entire list of files that came numbered. (001_filename.mp3, 002_filename.mp3, etc…) I’d like to end up with just the file names without the numbers. Any thoughts?
Here’s a very basic version of what you’re asking for to get you started. Note I have the users picking a folder rather than being used as a droplet.
set theFolder to choose folder
tell application "Finder"
try
set thefiles to every file of entire contents of theFolder as alias list
on error
set thefiles to every file of entire contents of theFolder as alias as list
end try
repeat with aFile in thefiles
set fileName to (name of aFile)
set name of aFile to (text 5 thru -1 of fileName)
end repeat
end tell