Apologies to those that read this in the MacOS forum - my fault for putting it in the wrong place.
Firstly, I am a complete newbie so please be gentle. Secondly, apologies if this has been asked before but I have had a quick search through the forum and not found what I am looking for.
I have a question on ‘choose from list’
On the choose from list dialog box, can you only ever have two buttons, not three like you can have on other dialog boxes? The reason I ask is that I want the user to be able to choose something from the list but if it is not there to be able to add something. So really I would like three buttons: ‘OK’, ‘cancel’ and ‘add’. I have developed a workaround by adding ‘…add item’ to the top of my list and if the user selects that it runs the script to add to the list but it’s not as neat and tidy.
My list is to be a list of folders on my mac. But when I compile the list, it includes invisible files and other files. How do I filter it so it is just folders? I thought this might work but I get an error (it compiles OK):
set choose_list to {“…add item”} & ((list folder “Macintosh HD:users:jontyhunter:desktop:script test”) whose kind is folder)
The error message reads as:
Can’t get {“.DS_Store”, “four”, “one”, “three”, “two”} whose folder of kind = true.
with the standard tools Choose file has only two buttons.
Your trick is fine but your coding fails.
I replaced your single instruction by:
set myFolder to (((path to desktop) as Unicode text) & "script test") as alias
tell application "Finder" to set myList to folders of myFolder
set choose_list to {"...add item"} & myList
and it works.
Yvan KOENIG (from FRANCE mercredi 8 novembre 2006 13:11:20)
set myFolder to (((path to desktop) as Unicode text) & “script test”) as alias
tell application “Finder” to set myList to folders of myFolder
set choose_list to {“…add item”} & myList
set which_folder to choose from list choose_list with prompt “Which folder?” default items {“…add item”}
if which_folder is {“…add client”} then
set new_folder to text returned of (display dialog “New folder?” default answer “”)
tell application “Finder”
make new folder at “Macintosh HD:users:jthunter:desktop:script test” with properties {name:new_folder}
end tell
end if
But when I run it I get this error:
Can’t make «class cfol» “four” of «class cfol» “script test” of «class cfol» “Desktop” of «class cfol» “jthunter” of «class cfol» “Users” of «class sdsk» of application “Finder” into the expected type.
The error comes at the line “set which_folder to choose from list…”
The trick is that your list can only contain names to be meaningful.
So you have to convert back to Finder aliases.
The following script deos work well for me
set myFolder to (((path to desktop) as Unicode text)) as alias
tell application "Finder" to set myList to get (name of folders of myFolder)
set choose_list to myList & {"...add item"}
set which_folder to choose from list choose_list with prompt "Which folder?" default items {"...add item"}
if "...add item" is in which_folder then
set new_folder to text returned of (display dialog "New folder?" default answer "")
tell application "Finder"
set theF to make new folder at "Macintosh HD:users:jthunter:desktop:script test" with properties {name:new_folder}
end tell
end if
if which_folder is false then return
tell application "Finder" to set theF to every folder of myFolder whose name is which_folder
theF
Others may be able to explain this more clearly, but…
This line in your script:
tell application "Finder" to set myList to folders of myFolder
returns Finder references, which look like this:
you need a string for a choose from list dialog. You could convert the reference to an alias as string:
set myFolder to (((path to desktop) as Unicode text) & "script test") as alias
tell application "Finder" to set aliasList to folders of myFolder as alias list
set chooseList to {}
repeat with anAlias in aliasList
set anAlias to anAlias as string
set end of chooseList to anAlias
end repeat
set end of chooseList to "...add item"
set chooseFolder to choose from list chooseList with prompt "Which Folder" default items {"...add items"}
but the choose dialog will be cluttered because an alias looks like:
so to make it easier to read, the solution is to choose from a list of names of the folders, as Eelco did:
tell application "Finder" to set myList to get (name of folders of myFolder)
which looks like:
but you can’t navigate directly to a folder with only a name, so here Eelco uses it to get a reference:
tell application "Finder" to set theF to every folder of myFolder whose name is which_folder¨
If you want an alias, change the line to:
tell application "Finder" to set theF to (every folder of myFolder whose name is which_folder) as alias
If you haven’t already, click “Event Log” before running a script in the editor to get a better idea of what is happening.
I started to answer this earlier, Jonty ” but was suddenly called away before I could post it. While I see that you’ve now had some excellent replies, I’ll throw my response into the hat anyway, just for good measure…
Button limit of ‘choose from list’
I’m afraid the ‘choose from list’ command does indeed offer only 2 buttons.
To work around the limitation, I often use the technique you describe earlier (except that I usually insert additional items at the end of the list). If the chance of a user cancellation is unlikely, another approach might be to insert the ‘cancel’ option in the list instead. A third possibility is to treat an empty selection as a cancellation. (See below for examples.)
Filtering items using ‘list folder’
By default, the ‘list folder’ command returns invisible items. You can, however, elect to ignore these by using the command’s ‘invisibles’ parameter. However, ‘list folder’ has no built-in method of filtering out the names of any visible files. This would normally require some additional processing (typically using a repeat loop) to check whether each item is a file or a folder.
Filtering items using Finder
Alternatively (and this would be my preferred approach for such a situation), you could use Finder (which doesn’t return invisible items anyway) to filter by class ” such as folder.
Finder and the desktop object
In addition, because Finder’s default object is the desktop, there’s no need to even specify a path when targeting a desktop object.
Examples
set choose_list to (list folder (path to desktop as Unicode text) & "script test" without invisibles) & "¢ add item."
set chosen_item to choose from list choose_list
if chosen_item is false then error number -128 (* cancel *)
set chosen_item to chosen_item's beginning
if chosen_item is "¢ add item." then
"your add item routine here."
else
"do something with " & chosen_item & "."
end if
tell application "Finder" to set choose_list to name of folder "script test"'s folders & "¢ add item."
set chosen_item to choose from list choose_list
if chosen_item is false then error number -128 (* cancel *)
set chosen_item to chosen_item's beginning
if chosen_item is "¢ add item." then
"your add item routine here."
else
"do something with " & chosen_item & "."
end if
tell application "Finder" to set choose_list to name of folder "script test"'s folders & "¢ Cancel"
set chosen_item to choose from list choose_list cancel button name "Add Item."
if chosen_item is false then
"your add item routine here."
else
set chosen_item to chosen_item's beginning
if chosen_item is "¢ Cancel" then
error number -128 (* cancel *)
else
"do something with " & chosen_item & "."
end if
end if
tell application "Finder" to set choose_list to name of folder "script test"'s folders
set chosen_item to choose from list choose_list with prompt "To cancel, select no items:" cancel button name "Add Item." with empty selection allowed
if chosen_item is false then
"your add item routine here."
else
if (count chosen_item) is 0 then
error number -128 (* cancel *)
else
"do something with " & chosen_item's beginning & "."
end if
end if
Cause of the error
Now let’s take a look at that subsequent issue, where you were getting an error starting: Can’t make «class cfol» “four” of «class cfol» “script test”…
The script began:
set myFolder to (((path to desktop) as Unicode text) & "script test") as alias
tell application "Finder" to set myList to folders of myFolder
set choose_list to {"...add item"} & myList
Now, if you run just that part of the script, you’ll see that the resulting list contains a string (“…add item”), followed by a number of Finder references. On your machine, this would probably look something like:
The problem arose in the very next statement…
set which_folder to choose from list choose_list with prompt "Which folder?" default items {"...add item"}
… from which the troublesome expression is:
choose from list choose_list
This results in a coercion failure (error number -1700).
Avoiding the coercion error
The ‘choose from list’ command typically expects a list of text (plain or unicode) and/or numbers (integer or real). If the list contains some other class, an attempt is made to coerce it to an appropriate class. However, as the error message suggests, the coercion of a Finder reference fails.
Sidebar: what the error message is really saying:
Eelco’s version (as well as the examples given above) gets folder names, rather than their references. So instead of something like…
tell application "Finder" to set myList to folders of folder "script test" ”> list of Finder references
… we need to use something more like:
tell application "Finder" to set myList to name of folders of folder "script test" ”> list of Unicode text
Translating a chosen name back to a reference
Having resolved the issues causing the error, we can now turn to what your script might need to do next. Eelco anticipated this by suggesting a way of converting the chosen name back into a Finder reference - so that the script can then do something with the folder of that name:
tell application "Finder" to set theF to every folder of myFolder whose name is which_folder
Note that this actually returns a list. I’d suggest a minor tweak, depending on whether or not the ‘choose from list’ form allows multiple selections: With multiple selections allowed
tell application "Finder" to set the_folders to myFolder's folders whose name is in which_folder ”> list of reference
Without multiple selections allowed
tell application "Finder" to set the_folder to myFolder's first folder whose name is which_folder ”> reference
Sorry if all this “goes on” a bit ” but I hope it helps to explain some of issues involved here.
I’m guessing I’m pushing my luck but I guess there is no way to make the “…add item” bit appear in the list in italics? No? Thought not. Oh well.
Also, can you specify the positions of dialog buttons? If you have three, it puts one to the left, one in the middle and one to the right which I don’t like the look of. Can they not be all arranged right like they are if there are only two?
In plain “vanilla” AppleScript there is no control over the size of a dialog box, the text font (or styling) used or the placement of its buttons. If you want a fancy interface, you have to design and build it yourself in AppleScript Studio (part of Xcode).