• 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: Jim Underwood <email@hidden>
  • Date: Fri, 08 Jul 2016 21:22:15 +0000
  • Thread-topic: ASObjC: Get Most Recent File or Folder

Chris and Shane,

Thanks for a great handler!

I'm trying to refactor it into a more user-friendly handler, with sort options.
But it fails when I use a variable for the sort key "NSURLAddedToDirectoryDateKey" or any other NS sort key.

How can I setup my handler to provide for sort options?

TIA for any help or suggestions.

MY TEST Script: (simplified for this question)


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 (choose folder) -- (path to downloads folder as text)


set sortBy to "NSURLAddedToDirectoryDateKey"


# Returns the POSIX Path of the last added item.


--- ORIGINAL STATEMENT ---  WORKS

set filePathMostRecent to its filesIn:thePath sortedBy:(current application's NSURLAddedToDirectoryDateKey) includeFolders:false returningAsList:false


--- MY MOD USING VARIABLE for sort key -- Does NOT Work

set filePathMostRecent to its filesIn:thePath sortedBy:(current application's sortBy) includeFolders:false returningAsList:false



--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--- MY HANDLER ---


on getFilesPath(pFolder, pFileOrList, pSortBy)

  local TBD

  

  if (pFileOrList = "List") then

    set returnListBol to true

  else

    set returnListBol to false

  end if

  

  set NSSortKeyList to {"NSURLNameKey", "NSURLCreationDateKey", "NSURLAttributeModificationDateKey", "NSURLAddedToDirectoryDateKey"}

  set sortByList to {"Name", "Created", "Modified", "Added"}  -- use Finder column labels

  

  set sortBy to my getItemInOtherList(pSortBy, sortByList, NSSortKeyList) as text

  

  set filePathOrList to its filesIn:pFolder sortedBy:(current application's sortBy) includeFolders:false returningAsList:returnListBol

  

end getFilesPath


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on getItemInOtherList(pItem, pSourceLIst, pLookupList)

  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

  

  local indexItem, itemInOtherList

  

  set indexItem to indexInList(pItem, pSourceLIst)

  set itemInOtherList to item indexItem of pLookupList

  

  return itemInOtherList

  

  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

end getItemInOtherList



--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on indexInList(pItem, pList)

  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

  repeat with i from 1 to the count of pList

    if item i of pList is pItem then return i

    

  end repeat

  return 0

  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

end indexInList





Best Regards,

Jim Underwood
aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of Chris Stone <email@hidden>
Date: Fri, Jun 3, 2016 at 12:34 AM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Re: ASObjC: Get Most Recent File or Folder

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
---------------------------------------------------------------------------------
useAppleScript version"2.4"
useframework "Foundation"
usescripting additions
---------------------------------------------------------------------------------

# POSIX Path of the folder you're interested in.
setthePathtoPOSIX pathof (path todownloads folder astext)

# Returns the POSIX Path of the last added item.
setsortedList toitsfilesIn:thePathsortedBy:(current application'sNSURLAddedToDirectoryDateKey) includeFolders:falsereturningAsList:false

---------------------------------------------------------------------------------
--» HANDLERS
---------------------------------------------------------------------------------
onfilesIn:folderPOSIXPathsortedBy:sortKeyincludeFolders:includeFoldersBoolreturningAsList: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
  setkeysToRequest to {current application'sNSURLPathKey, ¬
    current application'sNSURLIsPackageKey, ¬
    current application'sNSURLIsDirectoryKey, ¬
    sortKey}
  # Make an NSURL for the folder because that's what's needed
  settheFolderURL tocurrent application'sclass"NSURL"'sfileURLWithPath:folderPOSIXPath
  # Get a ref to the file manager
  settheNSFileManager tocurrent application'sNSFileManager's defaultManager()
  # Get list of NSURLs of items in folder, getting the values for our keys at the same time, and skipping invisible items
  setlistOfNSURLs to (theNSFileManager'scontentsOfDirectoryAtURL:theFolderURL ¬
    includingPropertiesForKeys:keysToRequest ¬
    options:(current application'sNSDirectoryEnumerationSkipsHiddenFiles) ¬
    |error|:(missing value))
  # Create an array to store just the key values in
  setvaluesNSArray tocurrent application'sNSMutableArray's array()
  # Loop through retrieving key values, adding each set as a dictionary/record to the array/list
  repeatwithoneNSURL inlistOfNSURLs
    (valuesNSArray'saddObject:(oneNSURL'sresourceValuesForKeys:keysToRequest|error|:(missing value)))
  end repeat

  

  # Filter out all directories that aren't packages using a predicate
  ifincludeFoldersBool ≠ truethen
    settheNSPredicate tocurrent application'sNSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application'sNSURLIsDirectoryKey, current application'sNSURLIsPackageKey)
    setvaluesNSArray tovaluesNSArray's filteredArrayUsingPredicate:theNSPredicate
  end if
  # Make a sort descriptor that describes the key to sort on
  settheDescriptor tocurrent application'sNSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
  # Sort the array
  settheSortedNSArray tovaluesNSArray's sortedArrayUsingDescriptors:{theDescriptor}

  

  ifreturningAsListBool = truethen
    # Extract just the paths and convert to an AppleScript list
    return (theSortedNSArray'svalueForKey:(current application'sNSURLPathKey)) aslist
  else
    # Or if you only want the most-recent, use this instead of the previous line:
    return (theSortedNSArray'slastObject()'s valueForKey:(current application'sNSURLPathKey)) astext
  end if

  

endfilesIn: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: Jim Underwood <email@hidden>
  • Prev by Date: Re: Trouble Saving New PDFPenPro File
  • Next by Date: Re: ASObjC: Get Most Recent File or Folder
  • Previous by thread: Re: Trouble Saving New PDFPenPro File
  • Next by thread: Re: ASObjC: Get Most Recent File or Folder
  • Index(es):
    • Date
    • Thread