• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
List Files Sorted by Last-Added Date
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

List Files Sorted by Last-Added Date


  • Subject: List Files Sorted by Last-Added Date
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 16 Sep 2015 12:50:16 -0500

Hey Folks,

This is just one of Shane's handlers repackaged a bit to make it easier for me to process.

Presently the sort is: NSURLAddedToDirectoryDateKey

* Other keys are listed for convenience.

You can get just the single most recent item by changing the boolean value in the handler-call to true.

I must say that after some concentrated attention ASObjC is getting much less mind-boggling.

I still have a ways to go before I'm comfortable enough with it to write my own routines, but modifying other people's work is easy enough (and how I started learning AppleScript to begin with 20+ years ago).

Looking at it doesn't give me a headache anymore either.  :)

Thanks to Shane Stanley for all the work he's put into it.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# Auth: Shane Stanley; ASObjC Guru
# dCre: 2015/09/15 20:08
# dMod: 2015/09/16 12:41
# Appl: ASObjC
# Task: Get a list of Files from a Directory sorted by Last-Added (other sort-keys listed for convenience).
# Libs: None
# Osax: None
# Tags: @Applescript, @ASObjC, @Script, @Sorted, @File, @List, @Shane
# Note: Tested only on OSX 10.10.5.
-------------------------------------------------------------------------------------------

use framework "Foundation"
use scripting additions

set targetFolder to path to downloads folder
set thePath to POSIX path of targetFolder

set sortedList to its filesIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey) returningSingleItem:false

-------------------------------------------------------------------------------------------

(*

OTHER SORT CRITERIA

NSURL AddedToDirectoryDateKey
NSURL AttributeModificationDateKey
NSURL ContentAccessDateKey
NSURL ContentModificationDateKey
NSURL CreationDateKey
NSURL FileAllocatedSizeKey
NSURL FileSizeKey
NSURL LabelColorKey
NSURL LabelNumberKey
NSURL LocalizedLabelKey
NSURL LocalizedTypeDescriptionKey
NSURL NameKey
NSURL TotalFileAllocatedSizeKey
NSURL TotalFileSizeKey
NSURL TypeIdentifierKey

* REMOVE THE SPACE AFTER 'NSURL' TO USE!

*)

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------

on filesIn:folderPOSIXPath sortedBy:sortKey returningSingleItem:singleItemBoolVal

  

  # Specify keys for the values we want for each item in the folder.
  # We want the path, whether it's a directory, whether it's a package, and the key to sort on.
  set keysToRequest to {current application's NSURLPathKey, ¬
    current application's NSURLIsPackageKey, ¬
    current application's NSURLIsDirectoryKey, ¬
    sortKey}

  

  # Make a NSURL for the folder because that's what's needed.
  set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath

  

  # Get a reference to the file manager.
  set theNSFileManager to current application's NSFileManager's defaultManager()

  

  # Get the NSURLs of items in folder while at the same time getting values for our keys and skipping invisible items.
  set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
    includingPropertiesForKeys:keysToRequest ¬
    options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
    |error|:(missing value)) as list

  

  # Create an array in which to store just the key values.
  set valuesNSArray to current application's NSMutableArray's array()

  

  # Loop through the NSURL list retrieving key values and adding each set as a dictionary/record to the array/list.
  repeat with oneNSURL in listOfNSURLs
    (valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value)))
  end repeat

  

  # Filter out all non-packages directories using a predicate.
  set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey)
  set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate

  

  # Make a sort descriptor that describes the specific sort key.
  set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true

  

  # Sort the array.
  set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}

  

  if singleItemBoolVal = false then
    # Extract just the paths and convert to an AppleScript list.
    return (theSortedNSArray's valueForKey:(current application's NSURLPathKey)) as list
  else
    # If you want the single most-recent file, use this instead of the previous line.
    return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
  end if

  

end filesIn:sortedBy:returningSingleItem:

-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: List Files Sorted by Last-Added Date
      • From: Takaaki Naganoya <email@hidden>
    • Re: List Files Sorted by Last-Added Date
      • From: Shane Stanley <email@hidden>
  • Prev by Date: convert text data to PDF
  • Next by Date: Re: Does this work for anyone in Yosemite?
  • Previous by thread: Re: [ALERT:Possible Phishing] convert text data to PDF
  • Next by thread: Re: List Files Sorted by Last-Added Date
  • Index(es):
    • Date
    • Thread