Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator Rename Finder Items NOT working in Leopard

I used to be able to Replace Text in filenames, as you would expect with Automator, now my old or new Automator actions don't work for this, whether I run them as a Finder plugin or from the Automator Run button...

Anyone else have this problem, or better yet success?

Macbook Pro, Mac OS X (10.5)

Posted on Nov 8, 2007 3:43 PM

Reply
Question marked as Best reply

Posted on Aug 3, 2017 1:49 AM

Your problem, hardware or software, may not be exactly the same as that of the original poster of this older thread, and it can be very confusing for everybody if we try to answer more than one question in each thread, which can also result in you applying the wrong advice to your particular problem.


In order for us to give your problem our proper attention to try to solve it, would you kindly start your own thread, describing the trouble you are having in the fullest detail, including completing your details to show what Mac or iDevice you are using, what operating system, and what version of the application in question. Please remember to post in the forum relevant to your hardware or version of OS X. A full list of all the support forums is here:


http://discussions.apple.com/index.jspa

42 replies

Nov 16, 2007 7:10 AM in response to Fumi

Another option is to activate the Scripts menu for the menubar. (/System/Library/Core Services/Menu Extras/Script Menu.menu)

Once set up you can use Finder Scripts/Replace text in item names.

I have used the Rename Finder Finder Items action with Move Finder Items in Tiger. Now I am back to the old Panther way of using the script above with Quick Access CM plugin and the Move To... option. In some ways it is more user friendly and has just made Automator a redundant feature in Mac OS X and has taken a little of the shine off of Leopard on moving from Tiger.

Nov 16, 2007 8:47 AM in response to OS Lucinity

*Here's a slight variation to the Script to ONLY replace filenames, and I removed the final confirmation dialogue popup. You might name this 'Replace Filenames'. For some reason, if you run this script in Automator it doesn't work, but running it from the Finder Scripts menubar icon works*

try
tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
set the source_folder to path to desktop folder as alias
end try

repeat
display dialog "Find and Replace:" default answer "" buttons {"Cancel", "OK"} default button 2
set the search_string to the text returned of the result
if the search_string is not "" then exit repeat
end repeat

repeat
display dialog "Replace With:" default answer "" buttons {"Cancel", "OK"} default button 2
set the replacement_string to the text returned of the result
if the replacement_string contains ":" then
beep
display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} >default button 2
else if the replacement_string contains "/" then
beep
display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", >"OK"} default button 2
else
exit repeat
end if
end repeat


set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info
set change_flag to false
if the current_name contains the search_string then
if the folder of this_info is false then
set the change_flag to true
end if
if the change_flag is true then
-- replace target string using delimiters
set AppleScript's text item delimiters to the search_string
set the text itemlist to every text item of the current_name
set AppleScript's text item delimiters to the replacement_string
set the new itemname to the text itemlist as string
set AppleScript's text item delimiters to ""
my set item_name(thisitem, new itemname)
end if
end if
end repeat

beep 2

on set item_name(thisitem, new itemname)
tell application "Finder"
--activate
set the parent containerpath to (the container of this_item) as text
if not (exists item (the parent containerpath & new itemname)) then
try
set the name of this_item to new itemname
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 error_message -- "The name is more than 31 >characters long."
end if
--beep
tell me to display dialog the error_message default answer new itemname >buttons {"Cancel", "Skip", "OK"} default button 3
copy the result as list to {new itemname, button_pressed}
if the button_pressed is "Skip" then return 0
my set item_name(thisitem, new itemname)
end try
else --the name already exists
--beep
tell me to display dialog "This name is already taken, please rename." default answer >new itemname buttons {"Cancel", "Skip", "OK"} default button 3
copy the result as list to {new itemname, button_pressed}
if the button_pressed is "Skip" then return 0
my set item_name(thisitem, new itemname)
end if
end tell
end set itemname

Dec 4, 2007 2:49 AM in response to Nikolas Zane

