i’m having trouble getting this script to work how i want it to. It’s supposed to get the contents of a chosen folder and list them in a text document. The script needs to get Files and Folders. It does mostly what i want, it gets files and folders and puts them into the text doc. the problem is that it lists all the items in the chosen folder, then all the items that are in the subfolders.
i need it to list each folder then its contents, then the next folder and its contents and so on.
here is what i have so far:
tell application “Finder”
set myNames to the name of items of (entire contents of folder (choose folder with prompt “choose source folder:”))
set file_list to myNames
tell application "TextEdit"
activate
try
make new document at the beginning of documents
set the name of window 1 to "File List"
set the item_count to the count of the file_list
set this_text to ""
repeat with i from 1 to the item_count
set this_item to item i of the file_list
set this_text to this_text & this_item & return
end repeat
set the text of the front document to this_text
repeat with i from 1 to the item_count
repeat with x in paragraphs of document 1
if x does not contain "." then
set font of x to "Helvetica Bold"
set color of x to {65535, 0, 0}
end if
end repeat
end repeat
end try
end tell
set MasterFolder to choose folder
tell application "Finder"
--Reference to a folder
set FolderRef to MasterFolder as string
--Gets contents of folder ready for processing
set FolderContents to items of folder FolderRef
end tell
set file_list to {}
repeat with thisFile in FolderContents
set theFilename to name of thisFile
set file_list to file_list & theFilename
end repeat
tell application "TextEdit"
try
--Removed activate, which was causing a blank document to open with new file
make new document at the beginning of documents
set the name of window 1 to "File List "
set the item_count to the count of the file_list
set this_text to ""
repeat with i from 1 to the item_count
set this_item to Abstract object i of the file_list
set this_text to this_text & this_item & return
end repeat
set the text of the front document to this_text
repeat with i from 1 to the item_count
repeat with x in paragraphs of document 1
if x does not contain "." then
set font of x to "Helvetica Bold"
set color of x to {65535, 0, 0}
end if
end repeat
end repeat
end try
end tell
That gets the list of folder items, but your script doesn’t save the file. Is that your goal?
SC
I am not running the latest version of AppleScript, but the script works on my system. I used your existing code and just gave you the handler for file/ foldernames. Now that I take a look at your text handler, I see it could be simplified:
set MasterFolder to choose folder
tell application "Finder"
set MastFolderName to name of MasterFolder
--Reference to a folder
set FolderRef to MasterFolder as string
--Gets contents of folder ready for processing
set FolderContents to items of folder FolderRef
end tell
set file_list to {}
repeat with thisFile in FolderContents
set theFilename to name of thisFile
set file_list to file_list & theFilename
end repeat
tell application "TextEdit"
try
make new document at the beginning of documents
set the name of window 1 to "File List "
set this_text to ""
repeat with thisFile in file_list--You don't need an item count to end the repeat here
set this_text to this_text & thisFile & return
end repeat
set the text of the front document to this_text
repeat with i from 1 to the item_count
repeat with x in paragraphs of document 1
if x does not contain "." then
set font of x to "Helvetica Bold"
set color of x to {65535, 0, 0}
end if
end repeat
end repeat
end try
end tell
That could be done by recursion through the folders, but I think it’d generally be faster to use ‘entire contents’ and then doctor the paths. Here’s a vanilla attempt, cobbled together from odd bits I have lying around. The file search and sort might (possibly) be faster in Unix, but I don’t know how to do that. It’s the colouring of the folder names that takes most of the time, though.
main()
on main()
-- Get a list of edited path strings (as a property of a script object).
set o to parseContents(choose folder)
-- Join the paths into a single, line-feed delimited text.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ((ASCII character 10) as Unicode text)
set theText to o's pathList as Unicode text
-- Create a TextEdit document containing the text and colour the folder names.
set redHeader to {"Helvetica Bold", {65535, 0, 0}}
set colon to ":" as Unicode text
tell application "TextEdit"
make new document at beginning with properties {text:theText}
set font of text of front document to "Helvetica"
considering case
repeat with i from 1 to (count o's pathList)
if (item i of o's pathList ends with colon) then
tell paragraph i of front document to set {its font, its color} to redHeader
end if
end repeat
end considering
end tell
tell application (path to frontmost application as Unicode text)
beep
display dialog "Done!" buttons "OK" default button 1 with icon note giving up after 10
end tell
end main
on parseContents(rootPath)
script o
property pathList : missing value
end script
-- Get the entire visible contents of the disk/folder as a list of Mac OS paths.
set rootPath to rootPath as Unicode text
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ((ASCII character 10) as Unicode text)
tell application "Finder"
set pathString to every item of entire contents of folder rootPath as Unicode text
end tell
set o's pathList to (get text items of pathString)
set beginning of o's pathList to rootPath
-- Sort the list lexically, which arranges each subfolder before the items it contains.
considering case
qsort(o's pathList, 1, -1)
end considering
-- Prepare a string of tabs.
set AppleScript's text item delimiters to ""
set tabSource to {tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab, tab} as Unicode text
-- Replace each item's container path with the equivalent number of tabs.
set colon to ":" as Unicode text
set AppleScript's text item delimiters to colon
repeat with i from 2 to (count o's pathList)
set ti to text items of (item i of o's pathList)
set tic to (count ti)
set pathEnd to end of ti
if ((count pathEnd) is 0) then -- the path ends with a colon
set item i of o's pathList to text 1 thru (tic - 2) of tabSource & (item (tic - 1) of ti) & colon
else
set item i of o's pathList to text 1 thru (tic - 1) of tabSource & pathEnd
end if
end repeat
set AppleScript's text item delimiters to astid
-- Return the script object containing the edited list.
return o
end parseContents
on qsort(theList, l, r) -- Knapp/Garvey combined, in-place Quicksort & insertion sort
script o
property cutoff : 10
property p : theList
on qsrt(l, r)
set i to l
set j to r
set v to my p's item ((l + r) div 2)
repeat while (j > i)
set u to my p's item i
repeat while (u < v)
set i to i + 1
set u to my p's item i
end repeat
set w to my p's item j
repeat while (w > v)
set j to j - 1
set w to my p's item j
end repeat
if (i > j) then
else
set my p's item i to w
set my p's item j to u
set i to i + 1
set j to j - 1
end if
end repeat
if (j - l < cutoff) then
else
qsrt(l, j)
end if
if (r - i < cutoff) then
else
qsrt(i, r)
end if
end qsrt
on isrt(l, r)
set x to l
set z to l + cutoff - 1
if (z > r) then set z to r
set v to my p's item x
repeat with y from (x + 1) to z
if (my p's item y < v) then
set x to y
set v to my p's item y
end if
end repeat
tell my p's item l
set my p's item l to v
set my p's item x to it
end tell
set u to my p's item (l + 1)
repeat with i from (l + 2) to r
set v to my p's item i
if (v < u) then
set my p's item i to u
repeat with j from (i - 2) to l by -1
if (v < my p's item j) then
set my p's item (j + 1) to my p's item j
else
set my p's item (j + 1) to v
exit repeat
end if
end repeat
else
set u to v
end if
end repeat
end isrt
end script
set listLen to (count theList)
if (listLen > 1) then
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1
if (r = l) then
else
if (l > r) then
set temp to l
set l to r
set r to temp
end if
if (r - l < o's cutoff) then
else
o's qsrt(l, r)
end if
o's isrt(l, r)
end if
end if
return -- nothing
end qsort
choose folder
set theFolder to (characters 1 through -2 of (POSIX path of result)) as text
set firstLine to quoted form of (theFolder & ":" & (ASCII character 10))
do shell script "echo " & firstLine & " \"`ls -pR1 " & quoted form of theFolder & "`\" | " & ¬
"perl -p -e 's:\\n:\\n\\t:g' | perl -p -e 's:\\t/:/:g'"
set theList to (lines 1 through -2 of result) as text
tell application "TextEdit"
make new document at beginning with properties {text:theList}
tell front document
set color of every paragraph whose character -2 is ":" to {65535, 0, 0}
end tell
activate
display dialog "Done!" buttons "OK" default button 1 giving up after 10
end tell
Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)
I have tried the below script - quite successfully, but how can I get it to loop, opening the CD tray at the start of each loop, and append the new info to an already open file??