Help needed with progress bar

Hi Scripters,

I need your help concerning a script written in AppleScript. The purpose of this script is pretty simple:

  1. When launched it asks to choose .otf or .ttf fonts
  2. When the folder containing the fonts is chosen it starts to rename the fonts according to their metadata name.

My problem is I don’t know how to show a progress bar with the number of fonts being processed.

Thanks for your help!

set theResponse to display dialog "TrueType (ttf) or OpenType (otf)" buttons {".ttf", ".otf"}
set buttonName to button returned of theResponse

set theSourceFolder to (choose folder with prompt "Choose font folder to rename")

tell application "Finder"
	set filecount to count files in the entire contents of theSourceFolder
end tell



tell application "Finder"
	set filesArray to every file in the entire contents of theSourceFolder
	
	repeat with theFile in filesArray
		
		set fileAlias to theFile as alias
		
		set filePOSIXPath to POSIX path of fileAlias
		set mdlsName to (do shell script "mdls -name com_apple_ats_name_full " & quoted form of filePOSIXPath)
		
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"\""}
		
		try
			set fullName to second text item of mdlsName
			set newName to fullName
			set name of theFile to newName & buttonName
			
			
			set AppleScript's text item delimiters to oldDelims
			x
		on error
			
			set AppleScript's text item delimiters to oldDelims
			
		end try
		
	end repeat
	
end tell

display dialog (filecount as string) & " fonts have been renamed" buttons {"Ok"} default button "Ok"

This is in response to an email I received from the OP.

Yesterday I posted the script contained below. On retesting, I deleted the script because it caused files to be removed from the selected folder, and I did not have the time to determine what was causing this behavior.

I have included the script below, and it should be noted that:

  • the script displays a progress dialog (progress bar) only when the script is run as an app, and the number of files being renamed must be a fairly large number to see the progress bar. If the script is run from within Script Editor a progress notation is shown in the status bar;

  • this works only with recent versions of macOS;

  • of necessity, I had to move script segments out of the Finder tell statement;

  • this script should be used with test font files;

  • I tested the script and it does rename the files, although I’m not sure if they are renamed as the OP wants.

set theResponse to display dialog "TrueType (ttf) or OpenType (otf)" buttons {".ttf", ".otf"}
set buttonName to button returned of theResponse

set theSourceFolder to (choose folder with prompt "Choose font folder to rename")

tell application "Finder" to set filesArray to every file in the entire contents of theSourceFolder as alias list

set fileCount to (count filesArray)

set progress total steps to fileCount
set progress completed steps to 0
set progress description to "The files are being renamed."
set progress additional description to " "

repeat with i from 1 to fileCount
	set fileAlias to item i of filesArray
	
	set progress additional description to "Processing file " & i & " of " & fileCount
	
	set filePOSIXPath to POSIX path of fileAlias
	
	set mdlsName to (do shell script "mdls -name com_apple_ats_name_full " & quoted form of filePOSIXPath)
	
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"\""}
	try
		set fullName to second text item of mdlsName
		set newName to fullName
		tell application "Finder" to set name of fileAlias to (newName & buttonName)
		set AppleScript's text item delimiters to oldDelims
		x
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	
	set progress completed steps to i
	
end repeat

display dialog (fileCount as string) & " fonts have been renamed" buttons {"Ok"} default button "Ok"

For further information on this topic, please see:

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayProgress.html

EDIT:

I happened to look to see if there were any hidden files in the source folder and found a number of them, one example of which is:

This could be the cause of the missing files, although this might have resulted from an earlier test version of the script.

Thanks Thanks Thanks

Awesome work, I tested in different situations and your script worked like a charm. I agree with you concerning renaming the fonts this way. However, I don’t have much of a choice, my hard drive containing all my fonts crashed. I was able to restore 90% of its content, but all files were renamed with hexadecimal characters, fortunately it kept the spotlight metadata, hence the usefulness of this script I wrote.

Thank you again and take care of you and your family in these dark days,

Regards,

Maher,

P.-S. My backup drive was out of date.