• 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: Shell script to get a list of all subfolders inside a folder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Shell script to get a list of all subfolders inside a folder


  • Subject: Re: Shell script to get a list of all subfolders inside a folder
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 13 Oct 2015 19:44:33 +1100

On 13 Oct 2015, at 6:28 PM, Bert Groeneveld <email@hidden> wrote:

OSX version is 10.10.5.

So I thought I'd try on my (modest) home folder. Asking the Finder (using "as alias list" in the hope of speeding things up) with a timeout of 6000 seconds got nowhere. 

Using Satimage.osax and this:

set myFolder to (path to home folder)
list files myFolder conforming to {"public.folder"} without invisibles

takes around 9 seconds, but still includes invisible folders.

Using this:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"

set thePath to POSIX path of (path to home folder)
its getFoldersIn:thePath

on getFoldersIn:posixPath
-- make URL
set theNSURL to current application's class "NSURL"'s fileURLWithPath:posixPath
-- make file manager
set theNSFileManager to current application's NSFileManager's new()
-- get URL enumerator
set theNSFileEnumerator to theNSFileManager's enumeratorAtURL:theNSURL includingPropertiesForKeys:{current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value)
-- get all items from enumerator
set allItems to theNSFileEnumerator's allObjects()
set theFolders to {} -- to store folders
-- loop through
repeat with i from 1 to count of allItems
-- is it a directory?
set {theResult, isDirectory} to ((item i of allItems)'s getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value))
if isDirectory as boolean then
set {theResult, isPackage} to ((item i of allItems)'s getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(missing value))
-- is it not a package?
if not isPackage as boolean then
set end of theFolders to (item i of allItems)'s |path|() as «class furl»
end if
end if
end repeat
return theFolders
end getFoldersIn:

takes around 12 seconds.

Adding the BridgePlus scripting library to the mix:

use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use script "BridgePlus" version "1.2"

set thePath to POSIX path of (path to home folder)
its getFoldersIn:thePath

on getFoldersIn:posixPath
-- make URL
set theNSURL to current application's class "NSURL"'s fileURLWithPath:posixPath
-- define the keys you want, and include the path key
set theKeys to {current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey, current application's NSURLPathKey}
-- make file manager
set theNSFileManager to current application's NSFileManager's new()
-- get URL enumerator
set theNSFileEnumerator to theNSFileManager's enumeratorAtURL:theNSURL includingPropertiesForKeys:theKeys options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value)
-- get all items from enumerator
set allItems to theNSFileEnumerator's allObjects()
load framework -- makes sure BridgePlus is loaded
-- get values for the keys as an array of dictionaries
set theInfos to current application's SMSForder's resourceValuesForKeys:theKeys forURLsOrFiles:allItems
log (count of theInfos)
-- filter array to include only directories that are not packages
set thePred to current application's NSPredicate's predicateWithFormat:"(%K == YES) AND (%K == NO)" argumentArray:theKeys -- first %K is first key, etc
set theFolderInfos to theInfos's filteredArrayUsingPredicate:thePred
-- get just the paths from the new array
set thePosixPaths to theFolderInfos's valueForKey:(current application's NSURLPathKey)
set theFolders to {} -- to store folders
-- loop through converting POSIX paths to class furl
repeat with i from 1 to count of thePosixPaths
set end of theFolders to (item i of thePosixPaths) as «class furl»
end repeat
return theFolders
end getFoldersIn:

brings it down to about 4.5 seconds. If you were happy with POSIX paths, it would take less than half that; most of the time is spent building the list of files.

-- 
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: Shell script to get a list of all subfolders inside a folder
      • From: Christopher Stone <email@hidden>
    • Re: Shell script to get a list of all subfolders inside a folder
      • From: Christopher Stone <email@hidden>
References: 
 >Shell script to get a list of all subfolders inside a folder (From: Bert Groeneveld <email@hidden>)
 >Re: Shell script to get a list of all subfolders inside a folder (From: Christopher Stone <email@hidden>)
 >Re: Shell script to get a list of all subfolders inside a folder (From: Bert Groeneveld <email@hidden>)

  • Prev by Date: Re: Folder Type Identifier
  • Next by Date: Re: Shell script to get a list of all subfolders inside a folder
  • Previous by thread: Re: Shell script to get a list of all subfolders inside a folder
  • Next by thread: Re: Shell script to get a list of all subfolders inside a folder
  • Index(es):
    • Date
    • Thread