Problem - How to change creation and modification date of selected files?

Creation date finder selection.kmmacros (3.4 KB)

hi

i think the macros didnt work, because i do not know how to make text data to unixtime.

I need that when i selected multiple file i can change creation date of this multiple file.

thanks

@qweewert, I’m not sure what your intent is in posting this macro.

We are glad to help, but we need more info.
Are you asking for help, or are you sharing a working macro you have built?

###If you are asking for help, please provide more details about the problem/issue:

  • What exactly is the problem?
  • What is the purpose/objective of your workflow and macro?
  • Where does the macro fail?
  • What is the typical source info that the macro needs to start?
    • Real-world examples are the best to provide
  • What is your expected outcome?

###If you are sharing a working macro, please post:

My apologies please

Hey @qweewert,

I think Keyboard Maestro’s action for setting the creation date expects epoch seconds.

Right now:

%Calculate%NOW()%

A specific date and time:

%Calculate%JD(2017, 04, 04, 10, 23, 00)%

-Chris

Since the User Prompt action in your macro is disabled, I assume you want to set the creation date to the current date.

In that case this will do the trick:

(You don’t need the other action.)

In case you want to set it to an arbitrary date, see Chris’ post for how to convert.


See also KM Wiki “Dates and Times”

No i need variable not NOW. I need to take data from user promt like 2017-03-03 and change creation date to this date

Creation date finder selection.kmmacros (3.3 KB)

Cant make it work

I wrote a similar macro last Sep. Maybe it would be of some help to you:
MACRO: [DATE] Change Creation and/or Modification Date of Selected Finder Item

1 Like

It change only 1 file at once

And vary difficult for me to understand )

Whups. I flubbed that and gave you the Julian Date function.

Try this:

Finder Selection ⇢ Set Creation date.kmmacros (3.7 KB)

-Chris

Thanks, but how to enter date to variable in yours script you set it to manual year day.........

Maby it be easier to make this script work on multiple file selection ?
https://forum.keyboardmaestro.com/uploads/default/original/2X/e/eaf7b4e6f746ab8eaa1bbdc116a867cc7fe2c2bc.kmmacros

Hey @qweewert,

I broke it up into variables, so you could do whatever you wanted in that regard...

In any case – here's one way of doing it.

-Chris


Finder Selection ⇢ Set Creation Date v1.00.kmmacros (4.5 KB)

3 Likes

Thank you!!! You genious
Solved

###Issue with GMTOFFSET()

Everyone should be aware of issues using GMTOFFSET(). It can lead to incorrect date/time in setting the date/time when DST is different between now and the date being set. See this complicated discussion with Peter for details.

That is one reason I prefer the AppleScript solution over the KM solution.

Yep, by am zero in apple scripting it more difficult )))

OK, I have modified the AppleScript to process ALL items in the Finder selection.
Just put this script in a Execute AppleScript Action.
Before that Action, set the KM Variable "FCD__NewDate", either directly or by using a Prompt.

NEW Date can be in ISO format (YYYY-MM-DD) or any format acceptable to AppleScript, like "MMM d, YYYY". AppleScript uses the date format set by your macOS, so it can vary with each Mac and locale. But the ISO format will work everywhere.

##applescript to Set Date(s) on All Items in Finder Selection

KM VARIABLES REQUIRED: (must be set before calling this script)

  • FCD__DateToChange
    • MUST be one of these:
      "Creation", "Modification", "Both"
  • FCD__NewDate
    • Date string convertible by AppleScript,
      like "2016-09-09" OR "Sep 9, 2016"
use AppleScript version "2.4" -- Yosemite (10.10)+
use framework "Foundation"
use scripting additions

property ptyScriptName : "Set Finder Item Dates"
property ptyScriptVer : "3.0"
property ptyScriptDate : "2017-04-06"
property ptyScriptAuthor : "JMichaelTX"

(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:  Set the Creation and/or Modification Date of Selected Finder Item
        (can be either file or folder)

RETURNS:

  IF SUCCESSFUL:
  • "OK" on line 1
  • Finder Item Name on line 3
  • Date info on lines 5 & 6
  
  IF ERROR:
    • "[ERROR]" on line 1
    • Error details on remaining lines

KM VARIABLES REQUIRED: (must be set before calling this script)

  • FCD__DateToChange -- MUST be one of these: "Creation", "Modification", "Both"
  • FCD__NewDate -- Date string convertable by AppleScript, like "Sep 9, 2016"
  
KM VARIABLES SET: (by this script)
  • NONE
  
REQUIRES:

  • masOS Yosemite (10.10)+
  • Keyboard Maestro 7.2.1
  
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
*)
set returnResults to "TBD"

