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
@wss7932
Copy link

wss7932 commented May 9, 2013

I am currently using this script to do some revision control with CAD files using DraftSight. Is there a way to do this same thing but, from the script menu while in DraftSight itself. Instead of clicking save periodically I would like to click the script and have it time/date stamp the saves so I have a history of my work.

@clemsam
Copy link

clemsam commented Aug 13, 2018

Hi!
I used your code to "synchronize" pictures (ab. 650) from various camera devices in my family (3 smartphones, 1 camera).
In order to get all pictures in a useful sequence I also had to add the time of day (hour, minute, seconds).

More problem I had is that our devices do NOT run correctly in regard to time – e.g. the camera "gains" about 2 minutes every month (here: +720 sec), my smartphone is hardly ever online and slow (-210 sec) ... so I added a "correction dialog" asking for a number (in seconds) to be subtracted or added to the "original" result.
(I had to find pictures taken at about the same time to get comparison values for these corrections.)

Here are my additions:
(* Time correction, in front of "tell application finder": *)
global plusMinus
set plusMinus to 0

(* Asking for value, put after "set the source_folder to (folder of the front window) as text" *)
set Corrections to display dialog "Insert number of seconds (also negative):" default answer "" buttons {"Okay"} default button 1
set plusMinus to text returned of Corrections as integer

The main calculation of course happens in "on dateFormat(theDate)":

(* Adding values for time of day, put after "set b's month to January" *)
set hou to (((t - plusMinus) / 3600) - ((t - plusMinus) / 3600 mod 1) as integer)
set min to ((((t - plusMinus) - hou * 3600) / 60) - (((t - plusMinus) - hou * 3600) / 60 mod 1) as integer)
set sec to (t mod 60)
if (hou < 10) then set hou to "0" & hou
if (min < 10) then set min to "0" & min
if (sec < 10) then set sec to "0" & sec
(* Change the "tell" part of "on dateFormat(theDate)" to: *)
set date_string to (text 1 thru 4 & text 5 thru 6 & text 7 thru 8 & "_" & hou & min & sec)

("mod 1" => "as integer" rounds values, sometimes up, sometimes down ...
"if (xxx < 10) ..." => get a leading zero: "180704" instead of "1874".)
Please be aware that I am not an IT professional – be lenient with any "stupid" algorithms ...

Last point:
one of our smartphones (or its transfer app) saved pictures with the "current" date – so I had to replace "modification date" in the above original code to "creation date" to get a valid result (I did NOT put this into another dialog).

@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