• 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
Filter Out Files by Extension
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Filter Out Files by Extension


  • Subject: Filter Out Files by Extension
  • From: Christopher Stone <email@hidden>
  • Date: Sun, 26 Feb 2017 23:51:30 -0600

Hey Folks,

I managed to repurpose one of Shane's scripts to filter-out-by-extension rather than find-by-extension.

I needed this for a task today, and now I'm struggling with figuring out compound predicates.

In any case I thought I'd share.  (The original is appended for convenience.)

--
Best Regards,
Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/05/13 23:06
# dMod: 2017/02/26 23:45
# Appl: AppleScriptObjC
# Task: List Files by Extension → Handler
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @List, @Files, @Extension, @File_Extension
------------------------------------------------------------------------------

use framework "Foundation"
use framework "AppKit"
use scripting additions

set sourceFolder to POSIX path of (path to desktop folder)

set textFiles to its filterOutFilesByExtension:{"txt", "scpt"} fromDirectory:sourceFolder

on filterOutFilesByExtension:listOfExtensions fromDirectory:sourceFolder
    set fileManager to current application's NSFileManager's defaultManager()
    set aURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
    set directoryContents to fileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:0 |error|:(missing value)
    set foundItemList to current application's NSPredicate's predicateWithFormat_("NOT pathExtension.lowercaseString IN %@", listOfExtensions)
    set foundItemList to directoryContents's filteredArrayUsingPredicate:foundItemList

    

    # Return as a list of as «class furl» 
    return foundItemList as list

    

    # Return as a list POSIX Paths
    # set foundItemList to (foundItemList's valueForKey:"path") as list
    # return foundItemList

    

end filterOutFilesByExtension:fromDirectory:

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





------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/05/13 23:06
# dMod: 2016/05/16 05:15
# Appl: AppleScriptObjC
# Task: List Files by Extension → Handler
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @List, @Files, @Extension, @File_Extension
------------------------------------------------------------------------------

use framework "Foundation"
use framework "AppKit"
use scripting additions

set sourceFolder to POSIX path of (path to desktop folder)

set pdfFiles to its listFilesByExtension:{"txt"} fromDirectory:sourceFolder

on listFilesByExtension:listOfExtensions fromDirectory:sourceFolder
set fileManager to current application's NSFileManager's defaultManager()
set aURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
set directoryContents to fileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:0 |error|:(missing value)
set foundItemList to current application's NSPredicate's predicateWithFormat_("pathExtension.lowercaseString IN %@", listOfExtensions)
set foundItemList to directoryContents's filteredArrayUsingPredicate:foundItemList


# Return as a list of as «class furl» 
return foundItemList as list


# Return as a list POSIX Paths
# set foundItemList to (foundItemList's valueForKey:"path") as list
# return foundItemList


end listFilesByExtension:fromDirectory:

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

 _______________________________________________
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: Filter Out Files by Extension
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Restoring the Finder Selection After Moving an Item or Items.
  • Next by Date: Re: Restoring the Finder Selection After Moving an Item or Items.
  • Previous by thread: Re: Restoring the Finder Selection After Moving an Item or Items.
  • Next by thread: Re: Filter Out Files by Extension
  • Index(es):
    • Date
    • Thread