I’m trying to come up with an AppleScript (OSX 10.3) to get the filenames from a folder, and simply save them as a text list. This sounds like it ought to be simple, but I’m having a heck of a time trying to make it happen. Can anyone help me out?
Alternately (though this is probably a pipe dream) i’d like to match up the names in an excel (or exported to tab-delimited text) file with the names of the photos in a folder with a bunch of office subfolders. Wouldn’t that be nice?
I suspect I’m going to end up copying and pasting though… if I can just get the directory to save as text!
Thanks for your time,
-Stryck-
Model: G5
AppleScript: 1.9.3
Browser: Firefox 1.0.7
Operating System: Mac OS X (10.3.9)
It is pretty simple using the Finder. This script will put all the filenames into a list:
set a to choose folder
tell application "Finder"
set b to the name of every file in folder a
end tell
Now that all the filenames are in a list, you can do pretty much whatever you want with them. For instance, this will create a TextEdit document with each filename on its own line:
set d to ""
repeat with c from 1 to count b--This is the list produced above, so this script needs to be appended to the script above.
set d to d & (item c of b) & return
end repeat
tell application "TextEdit"
activate
make new document at the front
set text of front document to d
end tell
And really, the sky is the limit. Once you have your data in a list, you can put them into a spreadsheet, database, whatever. If you want to reference each item with another existing file or folder, that is also possible, but a little more time consuming.
Vincent is correct! UNIX is our friend. Unfortunately, I am still a UNIX neophyte, so here is what I hope you were looking for to see all the subfolders:
set f to choose folder with prompt "Choose a folder to summarise:"
set d to text item delimiters
set text item delimiters to return
tell application "Finder" to set {n, t} to {f's name, paragraphs of (folders of f's entire contents as Unicode text)}
set text item delimiters to d
repeat with af from 1 to count t
tell application "Finder" to set gf to the name of every file in folder (item af of t)
set dd to ""
repeat with c from 1 to count gf
set dd to dd & (item c of gf) & return
end repeat
MakeTextFile(dd, (item af of t))
end repeat
on MakeTextFile(fn, tt)
set fnt to "Folder: " & tt & return & return & fn
tell application "TextEdit"
make new document at the front
set text of front document to fnt
end tell
end MakeTextFile
This will create a whole bunch of TextEdit files, (some of which will be empty) with the list of each filename in each folder and subfolder, along with the path to that folder as the document header.
I forgot to give credit where credit is due. The power portion that script (getting the list of all the subfolders in a folder) was provided by Kai, the great scripter, in another thread.
Unfortunately, Vincent, my Terminal is wonky… all you can see is basically the red button on any open terminal window, and I can’t get it to resize to a normal window.
I tried the applescript one from casdvm, and it put them all in separate textfiles. Is there a way to append them all into one, with the folder name included?
I know; I’m so demanding!
But thank you both for helping me so far!
-S-
I just shuffled the commands around a little bit, try this:
set fnt to ""
global fnt
set f to choose folder with prompt "Choose a folder to summarise:"
set d to text item delimiters
set text item delimiters to return
tell application "Finder" to set {n, t} to {f's name, paragraphs of (folders of f's entire contents as Unicode text)}
set text item delimiters to d
repeat with af from 1 to count t
tell application "Finder" to set gf to the name of every file in folder (item af of t)
set dd to ""
repeat with c from 1 to count gf
set dd to dd & (item c of gf) & return
end repeat
MakeTextFile(dd, (item af of t))
end repeat
tell application "TextEdit"
make new document at the front
set text of front document to fnt
end tell
on MakeTextFile(fn, tt)
set fnt to fnt & "Folder: " & tt & return & fn & return & return
end MakeTextFile
Here’s my two cents… A little droplet to drop your folder(s) on which will make one TextEdit file per folder …
on open srcFolder
set myPath to (path to desktop as string) -- Change this as needed, Desktop is quick!
repeat with theSource in srcFolder
set fileList to {} -- Flush the FileList each iteration.
tell application "Finder"
set myFile to (myPath & (name of theSource & " Listing")) -- Set up the filename
set folderList to list folder (theSource as string) without invisibles -- Ignore the .DSStore files
repeat with thisItem in folderList
copy ((thisItem as string) & return as string) to end of fileList -- Push the names into a list
end repeat
set fileList to fileList as string -- Convert the list to a string
open for access myFile with write permission -- This creates a file. Nice little AS trick.
write fileList to alias myFile
close access alias myFile
open alias myFile -- Tada!!!
end tell
end repeat
end open
This will list EVERYTHING in the folder (not subfolders though… recursion is a whole other issue!) but you can change the “list folder” to “every file of…” It would be fairly easy to push the code into a handler and give you the option of making individual text files or one BIG text file. Have fun.
property fileName : "File List.txt"
on run
choose folder with prompt "Get list of files from this folder:"
open {result}
end run
on open droppedItems
set ASTID to AppleScript's text item delimiters
tell application "Finder" to launch
repeat with thisItem in droppedItems
if (folder of (info for thisItem without size)) is true then
set AppleScript's text item delimiters to (ASCII character 13)
tell application "Finder" to set theList to entire contents of thisItem as text
set AppleScript's text item delimiters to ""
try
open for access file ((thisItem as text) & fileName) with write permission
set theFileRef to result
write theList to theFileRef
close access theFileRef
on error errorMsg number errorNum
try
close access theFileRef
end try
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "OK" default button 1 with icon caution
end try
end if
end repeat
set AppleScript's text item delimiters to ASTID
return true
end open
Or this:
property fileName : "File List.txt"
on run
choose folder with prompt "Get list of files from this folder:"
open {result}
end run
on open droppedItems
repeat with thisItem in droppedItems
if (folder of (info for thisItem without size)) is true then
get POSIX path of thisItem
try
do shell script "find " & quoted form of (text 1 through -2 of result) & " -not -iname .\\* > " & quoted form of (result & fileName)
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "OK" default button 1 with icon caution
end try
end if
end repeat
return true
end open
Thanks to all of you! I would have responded sooner but I was out sick for a few days. (lots of days. ugh!)
Are there any good books or websites that teach AppleScript basics so that I don’t have to bother all you nice people every time I have a question? I have some basic programming knowledge, in BASIC (heh) and C++ but I don’t remember much other than Hello, World!
I tried looking through the dictionary for finder, but I guess I was interpreting things wrong as to what they do. (Like “entire folder” seems like it should do something but it doesn’t!)
-Stryck-
Model: G5
AppleScript: 1.9.3
Browser: Firefox 1.0.7
Operating System: Mac OS X (10.3.9)
My favorite AppleScript book is Beginning Applescript by Steve Kochan. ( ISBN: 0-7645-7400-0) It is extremely well-written and informative. You can download all his example scripts and really get a solid handle on scripting.
I also use the book by Haanan Rosenthal (ISBN: 1590594045) from time to time.