Formatting text returned from shell script
Formatting text returned from shell script
- Subject: Formatting text returned from shell script
- From: Mr Tea <email@hidden>
- Date: Wed, 19 Mar 2003 00:54:36 +0000
In response to an enquiry on an unrelated list, I knocked up a script that
lists the contents of a nested folder hierarchy with items indented to
indicate how deeply they are nested. The desired end product needed to look
something like this (without the dashes at start of lines):
-Main folder name
-
-    Folder:  >  Subfolder 1
-                >  File
-                >  File
-                >  File
-        Folder:  >  sub-subfolder 1
-                    >  File
-                    >  File
-                    >  File
-    Folder:  >  subfolder 2
-                >  File
-                >  File
-                >  File
etc...
To do this, I used a shell script (*gasp*) to list the contents of the
target folder recursively, and then formatted the text in AppleScript. As
usual, I didn't so much craft the script as beat whatever raw materials came
to mind into shape with a large club. It works, but it ain't fast on a big
folder with 6000+ items nested in it, and I'm sure it could be prettier.
Any suggestions for optimisations or alternative routes to the same end
would be warmly received. Here's the script. I've formatted it so that only
broken lines abut the left edge of the message window. These should be
appended to the previous line.
--========================
    choose folder with prompt "Choose a folder to process..." returning
theFolder  
    set theName to name of (info for theFolder)
    set thepath to quoted form of POSIX path of theFolder
    set currentIndex to theFolder as string
    do shell script "ls -R " & thepath returning theDir
    set theDirList to every paragraph of theDir
      
    set newList to {"Contents of folder \"" & theName & "\"" & return &
thepath & return & return}
    set theFilePrefix to ""
    repeat with i from 1 to count of theDirList
      set theLine to item i of theDirList
      set my text item delimiters to "/"
      set theMarker to count of text items of thepath
      set theCount to count of text items of theLine
      set currentFolder to text item -1 of theLine
      set theFolderPrefix to ""
      if theCount is greater than theMarker then
        set theNestIndex to theCount - theMarker
        set theTally to -1
        set theFilePrefix to ""
        set theSuffix to ""
        repeat theNestIndex times
          set theFolderPrefix to theFolderPrefix & tab
          set theFilePrefix to theFilePrefix & tab
          if theTally is -1 then
            set theSuffix to text item theTally of theLine & theSuffix
          else  
            set theSuffix to text item theTally of theLine & ":" & theSuffix
          end if  
          set currentIndex to "" & theFolder & theSuffix
          set theTally to theTally - 1
        end repeat 
      end if  
      set my text item delimiters to ""
      if theLine is not "" then
        if word 1 of theLine is "Volumes" then
          set end of newList to theFolderPrefix & "Folder:    >  " &
currentFolder & return
        else  
          try  
            if not folder of (info for alias (currentIndex & theLine & ":"))
then  
              set end of newList to theFilePrefix & tab & tab & tab & ">  "
& theLine & return 
            end if 
          end try  
        end if  
      end if  
    end repeat  
      
    open for access file ((path to desktop as string) & "Contents of folder
- " & theName) with write permission returning theFile
    write (newList as string) to theFile
    close access theFile
--========================
TIA for any input.
Nick
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: 
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.