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

Apple script automating renaming folder names

Hi there,


I am a beginner at using Applescript but i'm slowly learning the new syntax and structures from what I am used to in VB.net (which I used at high school)


I have already found a lot of help on this forum from the user "Camelot" where they answered a lot of my questions but I just cant manage to tie up my program to be completely finished and would be more than grateful to gain some help from the Apple Community.


My program is to rename image files.


I start with a folder (Lots) containing folders, like the following:

(1,2,3,4,5...900,)


In each of these folders is a group of images named as follows:

(Img_xxx1.jpg, Img_xxx2.jpg, Img_xxx3.jpg, Main.jpg)


The goal outcome of this little program is to rename Main.jpg to the name of the folder it is in and the rest of the images to the name of the folder with the _x.jpg suffix.


So in the first folder (1) inside the folder will look like the following:

(1.jpg, 1_1.jpg, 1_2.jpg, 1_3.jpg)


and for example inside folder (564) will look like the following:

(564.jpg, 564_1.jpg, 564_2.jpg, 54_3.jpg)


This is what I currently have:


on run

set Thefolder to (choose folderwith prompt "select start folder")


RenameContents(Thefolder)

end run


on RenameContents(aFolder)

tell application "Finder"

set subFolders to every folder of aFolder

repeat with eachFolder in subFolders


end repeat

end tell


end RenameContents


Thank you for any help in advance.

MacBook Pro (13-inch Mid 2012), macOS High Sierra (10.13.1), AppleScript

Posted on Nov 16, 2017 5:12 AM

Reply
Question marked as Best reply

Posted on Dec 11, 2017 3:45 PM

on run
  set RootFolder to POSIX file "/Top/Level/Folder/Path" as alias

  -- If the folder specified above is empty, will assume it to
  -- be a test folder and will proceed to create test files and
  -- folders that mirror the structure:
  -- Img_{I}.jpg in folder {I}
  -- Main.jpg in every folder
  tell application "Finder" to get (number of items of folder RootFolder)
  if result is 0 then
       CreateTestFolders at POSIX path of RootFolder
       return display notification ¬
       "Files and folders created.\nNow re-run program." with title (POSIX path of RootFolder)
  end if

  -- The text we'll be replacing in the filename
  set AppleScript's text item delimiters to "Img"

  -- Progress indicator
  setProgress at 0 out of 0 given info:"Starting…"

  tell application "System Events"
       set _Folders to a reference to the folders in the RootFolder
       set _files to a reference to the files in _Folders

       get number of contents of _files
       set N to result

       set i to 0
       setProgress of me at i out of N given info:"Gathering files…"

       repeat with _F in _Folders as list -- Each folder named "1", "2", etc.
            set FolderName to name of _F     
            get items of _F 
    
            repeat with _jpeg in result -- Each item in folder
                 set i to i + 1

                 -- Update progress
                 setProgress of me at i out of N given info:[i, " of ", N]

                 set Filename to name of _jpeg

                 tell Filename to ¬
                      if it ends with ".jpg" or it ends with ".jpeg" then
                         if it starts with "Main" then
                              text items in [FolderName, ".jpg"]
                         else if it starts with "Img_" then
                              text items in [FolderName, its last text item]
                         else
                              false
                         end if
                      else
                         false
                 end if

                 if result is not false then set name of _jpeg to result as text
            end repeat
       end repeat
  end tell

  setProgress of me at 0 out of 0 given title:"", info:""
end run


-- Progress indication
on setProgress at steps as integer out of total as integer given title:t as string : "Processing Image Files…", info:i as string : ""
  local steps, total, t, i

  set progress completed steps to steps
  set progress total steps to total
  set progress description to t
  set progress additional description to i
end setProgress


-- Test folders and files
on CreateTestFolders at TestPath : "/Users/Haydon/Desktop/untitled folder"
  tell application "System Events" to ¬
       repeat with i from 1 to (random number 30)
            set F to (make new folder in folder TestPath ¬
                 with properties {name:i as text})     
            make new file at F with properties {name:"Main.jpg"}

            repeat with j from 1 to random number 30     
                 make new file in F with properties ¬
                      {name:"Img_" & j & ".jpg"}
            end repeat
       end repeat
end CreateTestFolders
5 replies
Question marked as Best reply

Dec 11, 2017 3:45 PM in response to haydon106

