• 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: Yvan KOENIG <email@hidden>
  • Date: Mon, 16 May 2016 17:33:21 +0200


Le 16 mai 2016 à 15:44, Christopher Stone <email@hidden> a écrit :

Hey Folks,

As you may (or may not) remember El Capitan completely frelled my Desktop Sweeper script.

Pre 10.11 it was fast enough if not lightning fast – post 10.11 it slowed to a very unacceptable crawl.

Initially I filled the gap with the Satimage.osax's glob command which is quite nice (instead of a fairly complex Finder whose clause).

set sourceFolder to path to desktop folder
set fileList to glob "*" of extension {"pdf", "PDF"} from sourceFolder

Then I was fooling with the Satimage.osax's list files command and noticed it understood UTIs, so I moved to that.

set sourceFolder to path to desktop folder
set fileList to list files sourceFolder conforming to {"com.adobe.pdf"}

The Satimage.osax has made my AppleScripting life sooo much easier since 2003 – long may it live.

Now I'm using the Desktop Sweeper script as a learning project for ASObjC.

This relatively simple handler (listFilesByExtension) will non-recursively list files by extension from the designated source folder.

It is case-insensitive on execution, BUT the list of file extensions in listOfExtensions MUST be lower-case.

It's set up to return a list of «class furl»’s, but I've included code to return POSIX Paths (commented out in the handler).

Thanks again to Shane – without his help I'd still be completely lost in Yonkers.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/05/13 23:06
# dMod: 2016/05/16 07:51
# Appl: AppleScriptObjC
# Task: listFilesByExtension → Handler → Example Sweeper
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @List, @Files, @Extension, @File_Extension, @Sweeper
-------------------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------------------------

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

set applescriptFileList to its listFilesByExtension:{"scpt", "scptd", "applescript"} fromDirectory:sourceFolder
set bbeditWorksheetList to its listFilesByExtension:{"worksheet"} fromDirectory:sourceFolder
set clippingFileList to its listFilesByExtension:{"textClipping"} fromDirectory:sourceFolder
set compressedArchiveList to its listFilesByExtension:{"sit", "zip"} fromDirectory:sourceFolder
set diskImageList to its listFilesByExtension:{"dmg"} fromDirectory:sourceFolder
set imageFileList to its listFilesByExtension:{"gif", "jpg", "png"} fromDirectory:sourceFolder
set movieFileList to its listFilesByExtension:{"flv", "mov", "mp4"} fromDirectory:sourceFolder
set pdfFiles to its listFilesByExtension:{"pdf"} fromDirectory:sourceFolder
set rtfFileList to its listFilesByExtension:{"rtf", "rtfd"} fromDirectory:sourceFolder
set textFileList to its listFilesByExtension:{"txt"} fromDirectory:sourceFolder
set webArchiveList to its listFilesByExtension:{"webarchive"} fromDirectory:sourceFolder
set webBookmarkList to its listFilesByExtension:{"webbookmark", "webloc"} fromDirectory:sourceFolder

{applescriptFileList, bbeditWorksheetList, clippingFileList, compressedArchiveList, diskImageList, imageFileList, movieFileList, pdfFiles, rtfFileList, textFileList, webArchiveList, webBookmarkList}

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
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 «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 listFilesByExtension:fromDirectory:
-------------------------------------------------------------------------------------------


Thanks Christopher and Shane

I just wish to edit an instruction :
set imageFileList to its listFilesByExtension:({"gif", "jpg", "png"} & {"jpeg", "tif", "tiff "}) fromDirectory:sourceFolder

because of course, the first time I ran the original script, it missed the ".jpeg" file and the ".tiff" one which were on my desktop ;-)


Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 16 mai 2016 17:31:46



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

  • Prev by Date: ASObjC – Find Files by File Extension
  • Next by Date: Re: ASObjC – Find Files by File Extension
  • Previous by thread: ASObjC – Find Files by File Extension
  • Next by thread: Re: ASObjC – Find Files by File Extension
  • Index(es):
    • Date
    • Thread