I prepared an alternative workflow combining Ask for text, Get selected item and Run shell script, that allows me to rename files as I did before, when Rename items was working.

1. Ask for text prompt the user to know the prefix to be appended to the file name
2. Get selected items brings the files selected in finder window
3. Run shell script is configured as
Shell: /usr/bin/perl
Pass input: as arguments
and contains the following perl code

use File::Basename;
# first parameter is the prefix to be appended
$prefix = (shift @ARGV);
### system("say appending $prefix");
## the rest of the parameters are the files to be renamed
foreach $f (@ARGV) {
## filenames are passed as absolute, split into basename and dir
$file = basename($f);
$dir = dirname($f);
## uncomment next line to listen the names of the files processed ...
## system("say now processing file $file");
## build new filename; to append at the end just postpone $prefix ...
$newfile = $dir."/".$prefix.$file;
## that's it, invoke rename command from underlying OS
system("mv $f $newfile");
}
## end of work, confirm everything has been done
system("say ok\, files renamed!");
## the exit
exit;


In case you want to have the name appended at the end of the filename, just change the line
$newfile = $dir."/".$prefix.$file;
to
$newfile = $dir."/".$file.$prefix;

Save the workflow Finder plug in and that's it.
Hope it helps.

Dec 4, 2007 3:01 AM in response to marcodpisa

Sorry, the cut&paste of the perl code did not work last time, here is the correct script to be used. (without comments, that look like to be misinterpreted ...)

use File::Basename;
$prefix = (shift @ARGV);
foreach $f (@ARGV) {
$file = basename($f);
$dir = dirname($f);
$newfile = $dir."/".$prefix.$file;
system("mv $f $newfile");
}
system("say ok\, files renamed!");
exit;

Dec 5, 2007 2:29 AM in response to Stephen Vaughan

I also "upgraded" to Leopard on my Mac Pro at work, and soon discovered that basic everyday functions such as the Rename Automator Action no longer functioned. Because this has broken my workflow, Leopard has sent me back to a time before Automator, where we batch-renamed files manually or were forced to write AppleScripts (like in Panther). This is the real Time Machine.

I fully expected 10.5.1 to not fix bugs such as this (a ".1" OS release is just for fixing critical bugs like data loss) but hopefully 10.5.2 will fix bugs like this one. Apple, please click the Forward button, not Backward.

Dec 6, 2007 6:04 AM in response to marcodpisa

To marcodpisa:

May I know how do you combine prefix to be prepended as first argument and paths of selected files as subsequent arguments in for the Perl script to process in Automator application ? I could not find any way to accomplish the required list of arguments in Automator.

Thank you for the perl script. Following is a revised version of your perl script which eliminate the costly system call for renaming file with Unix's mv command. This perl script has been tested in Terminal application of Mac OS 10.5.1 and proved to be working.

#! /usr/bin/perl
### This perl script prepend file name with prefix passed as first argument in command line.

### Required Perl library modules.
use File::Basename;

### First argument is the prefix to be prepended to following arguments passed as file names.
$prefix = (shift @ARGV);


### Loop through every subsequent argument passed as file name to be modified.
foreach $path (@ARGV) {

$file = basename($path);
$dir = dirname($path);

### Format of new file name
$newname = $dir."/".$prefix.$file;

rename $path, $newname || die "Failed to rename file: $path with error: $!\n";
}

exit;

Jan 22, 2008 12:48 PM in response to OS Lucinity

Wow, that's shocking: http://www.automator.us/leopard/downloads/index.html

* Automator Text Input Bug
There is a known issue with Automator 2.0 actions not accepting text input when they are set to display when the workflow is run.

There is no work-around for this problem other than to run the workflow containing the effected actions, opened in Automator with their Show When Run option disabled.

Actions listed on this page that are effected by this issue are marked with an asterisk (*).

Automator Rename Finder Items NOT working in Leopard

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