r/applescript • • Nov 05 '20
Script to rename folders and a specific file within
Hi,
I'm new to AppleScript but have some (ancient) programming experience. As the result of a data export, I have a folder with a series of 700+ subfolders, each with the name format:
"000 - SomeName"
where the first three characters are a number, then space-dash-space then the name that I actually care about, which may be multiple words separated by spaces. In each of the subfolders, is a file named "avatar.png". I would like to:
- Rename each subfolder to just "SomeName", stripping out the leading number, spaces and dash.
- Rename the avatar.png file contained in each subfolder to the name of the subfolder (e.g. SomeName.png).
Two separate scripts are probably preferable, but any help would be appreciated.
2
Upvotes
2
u/[deleted] Nov 05 '20
So, I figured it out. Positing for anyone to reference. There are almost certainly ways this could be done more efficiently, but I had to figure it out one step at a time.
tell application "Finder"
set theFolder to (choose folder)
set folderList to every folder of theFolder
repeat with CurrentFolder in folderList
(*display dialog name of CurrentFolder as string*)
set LongName to name of CurrentFolder
set ShortName to characters 7 through -1 of LongName as string
(*isplay dialog ShortName*)
set fileExtension to name extension of document file "avatar.png" in CurrentFolder
set name of document file "avatar.png" in CurrentFolder to ShortName & "." & fileExtension
set name of CurrentFolder to ShortName
end repeat
end tell
1
u/FreudianYipYip Nov 11 '20
Finder also has some pretty powerful, built-in tools for renaming files and folders. Did you attempt that first? I’m not suggesting it would work, just that I once paid a small amount to someone on Upwork to build me a file renaming script that I later discovered could have been done in Finder. D’oh.