I know I am going to get laughed at for this, but even though I wrote 20 or 30 scripts that worked quite perfectly in OS 9, I haven’t written any scripts in almost 2 years and now I find that I just cannot get any script I write in OS X to work. I am thinking that I missed some particular detail about how files and directories work in OS X differently than they did in OS 9. But that is just a guess. Even my most basic scripts don’t work.
What I need to do now is solve a problem quickly. I have no starting script at this point, but will be trying to code something in the next couple of days. I am trying to back up a folder on my Mac to a folder on the server. Unfortunately, the server does not like some of my file names. It says they are too long. (I hate this server!!)
Anyway, I need to come up with a script to select the folder to backup, recursively go through that folder and all subfolders, check the length of all file names, and if the name is longer than 32 characters shorten the name. But I HAVE TO KEEP the first 17 characters and whatever comes after the dot to identify the file type. Some of those are three characters like .eps and others are four like .indd.
Any help or good piece of starting code would be MOST appreciated. If this is just too much to ask I understand.
A few things have changed since OS 9, especially with regard to scripting the Finder. Some terms have changed, long references usually have to be broken up, some functions aren’t implemented yet, some actions are subcontracted to System Events and thus may or may not return the correct result first time. However, things are still the same in that you never know what’s going to work from version to version.
Here’s some starting code for you:
set origFolder to (choose folder)
checkNames(origFolder)
tell application "Finder" to duplicate origFolder to disk "myServer"
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
A few things have changed since OS 9, especially with regard to scripting the Finder. Some terms have changed, long references usually have to be broken up, some functions aren’t implemented yet, some actions are subcontracted to System Events and thus may or may not return the correct result first time. However, things are still the same in that you never know what’s going to work from version to version.
Here’s some starting code for you:
[END QUOTE]
Hi Nigel,
Thank you for your help with this piece of code. I have not yet gotten it to work. You are so very right about never knowing what is going to work. But I really want to get back into coding. It was just so much fun. (And, yes, I am somewhat masochistic.)
I am still working on this code. I will let you know what I end up with if I get it working. And I am sorry for taking so long to thank you.
Dont’ know where this stands, but I got it to at least get through the files copying… whether it truncates the files to 32 characters yet I haven’t figured it out, due to inconsistent/illegal filenames.
The main offender is that I have many files supplied to me with a “/” in them. Not a problem for an OSX only disk, but a problem for OS 9 driver disks.
So… I was wondering, anyone know how to change the “/” to a “-” in this thing, mid operation?