Skip to content

Instantly share code, notes, and snippets.

@codingChewie
Last active March 26, 2021 16:27
Show Gist options
  • Save codingChewie/9ca54d14cb2ebd380bf79299b9d63dd9 to your computer and use it in GitHub Desktop.
Save codingChewie/9ca54d14cb2ebd380bf79299b9d63dd9 to your computer and use it in GitHub Desktop.
AppleScript that will replace whitespace with underscores in a file's name within a given directory
(*
Date: 16-12-14
Developer: codingChewie
Purpose: Takes filenames with whitespace and replaces with underscore in a directory
Version: 1.0
Name: no-whitespace-files.scpt
Site: http://programmingmonkeys.com/
*)
on no_whitespace(theFolder, scriptTitle)
set theDelimiters to AppleScript's text item delimiters
tell application "Finder"
set theseItems to items of theFolder
try
repeat with thisItem in theseItems
set thename to name of thisItem
if thename contains " " then
set AppleScript's text item delimiters to " "
set newname to text items of thename
set AppleScript's text item delimiters to "_"
set name of thisItem to (newname as string)
set AppleScript's text item delimiters to theDelimiters
end if
end repeat
end try
end tell
display notification "Completed Script" with title scriptTitle
end no_whitespace
on run
set scriptTitle to "No Whitespace Script"
try
set theFolder to (choose folder with prompt "Please select a directory.")
no_whitespace(theFolder, scriptTitle)
on error error_message number error_number
if error_number is equal to -128 then
display notification "Script was cancelled" with title scriptTitle
else
display dialog error_message with title scriptTitle
end if
end try
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment