Thanks to a variety of helpful posts in this forum, I have some code that works for obtaining the Creation Date of a single user-selected NSURL. However, I cannot get the code to work for either a hard-coded NSURL, nor within a loop through an NSFileManager enumerator.
I am not a professional programmer; I make apps that are tools for office. My ultimate goal is to simply sort an Array of NSURL objects based on Creation Date.
The code I am using is below, which functions just fine as is, but if I try to use the commented line to evaluate a specific PDF file, I get the following error:
I get the exact same error when I try to add this code to a loop of NSURL objects procured via the NSFileManager Enumerator.
I cannot figure out how to use the error instruction to solve the problem. If anyone can assist, that would be tremendous. Thank you.
let chosenURL = NSOpenPanel().selectFile
//let chosenURL = NSURL.fileURL(withPath: "/Users/craigsmith/Desktop/PDFRotator Introduction.pdf")
do
{
var cr:AnyObject?
try chosenURL?.getResourceValue(&cr, forKey: URLResourceKey.creationDateKey)
if (cr != nil)
{
if let createDate = cr as? NSDate
{
print("Seems to be a date: \(createDate)")
let theComparison = createDate.compare(NSDate() as Date)
print("Result of Comparison: \(theComparison)") // Useless
let interval = createDate.timeIntervalSinceNow
print("Interval: \(interval)")
if interval < (60*60*24*7*(-1))
{
print("More than a week ago")
}
else
{
print("Less than a week ago")
}
}
else
{
print("Not a Date")
}
}
}
catch
{
}
You can extend URL as follow:
extension URL {
/// The time at which the resource was created.
/// This key corresponds to an Date value, or nil if the volume doesn't support creation dates.
/// A resource’s creationDateKey value should be less than or equal to the resource’s contentModificationDateKey and contentAccessDateKey values. Otherwise, the file system may change the creationDateKey to the lesser of those values.
var creation: Date? {
get {
return (try? resourceValues(forKeys: [.creationDateKey]))?.creationDate
}
set {
var resourceValues = URLResourceValues()
resourceValues.creationDate = newValue
try? setResourceValues(resourceValues)
}
}
/// The time at which the resource was most recently modified.
/// This key corresponds to an Date value, or nil if the volume doesn't support modification dates.
var contentModification: Date? {
get {
return (try? resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
}
set {
var resourceValues = URLResourceValues()
resourceValues.contentModificationDate = newValue
try? setResourceValues(resourceValues)
}
}
/// The time at which the resource was most recently accessed.
/// This key corresponds to an Date value, or nil if the volume doesn't support access dates.
/// When you set the contentAccessDateKey for a resource, also set contentModificationDateKey in the same call to the setResourceValues(_:) method. Otherwise, the file system may set the contentAccessDateKey value to the current contentModificationDateKey value.
var contentAccess: Date? {
get {
return (try? resourceValues(forKeys: [.contentAccessDateKey]))?.contentAccessDate
}
// Beginning in macOS 10.13, iOS 11, watchOS 4, tvOS 11, and later, contentAccessDateKey is read-write. Attempts to set a value for this file resource property on earlier systems are ignored.
set {
var resourceValues = URLResourceValues()
resourceValues.contentAccessDate = newValue
try? setResourceValues(resourceValues)
}
}
}
usage:
print(yourURL.creationDate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With