try
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  if (not my checkReqdASVer("2.4")) then
    error "This script requires macOS Yosemite (10.10)+"
  end if
  
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #  Select Item in Finder, then run this script
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  tell application "Keyboard Maestro Engine"
    set dateToChangeStr to getvariable "FCD__DateToChange"
    set newDateStr to getvariable "FCD__NewDate"
  end tell
  
  --- FOR TESTING ---
  (*  
  --- SET TO ONE OF THESE:  "Creation", "Modification", "Both" ---
  set dateToChangeStr to "Both"
  
  set newDateStr to "Sep 1, 2012"
*)
  
  
  tell application "Finder" to set itemList to selection as alias list
  if length of itemList = 0 then error "No files were selected in the Finder!"
  
  set returnResults to "OK"
  
  repeat with oItem in itemList
    set itemPosixPath to POSIX path of oItem
    set returnResults to returnResults & return & my changeDate(itemPosixPath, dateToChangeStr, newDateStr)
  end repeat
  
  
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
on error errMsg number errNum
  
  if errNum = -128 then ## User Canceled
    set returnResults to "[USER_CANCELED]" & return & return ¬
      & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer
  else
    set returnResults to "[ERROR]" & return & return ¬
      & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
      & "Error Number: " & errNum & return ¬
      & errMsg
  end if
  
  
end try
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- RETURN THE RESULTS TO THE KM EXECUTE SCRIPT ACTION ---
return returnResults

