Skip to content

Instantly share code, notes, and snippets.

@mnot
Last active January 27, 2021 06:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mnot/221399 to your computer and use it in GitHub Desktop.
Save mnot/221399 to your computer and use it in GitHub Desktop.
Applescript to rename a selected file, appending the current date
(*
Rename with Date 0.1
Copyright 2004 Mark Nottingham <mnot@mnot.net>
THIS SOFTWARE IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, AND MAY BE
COPIED, MODIFIED OR DISTRIBUTED IN ANY WAY, AS LONG AS THIS NOTICE
AND ACKNOWLEDGEMENT OF AUTHORSHIP REMAIN.
*)
tell application "Finder"
try
set the source_folder to (folder of the front window) as text
on error -- no open folder windows
set the source_folder to (path to desktop folder) as text
end try
set these_items to the selection
end tell
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items) as alias
set this_info to info for this_item
set {file_name, file_ext} to splitExtension from the name of this_info
set this_date to the modification date of this_info
set formatted_date to (my dateFormat(this_date))
set new_name to file_name & " (" & formatted_date & ")" & file_ext
-- check to see if it's already there
tell application "Finder"
if (exists item (source_folder & new_name)) then
display dialog new_name & " already exists."
else
set name of this_item to new_name
end if
end tell
end repeat
on dateFormat(theDate)
set {year:y, day:d, time:t} to theDate
copy theDate to b
set b's month to January
tell (y * 10000 + (b - 2500000 - theDate) div -2500000 * 100 + d) as string
set date_string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end tell
return date_string
end dateFormat
to splitExtension from file_name
set dot to "."
tell AppleScript
set oT to text item delimiters
set text item delimiters to dot
if (count text items of file_name) > 1 then
set out_name to (text items 1 through -2 of file_name) as string
set ext to last text item of file_name
else
set out_name to file_name
set ext to ""
end if
set text item delimiters to oT
if ext is not "" then set ext to dot & ext
return {out_name, ext}
end tell
end splitExtension
@bocciaman
Copy link

I tried to run this as a service but nothing seems to be happening. Help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment