• 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 22:50:43 +0000
  • Thread-topic: ASObjC: Get Most Recent File or Folder

I'm trying to mod the handler by Chris & Shane, but I can't get it to work when the sort is descending.
The original code used "lastObject()'s" with an ascending sort, so I thought I'd try "firstObject()'s" with a descending sort.

But that does not work.  The "firstObject()'s" returns the entire list of file paths.

How do I get just the first file path?

TIA for your help.

MY SCRIPT:  (excerpt from Chris' script, with my mod)


  # Make a sort descriptor that describes the key to sort on

  set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:sortAscendBool  # MY MOD to allow true or false

  

  # 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:

    

    if sortAscendBool then

      

      ### THIS WORKS -- THE LAST FILE PATH IS RETURNED ###

      return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text

    else

      ###   MY MOD -- THIS RESULTS IN THE ENTIRE LIST BEING RETURNED ###

      return (theSortedNSArray's firstObject()'s valueForKey:(current application's NSURLPathKey)) as text

    end if

  end if




I did a search in Shane's ASObjC book, but did not find any info about "lastObject" or "firstObject".

Best Regards,

Jim Underwood
aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of JMichael <email@hidden>
Date: Fri, Jul 8, 2016 at 4:44 PM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Re: ASObjC: Get Most Recent File or Folder

Follow-up:

I do have this approach, but thought there might be a better one:



  set currApp to current application

  

  if (pSortBy = "Name") then

    set NSSortBy to currApp's NSURLNameKey

  else if (pSortBy = "Created") then

    set NSSortBy to currApp's NSURLCreationDateKey

  else if (pSortBy = "Modified") then

    set NSSortBy to currApp's NSURLAttributeModificationDateKey

  else if (pSortBy = "Added") then

    set NSSortBy to currApp's NSURLAddedToDirectoryDateKey

  end if

  

  set filePathOrList to its filesIn:pFoldersortedBy:NSSortByincludeFolders:falsereturningAsList:returnListBol





Best Regards,

Jim Underwood
aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of JMichael <email@hidden>
Date: Fri, Jul 8, 2016 at 4:22 PM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Re: ASObjC: Get Most Recent File or Folder

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?
 _______________________________________________
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: Shane Stanley <email@hidden>
    • Re: ASObjC: Get Most Recent File or Folder
      • From: Jim Underwood <email@hidden>
References: 
 >Re: ASObjC: Get Most Recent File or Folder (From: Jim Underwood <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: ASObjC: Get Most Recent File or Folder
  • 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