--~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  HANDLERS FOR THIS SCRIPT
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on changeDate(pPOSIXpath, pDateToChange, pNewDate)
  --—————————————————————————————————————————————————————————————————————
  -- VER: 2.0    2016-09-10
  (*
  PURPOSE:  Change Creation and/or Modification Date(s) of Finder Item
  RETURNS:  String -- Summary of Changes that were made
  PARAMETERS:
    • pPOSIXpath    | text  | POSIX path to Finder Item
    • pDateToChange  | text  | Must be one of these:
                  "Creation"
                  "Modification"
                  "Both"
    • pNewDate    | text or date   | New Date to be set
  
  # Auth: Christopher Stone (@ccstone) & @ShaneStanley
  # ModBy: @JMichaelTX (minor mods)
  # dMod: 2016-09-09
  # Tags: @Core, @Date, @Files, @Finder, @Applescript, @Script
  
  HANDLERS REQUIRED:
  • on convertToDate(pDateStr)
  • on convertASDateToNSDate(pASDate)
  • on getItemName(pPOSIXpath)
  • to convertISOtoDate(pDateStr)
  • on formatDateISO(pDate)
  
  REQUIRES:
    • macOS Mavericks (10.9)+
    
  MODS BY @JMichaelTX:
    • Added handler to convert AppleScript Date to NSDate
    • Moved Finder commands to main script
    • Added Conversion of string date in ISO format to AS Date
    • Added return of results summary
    
  REFERENCES:
    • @ShaneStanley, 2016-06-27, ASUL, Can a Creation Date be Modified with ASObjC?
      http://lists.apple.com/archives/applescript-users/2016/Jun/msg00209.html
      
  You would think that since creation-date is NSURLCreationDateKey that modification-date would be NSURLModificationDateKey, but it's not.
  
  I had to search this reference:
  https://developer.apple.com/reference/foundation/urlresourcekey
  *)
  ---------------------------------------------------------------------------------
  local newDateClass, newDateISO, theURL, theResult, theError, itemName, resultsStr
  
  set newDateClass to class of pNewDate
  
  if newDateClass ≠ date then
    set pNewDate to my convertToDate(pNewDate)
  end if
  
  set newDateISO to text 1 thru 10 of (pNewDate as «class isot» as string)
  
  
  --- CONVERT AppleScript DATE TO NSDATE FOR USE WITH macOS before El Capitan (10.11) ---
  set pNewDate to convertASDateToNSDate(pNewDate)
  set theURL to current application's |NSURL|'s fileURLWithPath:pPOSIXpath
  
  if ((pDateToChange = "Creation") or (pDateToChange = "Both")) then
    
    #-----------------
    # Creation Date
    #-----------------
    
    set {theResult, theError} to theURL's setResourceValue:pNewDate forKey:(current application's NSURLCreationDateKey) |error|:(reference)
    
    if theResult as boolean is false then
      error (theError's localizedDescription() as text)
    end if
    
  end if
  
  if ((pDateToChange = "Modification") or (pDateToChange = "Both")) then
    
    #----------------------
    # Modification Date
    #----------------------
    
    set {theResult, theError} to theURL's setResourceValue:pNewDate forKey:(current application's NSURLContentModificationDateKey) |error|:(reference)
    
    if theResult as boolean is false then error (theError's localizedDescription() as text)
    
  end if
  
  set itemName to my getItemName(pPOSIXpath)
  
  set resultsStr to "Finder Item: " & return & itemName ¬
    & return & "Date(s) Chanaged: " & return & pDateToChange & return & newDateISO
  
  display notification "New Date: " & newDateISO ¬
    with title pDateToChange & " Date(s) Changed For:" subtitle itemName
  
  
  return resultsStr
  
end changeDate
-------------------------------------------------------------------------------------------


on convertToDate(pDate)
  
  --==========================================
  --  CONVERT TO DATE OBJECT
  --==========================================
  
  -- MUST NOT BE IN ANY APP WHEN USING THE date FUNCTION --
  
  if (class of pDate = date) then
    set dateAsDate to pDate
    
  else
    
    try -- Try Standard U.S. date format --
      set dateAsDate to date (pDate)
    on error --- Try International Date format (YYYY-MM-DD)
      set dateAsDate to my convertISOtoDate(pDate)
    end try
    
  end if
  
  return dateAsDate
  
end convertToDate

to convertISOtoDate(pDateStr)
  
  --- pDateStr MUST be in the format of YYYY<delim>MM<delim>DD
  ---    where <delim> can be any character
  ---    like 2016-01-05
  
  set resultDate to the current date
  
  set the year of resultDate to (text 1 thru 4 of pDateStr)
  set the month of resultDate to (text 6 thru 7 of pDateStr)
  set the day of resultDate to (text 9 thru 10 of pDateStr)
  set the time of resultDate to 0
  
  if (length of pDateStr) > 10 then
    set the hours of resultDate to (text 12 thru 13 of pDateStr)
    set the minutes of resultDate to (text 15 thru 16 of pDateStr)
    
    if (length of pDateStr) > 16 then
      set the seconds of resultDate to (text 18 thru 19 of pDateStr)
    end if
  end if
  
  return resultDate
  
end convertISOtoDate

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on checkReqdASVer(pVerReqdStr)
  -----------------------------------------------------------------
  
  considering numeric strings
    -- Use of "considering numeric strings" allows for the comparison
    --  of strings like "1.10.3" vs "1.9" and return that "1.10.3" is larger.
    --  Applescript's version returns a text value
    
    if AppleScript's version < pVerReqdStr then
      set verOK to false
      --error "This script requires macOS El Capitan (10.11)+"
    else
      set verOK to true
    end if
  end considering
  
  return verOK
  
end checkReqdASVer

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on convertASDateToNSDate(pASDate)
  --—————————————————————————————————————————————————————————————————————
  -- VER: 1.1    2016-09-10
  (*
  PURPOSE:  Convert Standard AppleScript Date to NSDate (ASObjC)
  
  REQUIRES:
    • use framework "Foundation"
    • macOS Mavericks (10.9)+

  
  NOTE:    This is needed ONLY in macOS prior to El Capitan (10.11)
        It should work with both Yosemite (10.10) and Mavericks (10.9)
        
  AUTHOR:  Adapted by @JMichaelTX from original by @ccstone & @ShaneStanley
  --——————————————————————————————————————————————————————————————————————
  *)
  local newNSDate, theYear, theMonth, theDay, theSeconds, theCalendar
  
  set {theYear, theMonth, theDay, theSeconds} to pASDate's {year, month, day, time}
  
  if theYear < 0 then
    set theYear to -theYear
    set theEra to 0
  else
    set theEra to 1
  end if
  
  set theCalendar to current application's NSCalendar's currentCalendar()
  set newNSDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) ¬
    |day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0
  
  return newNSDate
  
end convertASDateToNSDate

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getItemName(pPOSIXpath)
  --—————————————————————————————————————————————————————————————————————
  -- VER: 1.0    2016-09-10
  
  
  set AppleScript's text item delimiters to "/"
  set pathList to text items of pPOSIXpath
  
  repeat with oItem in (reverse of pathList)
    if (text of oItem ≠ "") then
      set itemName to text of oItem
      exit repeat
    end if
  end repeat
  
  return itemName
  
end getItemName

OK, I have used the above script, which processes ALL items in the Finder Selection, in a new macro I just published:

MACRO: @Date Set Date to Prompt User for ALL Items in Finder Selection

Feel free to use this macro, or parts thereof, to meet your needs.

This macro/script avoids the Issue with GMTOFFSET() that I mentioned above.

I came late to this conversation but took this one tidbit out and made my own macro for date modified – very helpful, thanks Tom!