• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Renamer script (CafeTran Wiki)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Renamer script (CafeTran Wiki)


  • Subject: Re: Renamer script (CafeTran Wiki)
  • From: Michael Schwarz <email@hidden>
  • Date: Fri, 08 Jan 2016 18:25:10 +0100

> Fri, 08 Jan 2016 16:43:33 +1100
> I have a folder with 450 subfolders each containing one or more PNG files:
> I want to rename the PNG files by adding the subfolder (plus a dash) to the file name

An example for a script to rename the files dropped on an applet or the files in a chosen folder is as follows.

property startStr : "001"
property nfSep : "-"

on run
try
set inputFolder to choose folder with prompt "Please select the folder containing the files to be numbered."
on error
return
end try
set nameStr to GetFolderName(inputFolder as text)
tell application "Finder"
set inputFiles to every file of inputFolder
end tell
RenameFiles(nameStr, inputFiles)
end run

on open fileList
set firstFileStr to (item 1 of fileList as text)
set nameStr to GetFolderName(firstFileStr)
RenameFiles(nameStr, fileList)
end open

on GetFolderName(theString)
set {oldTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set strList to (text items of theString)
set nameString to (text item -2 of strList)
set AppleScript's text item delimiters to oldTIDs
return nameString
end GetFolderName

to RenameFiles(nfName, theFileList)
set nfNum to (text returned of (display dialog "Please enter the start number with all leading zeros." default answer startStr))
if nfNum is not equal to startStr then
set startStr to nfNum
end if
set startNumLength to (count of startStr)
try -- convert the starting value string into a number
set startNum to (startStr as integer)
on error -- the starting value string contained a non-number
display dialog "The value " & startStr & " is not a valid number." & return & "Please enter numbers only!" with icon stop buttons {"Sorry"} default button 1
end try
set zeroMask to ""
repeat startNumLength times
set zeroMask to (zeroMask & "0")
end repeat
set increment to 0
repeat with i from 1 to (count of theFileList)
set nf to (item i of theFileList)
tell application "Finder" to set fex to name extension of nf
set nfNum to (startNum + increment)
set nnStr to zeroMask & nfNum
set nextName to nfName & nfSep & (text -1 thru -(startNumLength) of nnStr)
tell application "Finder" to set the name of nf to (nextName & "." & fex)
set increment to (increment + 1)
end repeat
end RenameFiles

It would have to be expanded a bit for recursive renaming, though.

Best regards, Michael

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Renamer script (CafeTran Wiki)
      • From: Christopher Stone <email@hidden>
  • Prev by Date: Re: [ANN] CalendarLib, improved Calendar scripting
  • Next by Date: Re: [ANN] CalendarLib, improved Calendar scripting
  • Previous by thread: Re: standard library
  • Next by thread: Re: Renamer script (CafeTran Wiki)
  • Index(es):
    • Date
    • Thread