• 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 – Find Files by File Extension
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ASObjC – Find Files by File Extension


  • Subject: Re: ASObjC – Find Files by File Extension
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 17 May 2016 09:52:07 +1000

On 17 May 2016, at 5:40 AM, Christopher Stone <email@hidden> wrote:

Maybe if we ask nicely Shane will show us how to search by UTI.  (Please...)

It's not quite as simple, because UTIs aren't properties of an NSURL, so you can't use a predicate on an array of URLs.

What you need to do is get the URLs, then build an array of dictionaries -- a list of records, in AppleScript terms -- where each record contains the URL plus its UTI. Then you can use a predicate to filter based on the UTI.

Because getting the UTIs involves a repeat loop, it makes sense to do that only once, in its own handler. So something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set sourceFolder to POSIX path of (path to desktop folder)
set theFileInfo to my listFilesAndUTIsIn:sourceFolder
set theResult to my filterArray:theFileInfo conformingTo:"public.image"

on listFilesAndUTIsIn: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 tempArray to current application's NSMutableArray's arrayWithCapacity:(directoryContents's |count|()) -- array to hold values
repeat with aURL in directoryContents
set {theResult, theUTI} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLTypeIdentifierKey) |error|:(missing value))
(tempArray's addObject:(current application's NSDictionary's dictionaryWithObjects:{aURL, theUTI} forKeys:{"theURL", "theUTI"}))
end repeat
return tempArray
end listFilesAndUTIsIn:

on filterArray:tempArray conformingTo:someUTI
set thePredicate to current application's NSPredicate's predicateWithFormat_("theUTI UTI-CONFORMS-TO %@", someUTI)
set foundItemList to (tempArray's filteredArrayUsingPredicate:thePredicate)'s valueForKey:"theURL"


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


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


end filterArray:conformingTo:


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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 – Find Files by File Extension
      • From: Yvan KOENIG <email@hidden>
References: 
 >ASObjC – Find Files by File Extension (From: Christopher Stone <email@hidden>)
 >Re: ASObjC – Find Files by File Extension (From: Yvan KOENIG <email@hidden>)
 >Re: ASObjC – Find Files by File Extension (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Comparison of Day Names Failing Although Appearing To Be The Same
  • Next by Date: Re: ASObjC – Find Files by File Extension
  • Previous by thread: Re: ASObjC – Find Files by File Extension
  • Next by thread: Re: ASObjC – Find Files by File Extension
  • Index(es):
    • Date
    • Thread