on run
  set RootFolder to POSIX file "/Top/Level/Folder/Path" as alias

  -- If the folder specified above is empty, will assume it to
  -- be a test folder and will proceed to create test files and
  -- folders that mirror the structure:
  -- Img_{I}.jpg in folder {I}
  -- Main.jpg in every folder
  tell application "Finder" to get (number of items of folder RootFolder)
  if result is 0 then
       CreateTestFolders at POSIX path of RootFolder
       return display notification ¬
       "Files and folders created.\nNow re-run program." with title (POSIX path of RootFolder)
  end if

  -- The text we'll be replacing in the filename
  set AppleScript's text item delimiters to "Img"

  -- Progress indicator
  setProgress at 0 out of 0 given info:"Starting…"

  tell application "System Events"
       set _Folders to a reference to the folders in the RootFolder
       set _files to a reference to the files in _Folders

       get number of contents of _files
       set N to result

       set i to 0
       setProgress of me at i out of N given info:"Gathering files…"

       repeat with _F in _Folders as list -- Each folder named "1", "2", etc.
            set FolderName to name of _F     
            get items of _F 
    
            repeat with _jpeg in result -- Each item in folder
                 set i to i + 1

                 -- Update progress
                 setProgress of me at i out of N given info:[i, " of ", N]

                 set Filename to name of _jpeg

                 tell Filename to ¬
                      if it ends with ".jpg" or it ends with ".jpeg" then
                         if it starts with "Main" then
                              text items in [FolderName, ".jpg"]
                         else if it starts with "Img_" then
                              text items in [FolderName, its last text item]
                         else
                              false
                         end if
                      else
                         false
                 end if

                 if result is not false then set name of _jpeg to result as text
            end repeat
       end repeat
  end tell

  setProgress of me at 0 out of 0 given title:"", info:""
end run


-- Progress indication
on setProgress at steps as integer out of total as integer given title:t as string : "Processing Image Files…", info:i as string : ""
  local steps, total, t, i

  set progress completed steps to steps
  set progress total steps to total
  set progress description to t
  set progress additional description to i
end setProgress


-- Test folders and files
on CreateTestFolders at TestPath : "/Users/Haydon/Desktop/untitled folder"
  tell application "System Events" to ¬
       repeat with i from 1 to (random number 30)
            set F to (make new folder in folder TestPath ¬
                 with properties {name:i as text})     
            make new file at F with properties {name:"Main.jpg"}

            repeat with j from 1 to random number 30     
                 make new file in F with properties ¬
                      {name:"Img_" & j & ".jpg"}
            end repeat
       end repeat
end CreateTestFolders

Dec 11, 2017 4:08 PM in response to _CJK

The script above renames a file from something like "Img_1234.jpg" in folder "45" to "45_1234.jpg", and "Main.jpg" to "45.jpg". I assume that's what you wanted. Just change line 3 and replace "/Top/Level/Folder/Path" with the path to the folder that contains the folders "1", "2", "3", etc. (e.g."/Users/Haydon/Pictures/This Folder").


Firstly, sorry for the crappy code formatting. The text editor on this website is so rubbish and it ended up being the least worst option.


Secondly, it's not as scary as it looks, once you paste it into Script Editor.

Firstly, sorry for the crappy code formatting. The text editor on this website is so rubbish and it ended up being the least worst option.


Secondly, it's not as scary as it looks, once you paste it intoScript Editor. The two functions at the bottom are largely superfluous to the overall running of the script:setProgressjust updates the progress indicator at the bottom of theScript Editorwindow; andCreateTestFolderswas just a function I used to create some test folders to ensure the script worked properly.


The main core of the script is the chunk of code in thetellblock between lines 23 and 61. It's doing precisely what you were going to do, which is to cycle through each folder, then cycle through each image in each folder, and rename the image accordingly.


I useSystem Eventsrather thanFinderbecause it's generally faster and less likely to crash on you.


You'll notice I use references to folders and files, rather than assigning them directly to variables (lines 24-25). This is because it sounds like there will be a lot of images in a lot of folders, and if you try and getScript Editorto store all of that in memory, it'll just crash or freeze.


In fact, even doing it this way, with a sample of 50 folders each containing 50 files, Script Editor had a bit of a tough time, but managed to get through it successfully.

Apple script automating renaming folder names

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