Tuesday, March 28, 2017

#1 2015-12-04 12:15:13 am

Shane Stanley
From: Australia
Registered: 2002-12-07
Posts: 4963

Listing folder contents

One of the things I regularly want to do is see the contents of a folder, fully expanded, but with full paths. I've written scripts to do it before, but sometimes the full paths are distracting. I finally got around to writing one that shows the paths, but with all but the name in a lighter color. The listing gets saved to the desktop as an rtf file, and opened in the default rtf editor. It's not perhaps the most obvious of code, but some might find it useful.

It requires the BridgePlus script library (download here: www.macosxautomation.com/applescript/ap … ePlus.html), although it could easily be modified to do without it. Because it's potentially doing a lot of styling, don't point it at very large folders and expect to see anything in a hurry.

It works on the target of the frontmost Finder window, and if you hold the control key while launching it, it includes the contents of packages.

It requires at least OS X 10.9 (you'll need to put it in a script library under Mavericks).


use AppleScript version "2.3.1"
use scripting additions
use framework "Foundation"
use framework "AppKit"
use script "BridgePlus" version "1.3.1"

set doPackages to not (my checkModifier:"control") -- if control key is down, include contents of packages

tell application "Finder"
       set theTarget to target of Finder window 1 as alias
   on error
       error number -128
   end try
end tell

set Forder to load framework

-- set up destination path
set deskPath to POSIX path of (path to desktop)
set destURL to (current application's class "NSURL"'s fileURLWithPath:deskPath)'s URLByAppendingPathComponent:"_WindowContents.rtf" -- change to suit
-- set up paragraph style for line-spacing
set parStyle to current application's NSParagraphStyle's defaultParagraphStyle()'s mutableCopy()
parStyle's setLineHeightMultiple:1.3 -- change to suit
-- set up font
set theFont to current application's NSFont's fontWithName:"Menlo-Regular" |size|:14.0 -- change to suit
-- set up colors
set highlightColor to current application's NSDictionary's dictionaryWithObject:(current application's NSColor's blueColor()) forKey:(current application's NSForegroundColorAttributeName) -- change to suit
set lighterColor to current application's NSDictionary's dictionaryWithObject:(current application's NSColor's lightGrayColor()) forKey:(current application's NSForegroundColorAttributeName) -- change to suit
-- define base style from above
set baseStyle to current application's NSMutableDictionary's dictionaryWithObjects:{theFont, parStyle} forKeys:{current application's NSFontAttributeName, current application's NSParagraphStyleAttributeName}
baseStyle's addEntriesFromDictionary:lighterColor

-- get all items in folder
set allPaths to Forder's itemsIn:theTarget recursive:true skipHidden:true skipInsidePackages:doPackages asPaths:true
-- match Finder sorting
set allPaths to allPaths's sortedArrayUsingSelector:"localizedStandardCompare:"
-- make string of paths
set theString to allPaths's componentsJoinedByString:linefeed
set theString to theString's stringByAppendingString:linefeed
-- get range of last component of paths
set theRegex to current application's NSRegularExpression's regularExpressionWithPattern:"[^\\/]+\\n" options:0 |error|:(missing value)
set theMatches to (theRegex's matchesInString:theString options:0 range:{0, theString's |length|()})'s valueForKey:"range"
-- make styled string
set styledString to current application's NSMutableAttributedString's alloc()'s initWithString:theString attributes:baseStyle
-- add highlightColor to the names
repeat with aRange in theMatches
   (styledString's addAttributes:highlightColor range:aRange)
end repeat
-- get RTF data, write it to file, then open the file
set theData to styledString's RTFFromRange:{0, styledString's |length|()} documentAttributes:(missing value)
theData's writeToURL:destURL atomically:true
current application's NSWorkspace's sharedWorkspace()'s openURL:destURL

on checkModifier:keyName
   if keyName = "option" then
       set theMask to current application's NSAlternateKeyMask as integer
   else if keyName = "control" then
       set theMask to current application's NSControlKeyMask as integer
   else if keyName = "command" then
       set theMask to current application's NSCommandKeyMask as integer
   else if keyName = "shift" then
       set theMask to current application's NSShiftKeyMask as integer
       return false
   end if
   set theFlag to current application's NSEvent's modifierFlags() as integer
   if ((theFlag div theMask) mod 2) = 0 then
       return false
       return true
   end if
end checkModifier:

Edited to include Finder-style sorting

Last edited by Shane Stanley (2015-12-04 06:25:16 am)

Shane Stanley <sstanley@myriad-com.com.au>



Board footer

Powered by FluxBB

[ Generated in 0.075 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)