I’m not new to AppleScript. but I sure don’t script it as much as I used to. No where near as much. which is sad. but nevermind that. let’s get to my problem.
Okay. my first order of business, generate a branching list containing all folders, subfolders,etc and storing it in a single variable. Should be easy right? Well it is. or it should be easy. But nothing comes easy to me anymore. My code.
on run
return getFolderContents(pathStart)
end run
-- getFolderContents handler (Scans thru folders making record of contents)
on getFolderContents(folderName)
local currentFolderRec
set theContents to list folder folderName
set currentFolderRec to {theFolder:{}, theContents:{}}
--
set end of (theFolder of currentFolderRec) to folderName
repeat with theItem in theContents
-- get item type (folder or not) look below...
if getItemType(folderName & theItem) is "folder" then
try -- i should not need a try statement here
-- generates folder path with ":" at end
set newPath to folderName & theItem & ":" as string
-- recursive call to get contents of sub folder
set end of (theContents of currentFolderRec) to {getFolderContents(newPath)}
end try
else
-- Appends document items to content list
set end of (theContents of currentFolderRec) to theItem as string
end if
end repeat
return currentFolderRec
end getFolderContents
-- getItemType handler (returns Item Type) *BAD HACK*
on getItemType(theItem)
(*
There MUST be a better way to get an item type (folder, document, etc)
*)
tell application "Finder"
try
return (class of item theItem) as string
end try
-- I hate this hack.
return "Document"
end tell
end getItemType
The comments should help explain what I’m having difficulty with. but I do have one question that totally baffels me.
try -- i should not need a try statement here
-- generates folder path with ":" at end
set newPath to folderName & theItem & ":" as string
-- recursive call to get contents of sub folder
set end of (theContents of currentFolderRec) to {getFolderContents(newPath)}
end try
The “try” statement is there for one reason. Occasionally it returns an error on a folder that it doesn’t seem to agree if it’s a folder or not. It’s always triggered on the same folder(s). I get a “Bad name for file” error. Even though it’s not a file. What could be causing such an oddity?
If you are operating in 10.4.x, this may be close to what you are looking for:
set a to choose folder --Pick any folder
set {each_Folder, dummy} to {{}, {}} --Create two empty lists, one of which is a dummy, to be used over and over
tell application "Finder"
set allFold to every folder of entire contents of folder a --This will generate every folder (including subfolders) within the chosen folder.
repeat with a_Fold in allFold --Repeat through each one individually
set dummy to the name of every file in a_Fold --Get a list of filenames within each folder
set end of each_Folder to {(a_Fold as Unicode text), dummy} --Now, make a list of the lists. Each item in [each_Folder] contains two items, the string path to the folder, and a list of all filenames within that folder
set dummy to {} --reset the dummy to empty
end repeat
end tell
each_Folder
Let me know if this is close or not.
Hi,
I know it is a list of lists. How would you retrieve the information to make any use of it. I want to take the path which seems to be the first item and join it with each consecutive item in the list, So then I could eventually open the file.
I know the first part of each_Folder is the path and the second part is a list. I can count the items in the second part(List) but it will not let me retrieve them as text, string, unicode text. I even tried as file.
on breakDownLists(each_Folder)
local separateLists
set separateLists to {}
local aLists
set aLists to {}
local theLists
set theLists to {}
local finalList
set finalList to {}
local listCnt
local filePath
local theFile
local fileName
local fileCnt
tell application "Finder"
set listCnt to count every list of each_Folder
log listCnt & " ----------------------------------------- listCnt - each_Folder"
-- go through each list
repeat with a from 1 to listCnt
set AppleScript's text item delimiters to ","
set filePath to item a of each_Folder as list
log filePath & "--------------------------- filePath"
set fileName to item 2 of filePath
log fileName & "----------------------------- fileName - item 2 pf filePath"
set fileCnt to count every item in fileName
log fileCnt & " ------------------------------- fileCnt - each file in filePath"
repeat with b from 1 to fileCnt
set theFile to item b of fileName as text
if theFile = 1 then
set the end of finalPath to filePath & fileName
end if
end repeat
end repeat
log finalPath & "--------------------------------- finalPath - our path eith the file"
(* repeat with b from 1 to listCnt2
set AppleScript's text item delimiters to ","
set fileList to item b of filePath
log dir & "----------------------------------------- dir"
--repeat with c from 1 to listCnt2
end repeat
end repeat
*)
end tell
end breakDownLists