Skip to content

Instantly share code, notes, and snippets.

@dannysmith
Created June 28, 2013 21:57
Show Gist options
  • Select an option

  • Save dannysmith/5888471 to your computer and use it in GitHub Desktop.

Select an option

Save dannysmith/5888471 to your computer and use it in GitHub Desktop.
--Fucntions
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
--Build Year String
set y to year of (current date)
set d to my add_leading_zeros(day of (current date), 1)
set m to my add_leading_zeros(month of (current date) as integer, 1)
set myDate to y & m & d as string
--Get Selected Text
tell application "Finder"
set fileAlias to selection as alias
set filePath to fileAlias as text
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set fileName to last text item of filePath
set AppleScript's text item delimiters to "."
set fileNamePart1 to text item 1 of fileName
set fileNamePart2 to text item 2 of fileName
set AppleScript's text item delimiters to TID
set mySubject to fileNamePart1 as text
set legalPrefixCharacters to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set legalSuffixCharacters to {"-U", "-P", "-PS", "-PC", "-PD", "-R", "-S", "-TS", "9", "0"}
--Get the prefix eg "20130101" and the suffix eg "-U".
if length of mySubject > 8 then
set thePrefix to (characters 1 thru 8) of mySubject
set hasMODPrefix to true
--Check that the prefix is all numbers
repeat with thisChr from 1 to (get count of characters in thePrefix)
set theChr to character thisChr of thePrefix
if theChr is not in legalPrefixCharacters then
set hasMODPrefix to false
end if
end repeat
else
set hasMODPrefix to false
end if
--If the prefix is there, get the rest of the message and put the new prefix date in it.
--Otherwise use the whole message and add the new prefix date.
if hasMODPrefix is true then
set newSubject to (myDate & "-" & (characters 10 thru -1) of mySubject as text)
else
set newSubject to myDate & "-" & mySubject
end if
set theSuffix to (characters -1 thru -2) of newSubject as text
--If the suffix is not there, add it
if theSuffix is not in legalSuffixCharacters then
set newSubject to newSubject & "-AdjCTT-U"
end if
set fileName to newSubject & "." & fileNamePart2
--set name of fileAlias to fileName
set properties of fileAlias to {name:fileName}
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment