Skip to content

Instantly share code, notes, and snippets.

@youandhubris
Last active April 1, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youandhubris/bd896229e1ba3757f7b56da69fcdf2a2 to your computer and use it in GitHub Desktop.
Save youandhubris/bd896229e1ba3757f7b56da69fcdf2a2 to your computer and use it in GitHub Desktop.
# Trim Filenames by Character Count
set userFolder to choose folder
set dialogResult to display dialog "Numbers of characters to remove:" buttons {"Cancel", "At beginning", "At end"} default answer 0 cancel button "Cancel"
set buttonResult to button returned of dialogResult
set numberOfChars to text returned of dialogResult as integer
if buttonResult = "At end" then set numberOfChars to -numberOfChars
tell application "Finder"
repeat with eachFile in (get document files in userFolder)
set fullName to name of eachFile
set {textDelimiters, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
if (count of text items of fullName) is greater than 1 then
set properName to my Trim((text items 1 thru -2 of fullName) as text, numberOfChars)
set newName to ({properName, (get last text item of fullName)}) as text
else
set newName to my Trim(fullName, numberOfChars)
end if
set AppleScript's text item delimiters to textDelimiters
set name of eachFile to newName
end repeat
end tell
on Trim(t, n)
if n > 0 then
return text (n + 1) thru -1 of t
else
return text 1 thru (n - 1) of t
end if
end Trim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment