Q: Filenames... Truncation to 32chars. and Replacing TroublesomeText
Q: Filenames... Truncation to 32chars. and Replacing TroublesomeText
- Subject: Q: Filenames... Truncation to 32chars. and Replacing TroublesomeText
- From: Wade Cady <email@hidden>
- Date: Thu, 21 Jul 2005 11:23:45 -0500
Greetings.
====== Serious Newbie Alert======
I lurk here, hoping that it will sink in one day, and have recently
decided to jump into learning how to script. I borrowed some code for
my first one, and made some alterations, and it worked, so now I'm just
savvy enough to be irritating and dangerous with it.
That said.
Problem:
I have a 120 GB volume that is a rudimentary fileserver which runs on a
mac with Panther. I have another disk, recently purchased, that I want
to serve those files until we get a grasp on future file serving
possibilities and solutions. Thing is, this mac is running OS 9, due to
an archiving system that isn't broken I don't want to upgrade it ...
yet. The actual issue is... I want to copy all the files from disk one
(OS X) to disk two (OS 9.2.2 I believe).... but some file's filenames
on the original volume are more than 32 characters.
Now. I have found a script on http://www.macscripter.net that Solves
this.
<Applescript>
set origFolder to (choose folder)
checkNames(origFolder)
tell application "Finder" to duplicate origFolder to disk "mydisk"
on checkNames(theFolder)
set maxLen to 32
tell application "Finder"
set fileNames to name of every file of theFolder
-- Rename any files with more than 32 characters in their names.
repeat with thisName in fileNames
if (thisName's length > maxLen) then
set name of file thisName of theFolder to my truncateName(thisName,
maxLen)
end if
end repeat
-- Recurse through the subfolders, renaming files if necessary.
set subfolders to theFolder's folders
repeat with thisSubfolder in subfolders
my checkNames(thisSubfolder)
end repeat
-- Finally, rename this folder if necessary.
set folderName to theFolder's name
if ((count folderName) > maxLen) then
set theFolder's name to my truncateName(folderName, maxLen)
end if
end tell
end checkNames
on truncateName(thisName, maxLen)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
if ((count thisName's text item 1) = (count thisName)) then
set newName to text 1 thru maxLen of thisName
else
set nameExtn to "." & thisName's text item -1
set newName to text 1 thru (maxLen - (count nameExtn)) of thisName &
nameExtn
end if
set AppleScript's text item delimiters to astid
return newName
end truncateName
</Applescript>
This works well, so far as I have tested. BUT. when it encounters
certain illegal characters (The one I know for sure is a "/"... hey, I
didn't name these files!) it errors and stops the operation of renaming
and copying the files to a new volume.
So now I am trying to find a script that finds and replaces the text of
my choice from file/folder names. There is such a script in the "Script
Menu" under OS X panther, BUT..... it will not replace text in
filenames for files and folders nested within the open folder.
Here is that script.
<Applescript>
(*
Replace Text In Item Names
Copyright © 2001 Apple Computer, Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
--set the source_folder to choose folder with prompt "Folder containing
items to edit:"
-- get the path to the folder of the front window
-- if no windows are open, the desktop folder will be used
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
display dialog "Search and replace in:" buttons {"File Names", "Folder
Names", "Both"} default button 3
set the search_parameter to the button returned of the result
repeat
display dialog "Enter text to find in the item names:" 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 "Enter replacement text:" 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
display dialog "Replace “" & the search_string & "” with “" & the
replacement_string & "” in every item name?" buttons {"Cancel", "OK"}
default button 2
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 search_parameter is "Folder Names" and ¬
folder of this_info is true then
set the change_flag to true
else if the search_parameter is "File Names" and ¬
folder of this_info is false then
set the change_flag to true
else if the search_parameter is "Both" 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_item_list to every text item of the current_name
set AppleScript's text item delimiters to the replacement_string
set the new_item_name to the text_item_list as string
set AppleScript's text item delimiters to ""
my set_item_name(this_item, new_item_name)
end if
end if
end repeat
beep 2
on set_item_name(this_item, new_item_name)
tell application "Finder"
--activate
set the parent_container_path to (the container of this_item) as text
if not (exists item (the parent_container_path & new_item_name)) then
try
set the name of this_item to new_item_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 error_message -- "The name is more than
31 characters long."
end if
--beep
tell me to display dialog the error_message default answer
new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is "Skip" then return 0
my set_item_name(this_item, new_item_name)
end try
else --the name already exists
--beep
tell me to display dialog "This name is already taken, please
rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"}
default button 3
copy the result as list to {new_item_name, button_pressed}
if the button_pressed is "Skip" then return 0
my set_item_name(this_item, new_item_name)
end if
end tell
end set_item_name
</Applescript>
Ok... So...
What I am wondering is....
Can anyone help me understand how to parse the part of the first script
that enables it do "act upon" all the files within a folder (nested
files/folders included rather than just files and folders in the open
directory) as the first script is able to do, into the second "replace
filename text" script so that I don't have to open 115 GB worth of
folders to find the files with the known offending characters. OR...
give me a clue how to alter the first script so that it replaces
erroneous characters WHILE truncating the filenames.
Perhaps I am asking too much, but seems plausible... and I hope I have
provided enough information and footwork to entice someone into shoving
me in the right direction. Thanks for your time if you've read all
this. I intend my next question won't be so .... whatever this one is.
Wade
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden