• 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
Re: ASObjC: Get Most Recent File or Folder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ASObjC: Get Most Recent File or Folder


  • Subject: Re: ASObjC: Get Most Recent File or Folder
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 03 Jun 2016 00:34:45 -0500

On Jun 03, 2016, at 00:11, Jim Underwood <email@hidden> wrote:
For those that are blind like me, you can toggle the return of either:
  • only the last item added, 
  • OR a list of items sorted by date added,
______________________________________________________________________

Hey Jim,

Okay, this one has an option in the handler-call.

--
Take Care,
Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/02/09 06:58
# dMod: 2016/06/03 00:31
# Appl: ASObjC
# Task: Get the most recent item path from the designated directory.
#     : Allows for exclusion of folders.
# Tags: @Applescript, @Script, @ASObjC, @Get, @Most, @Recent, @Path
---------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------

# POSIX Path of the folder you're interested in.
set thePath to POSIX path of (path to downloads folder as text)

# Returns the POSIX Path of the last added item.
set sortedList to its filesIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey) includeFolders:false returningAsList:false

---------------------------------------------------------------------------------
--» HANDLERS
---------------------------------------------------------------------------------
on filesIn:folderPOSIXPath sortedBy:sortKey includeFolders:includeFoldersBool returningAsList:returningAsListBool
  # 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 an NSURL for the folder because that's what's needed
  set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath
  # Get a ref to the file manager
  set theNSFileManager to current application's NSFileManager's defaultManager()
  # Get list of NSURLs of items in folder, getting the values for our keys at the same time, and skipping invisible items
  set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
    includingPropertiesForKeys:keysToRequest ¬
    options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
    |error|:(missing value))
  # Create an array to store just the key values in
  set valuesNSArray to current application's NSMutableArray's array()
  # Loop through retrieving key values, 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 directories that aren't packages using a predicate
  if includeFoldersBool ≠ true then
    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
  end if
  # Make a sort descriptor that describes the key to sort on
  set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
  # Sort the array
  set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}

  

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

  

end filesIn:sortedBy:includeFolders:returningAsList:

# You can sort on other criteria, including: NSURLAttributeModificationDateKey, NSURLContentAccessDateKey, NSURLCreationDateKey, NSURLLabelColorKey, NSURLLabelNumberKey, NSURLLocalizedLabelKey, NSURLLocalizedTypeDescriptionKey, NSURLNameKey, NSURLTypeIdentifierKey, NSURLFileSizeKey, NSURLFileAllocatedSizeKey, NSURLTotalFileAllocatedSizeKey, and NSURLTotalFileSizeKey.

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

 _______________________________________________
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: ASObjC: Get Most Recent File or Folder
      • From: Dave <email@hidden>
References: 
 >ASObjC: Get Most Recent File or Folder (From: Christopher Stone <email@hidden>)
 >Re: ASObjC: Get Most Recent File or Folder (From: Jim Underwood <email@hidden>)

  • Prev by Date: Re: ASObjC: Get Most Recent File or Folder
  • Next by Date: Re: [FYI] more sdef-based library stupidity
  • Previous by thread: Re: ASObjC: Get Most Recent File or Folder
  • Next by thread: Re: ASObjC: Get Most Recent File or Folder
  • Index(es):
    • Date
    • Thread