choose folders and files

Hi all suffering Hang over here so forgive me if this is a dumb one.

How do you choose files and folders with the same choose dialog prompt??

Cheers.
Mark

You can’t currently do it using just Standard Additions, Mark. However, there’s still a way:

Requires: Satimage.osax


navchoose object


I tried to find a solution for this some time ago - here my attempt (needs an extra dialog box to change the current location):

on chooseFilesNFolders(defaultFolder)
	set {olddelims, text item delimiters} to {text item delimiters, ": "}
	set choosing to true
	set currentFolder to defaultFolder
	repeat while choosing
		set theList to {}
		tell application "Finder"
			repeat with theFolder in (get folders of currentFolder)
				set theList to theList & {"Folder: " & name of theFolder}
			end repeat
			repeat with theFile in (get files of currentFolder)
				set theList to theList & {"File: " & name of theFile}
			end repeat
		end tell
		if (length of theList) > 0 then
			set theSelection to (choose from list theList OK button name "select" cancel button name "change location" with multiple selections allowed and empty selection allowed)
			if theSelection ≠ false then
				set choosing to false
				set retValue to {}
				repeat with thisItem in theSelection
					if (text item 1 of thisItem = "Folder") then
						set retValue to retValue & {alias (((currentFolder as string) & (text item 2 of thisItem) & ":") as string)}
					else
						set retValue to retValue & {alias (((currentFolder as string) & (text item 2 of thisItem)) as string)}
					end if
				end repeat
				set text item delimiters to olddelims
				return retValue
			end if
		else
			display dialog "empty folder" giving up after 2
		end if
		set currentFolder to (choose folder with prompt "Select new location" default location currentFolder without multiple selections allowed)
	end repeat
end chooseFilesNFolders

Thanks you kai,
Thats good to know about Satiimage.osax,
But I do not want to have to use third- party additions.
Maybe one day Apple will have a rethink.

Dominik, I thought about going down the list route but decided against trying just yet, but running your script changed my mind, Very nice. Thank you.
I will incorporate it in the script I was playing with, seen here. in this thread.

Understood, Mark.

Indeed. A minor clean-up routine aside, the following variation on Dominik’s nifty idea largely avoids repeat loops ” so it should compile/display lists faster:

to |list items|(list_title, list_content)
	set text item delimiters to return & tab
	paragraphs of ((list_title as Unicode text) & return & tab & list_content)
end |list items|

to |choose folder or file| from folder_alias
	set tid to text item delimiters
	set posix_path to folder_alias's POSIX path
	set item_list to {}
	tell application "Finder" to tell folder folder_alias
		if (count folders) > 0 then set item_list to item_list & my |list items|("Choose Folder:", name of folders)
		if (count files) > 0 then set item_list to item_list & my |list items|("Choose File:", name of files)
	end tell
	set text item delimiters to tid
	if (count item_list) is 0 then return |choose folder or file| from choose folder with prompt ¬
		"There are no items in " & posix_path & ". Please choose another folder:" default location folder_alias
	set l to choose from list item_list with prompt "Choose from " & posix_path & ":" OK button name ¬
		"Select" cancel button name "Change Folder" with multiple selections allowed and empty selection allowed
	if l is false then return |choose folder or file| from choose folder default location folder_alias
	set text item delimiters to ""
	set l to l as Unicode text
	repeat with i in {"Choose Folder:", "Choose File:"}
		set text item delimiters to i
		if (count l's text items) > 1 then
			set l to l's text items
			set text item delimiters to ""
			set l to l as Unicode text
		end if
	end repeat
	set text item delimiters to tab
	set l to rest of l's text items
	set text item delimiters to tid
	tell application "Finder" to tell folder folder_alias
		if (count l) is 1 then return (first item whose name is in l) as alias
		(items whose name is in l) as alias list
	end tell
end |choose folder or file|

|choose folder or file| from path to documents folder (* or any other folder alias *)