• 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: Script that "Tells" Finder just Focuses on Finder instead of running
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script that "Tells" Finder just Focuses on Finder instead of running


  • Subject: Re: Script that "Tells" Finder just Focuses on Finder instead of running
  • From: Shane Stanley <email@hidden>
  • Date: Tue, 06 Jan 2015 18:30:21 +1100

On 6 Jan 2015, at 12:44 pm, Shane Stanley <email@hidden> wrote:

But again, formatting the results as Alex wanted still seems a fair bit of work.

Or maybe I'm just being lazy. So here's a script that does the recursion itself, which seems to me to make the formatting easier. The output format is like this:

Folder '/Users/shane' contains 3 files and 18 folders:
Shrink PDF.app
System Events.h
System Events.sdef
Folder '/Users/shane/Applications' contains 1 files and 2 folders:
Ingredients.app
...

use scripting additions
use framework "Foundation"

-- this where we'll store the stuff as we collect it; we could use a list, but it could get very long
set finalNSArray to current application's NSMutableArray's array()
-- make NSURL of where to start
set thePath to POSIX path of (path to applications folder)
set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath
-- call our handler
my listURL:anNSURL inArray:finalNSArray
-- join the strings in the array, and convert it to an AS string
return (finalNSArray's componentsJoinedByString:linefeed) as text

on listURL:anNSURL inArray:finalNSArray
-- get file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- define options to use
set enumerationOptions to current application's NSDirectoryEnumerationSkipsHiddenFiles
-- get list of items in directory
set theItems to (theNSFileManager's contentsOfDirectoryAtURL:anNSURL includingPropertiesForKeys:{(current application's NSURLIsPackageKey), (current application's NSURLIsDirectoryKey)} options:enumerationOptions |error|:(missing value)) as list
-- create lists to store results; it might be quicker to use arrays, but let's keep it simple
set theFiles to {}
set theFolders to {}
-- loop through the items
repeat with anItem in theItems
-- is it a directory?
set {theResult, theValue} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsDirectoryKey) |error|:(missing value))
if theValue as boolean then
-- is it a package?
set {theResult, theValue} to (anItem's getResourceValue:(reference) forKey:(current application's NSURLIsPackageKey) |error|:(missing value))
if not theValue as boolean then
set end of theFolders to anItem
else -- it's a file, so store name
set end of theFiles to anItem's |path|()'s lastPathComponent()
end if
else -- it's a file, so store name
set end of theFiles to anItem's |path|()'s lastPathComponent()
end if
end repeat
-- add folder line
(finalNSArray's addObject:(current application's NSString's stringWithFormat_("Folder '%@' contains %@ files and %@ folders:", anNSURL's |path|(), count of theFiles, count of theFolders)))
-- add list of filenames
(finalNSArray's addObjectsFromArray:theFiles)
-- now do the same thing with each of the folders
repeat with anNSURL in theFolders
(my listURL:anNSURL inArray:finalNSArray)
end repeat
end listURL:inArray:

Whereas my simple previous script took about a second, this takes 15. On the other hand, if I just run a rough shell equivalent:

do shell script "ls -R /Applications"

It takes about 27 seconds. Obviously my script saves time by skipping packages, and /Applications is full of them. Still, that's not too shabby.

I then tried them both on my home folder: the above took about 21 seconds, versus 18 for the ls -R shell script. Again, I may have more packages than most, but I reckon that's a pretty small margin to pay for eliminating the contents of packages, and slightly nicer formatting. It could probably be tweaked a bit more, too.

It would also be trivial to include a counter, so that when finalNSArray exceeds your limit, it posts a dialog and quits (or gives you an option to continue or cancel).

-- 
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: Script that "Tells" Finder just Focuses on Finder instead of running
      • From: Yvan KOENIG <email@hidden>
References: 
 >Script that "Tells" Finder just Focuses on Finder instead of running (From: Alex Hall <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Deivy Petrescu <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: 2551 <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Alex Hall <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Deivy Petrescu <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Shane Stanley <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Deivy Petrescu <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Shane Stanley <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Deivy Petrescu <email@hidden>)
 >Re: Script that "Tells" Finder just Focuses on Finder instead of running (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Script that "Tells" Finder just Focuses on Finder instead of running
  • Next by Date: Re: Script that "Tells" Finder just Focuses on Finder instead of running
  • Previous by thread: Re: Script that "Tells" Finder just Focuses on Finder instead of running
  • Next by thread: Re: Script that "Tells" Finder just Focuses on Finder instead of running
  • Index(es):
    • Date
    • Thread