Hi,
I had not thought of that, but the first script will not work if the folder name of have already a space at the beginning.
Example: if you add a new folder in the subfolder " A", run the script, the new folder will not be renamed because the script is recursive on folders whose name does not begins with a space, which is not the case for the folder " A".
Therefore, the script must be recursive on all folders, but it must checks each folder name and rename it if the name does not begin with a space.
BGmail wrote:
Hey Jacques,
Thanks very much for the script. One question. Aside from renaming the sub folders how can the script rename the parent folder too.
And how would I be able to rename a folder that OS X will not let me rename due to permissions. The account I am using is and Admin account.
...Bruce
Here is the modified script, and I added a shell command to rename the directories (if the Finder was not able to rename these folders), this shell requires an administrator password.
-----------------------
propertyrecursive : true -- recursively descends the directory tree.
-- Important ** set it to false to rename subFolders in the choosen folder only **
setmasterFoldertochoose folder
set err to ""
tellapplication "Finder"
setfNametonameofmasterFolder
iffNamedoes notstart with " " thentry
set name of masterFolder to " " & fName -- rename the parent folder
onerror
seterrtoPOSIX pathofmasterFolder
endtry
setrtomyrenameSubFolders((foldersofmasterFolder) asalias list)
endtell
iferrisnot "" orrisnot {} then -- if the Finder failed to rename one or more folders
if r is not {} then
setoTidtotext item delimiters
settext item delimitersto (characterid 0)
iferrisnot "" thensetendofrtoerr -- the master folder has not been renamed, add it to list
seterrtorasstring -- convert list of folders path to string separated by null character
settext item delimiterstooTid
end if
-- ** write folders path to the temporary file **
setWriteSucceededtofalse
try
set fPath to (path to desktop folder as text) & "__tempFile__.zxy"
setftoopen for accessfilefPathwithwrite permission
set eof f to 0
write (err & characterid 0) starting ateofas «class utf8» tof
setWriteSucceededtotrue
close accessf
onerror
try
close accessfilefPath
end try
endtry
ifWriteSucceededthen
-- rename folders with a shell (A dialog will appear asking for the administrator name and password).
settempfiletoquoted formofPOSIX pathoffPath
do shell script "while read -d $'\\0' f;do d=${f%/*}; name=${d##*/}; /bin/mv -n \"$f\" \"${f%/*/*}/ $name/\"; done<" & tempfile with administrator privileges
do shell script "/bin/rm " & tempfile & " > /dev/null 2>&1 &" -- remove the temporary file
end if
endif
onrenameSubFolders(thesefolders)
scripto
property dirToRename : {}
endscript
tell application "Finder"
repeatwithiinthesefolders
if recursive then
tell ((folders of i) as alias list) to if it is not {} then
tell my renameSubFolders(it) to if it is not {} then
set end of o's dirToRename to it
end if
end if
end if
set fName to name of i
iffNamedoes notstart with " " thentry
set name of i to " " & fName
onerror -- permissions or others
set end of o's dirToRename to (POSIX path of i)
end try
end repeat
endtell
returno's dirToRename -- returns folders path that renaming has failed with the Finder.
endrenameSubFolders
-----------------------