Modification date of a file

Hello,

In my application, I want to read the modification date of a file.

This code works…

set diskName to boot volume of (system info)

tell application "System Events"
set dateDaily to (text 1 thru -4 of ((modification date of (info for file (diskName & ":private:var:log:daily.out"))) as text))
end tell

But I want not use “System Events” anymore (problem with Mojave).

Is it possible?

Thanks!!

Hi.

You’re not using System Events in that script. You’re using ‘info for’ and merely wrapping it in a ‘tell application “System Events”’ statement. You can either leave out ‘info for’ (which has been deprecated for ages anyway) or leave out the ‘tell’ wrapper.

Hi,
Thanks for this precision.

Strange… If I delete the ‘tell application “System Events”’ statement in the Script Editor, it works perfectly.
… but in Xcode (with the try instruction), I have the message “File some object wasn’t found” (Error -43).

 set diskName to boot volume of (system info)
 
try
	set dateDaily to (text 1 thru -4 of ((modification date of (info for file (diskName & ":private:var:log:daily.out"))) as text))
	display alert dateDaily
on error errText number errNum
	display alert ("Error: " & errNum) message errText
end try

Is there another solution to display the modification date (in ObjectiveC perhaps?)

use framework "Foundation"
use scripting additions

set theURL to current application's |NSURL|'s fileURLWithPath:"/"
set {theResult, theDate} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLContentModificationDateKey) |error|:(missing value))

Thanks a lot Shane but it’s beyond my skills.

I don’t know how to use this in my ASOC application.

I tried:

            set theURL to current application's |NSURL|'s fileURLWithPath:"/private/var/log/daily.out"
            set {theResult, theDate} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLContentModificationDateKey) |error|:(missing value))

            dailyscriptField's setStringValue_(theDate)

You need a string version of the date:

set theURL to current application's |NSURL|'s fileURLWithPath:"/private/var/log/daily.out"
set {theResult, theDate} to (theURL's getResourceValue:(reference) forKey:(current application's NSURLContentModificationDateKey) |error|:(missing value))
set df to current application's NSDateFormatter's new()
df's setDateFormat:"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXX" -- see https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
set theString to df's stringFromDate:theDate
dailyscriptField's setStringValue:theDate

Thank you very much Shane. It helps me a lot!!

I will adapt this for each localization now. :wink:

You can probably localize it by changing this:

df's setDateFormat:"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXX"

to this:

df's setLocalizedDateFormatFromTemplate:"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXX"

Great!! You are a god!

Just one thing… In the last line, replace theDate by theString. :wink:

dailyscriptField’s setStringValue:theString :cool: