Hello,
in order to show a small panel (possibly a HUB NSPanel) with an outline view whose data is the content of a folder do I need to use AS Studio?
I’d like to have a script rather than a Cocoa application bundle but I’m not sure if it is possible at all.
The only type of outline view you can get in applescript is a choose list dialog. The list can be anything you want such as the file names or paths in a folder.
set theList to {"yes", "no", "maybe"}
choose from list theList with title "Choose From The List" with prompt "PickOne" default items "maybe" OK button name "Select" cancel button name "Quit"
tell result
if it is false then error number -128 -- cancel
set choice to first item
end tell
What I really need is to be able to show a list with all files in a specific folder (that is fixed), choose one of the prompted item and put the content (they will be text files) of the corresponding file into the front window of the calling application. The NSPanel HUD was just for appearance.
-- this path can be adjusted to your needs
set defloc to (path to desktop folder from user domain)
-- set defloc to ("Macintosh HD:A folder:path:" as alias)
set chosenfile to choose file with prompt "Please select a text file:" default location defloc of type "TEXT" without invisibles and multiple selections allowed
set filecont to read chosenfile
No, I prefer a choose from list rather than a standard file dialog.
Grabbing a little code around I’m trying to run this
on getContentAsList(theFolderPath)
set theResult to {}
set theFolder to POSIX file theFolderPath
tell application "Finder"
set theFolders to folders of folder theFolder
set theFiles to files of folder theFolder
end tell
if theFolders is not {} then
repeat with fold in theFolders
set theResult to theResult & getContentAsList(fold)
end repeat
end if
return theResult
end getContentAsList
where theFolderPath is a plain string, but things get a little messy at the recursive call.
Basicallly I want a list of every files so I split the result in two parts: theFiles and theFolders.
theFiles is concatenated to theResult and if theFolders is not empty then for each item in it I concat the result too.
set theFolderPath to path to desktop folder as text
getContentAsList(theFolderPath)
on getContentAsList(theFolderPath)
set theFolder to theFolderPath as text
tell application "Finder"
set theFolders to folders of folder theFolder
set theFiles to files of folder theFolder
end tell
set theResult to {}
repeat with aFolder in theFolders
set end of theResult to aFolder as text
end repeat
repeat with aFile in theFiles
set end of theResult to aFile as text
end repeat
return theResult
end getContentAsList
set theFolderPath to path to desktop folder as text
set theList to getContentAsList(theFolderPath)
set allowedFileTypes to {"TEXT"}
on getContentAsList(theFolderPath)
set theFolder to theFolderPath as text
tell application "Finder"
set theFolders to folders of folder theFolder
set theFiles to files of folder theFolder
end tell
set theResult to {}
repeat with aFolder in theFolders
set end of theResult to aFolder as text
end repeat
repeat with aFile in theFiles
set temp_file_type to file type of aFile
if allowedFileTypes contains temp_file_type then set end of theResult to aFile as text
end repeat
return theResult
end getContentAsList
the allowedFileTypes variable is not defined into the function, how can set a global scope for it?
I see paths are old-style mac paths, I tried using POSIX path but got a little confused.
Applescript always uses old-style mac paths. The only place in applescript to use posix paths is with the “do shell script” command.
Here’s how to pass that variable… just add it into the handler.
set theFolderPath to path to desktop folder as text
set allowedFileTypes to {"TEXT"}
set theList to getContentAsList(theFolderPath, allowedFileTypes)
on getContentAsList(theFolderPath, allowedFileTypes)
set theFolder to theFolderPath as text
tell application "Finder"
set theFolders to folders of folder theFolder
set theFiles to files of folder theFolder
end tell
set theResult to {}
repeat with aFolder in theFolders
set end of theResult to aFolder as text
end repeat
repeat with aFile in theFiles
set temp_file_type to file type of aFile
if allowedFileTypes contains temp_file_type then set end of theResult to aFile as text
end repeat
return theResult
end getContentAsList
Back to the topic:
would it be possible to launch a Cocoa app with the NSPanel choose an item and get the result back into the launching script?
Or should I rather launch a second script from the Cocoa app?
The choose list is fine but has a flat structure so to browser subdirectory I’m showing Folder:SubFolder:File in list while I’d like something visually more explicative.
the app puts its result on the clipboard or writes it to a text file
when your script sees that your app is finished it grabs the result from the clipboard or the text file and continues
But if you want something more complete then the ‘choose folder’ dialog was a good suggestion. If that’s not good enough then it would probably make the most sense to just do the whole thing in an AS Studio.
Yes, probably choose file makes sense but this is a script I use in TextWrangler so I wanted a tiny dialog, i.e. without “Places” and other things, but this could get complicated, it’s better to keep it simple and proceed with what is already present.