how to choose a file or a folder

Hello I am building a little script to automate changing a file’s or a folder’s icon by accessing the get info window using the es suite. Sometimes I need the icon of a file and sometimes I need the icon of a folder.


tell application "Finder"
	select item (choose file) -- select an item
	activate
end tell

for example in the above snippet the item I can select a can be written (choose file) or (choose folder) but I can’t see how to write (choose file or folder).
I was hoping (navchoose object) from satimage would work, but no dice.
Help
Pete O’Connell

If you are using the select item method each time the first
example will work. If you want to select the item in the
finder first and then run the script the second example
will work.


--First Example
tell application "Finder"
	set buttonChoice to button returned of (display dialog "Folder or file?" buttons {"Folder", "File"} default button 2)
	if buttonChoice is "Folder" then
		select item (choose folder)
	else
		select item (choose file)
	end if
end tell

--Second Example
tell application "Finder"
	set theSelection to selection as alias
	
	if kind of theSelection is "Folder" then
		--do your code here
	else
		--do your code here
	end if
	
end tell

Cheers,

Craig

Hello I can’t seem to get those to do what I want which is: While I am navigating through a (choose file) or (choose folder) dialog to be able to choose either a file OR a folder within the same dialog on the fly since when I invoke the choose window I haven’t yet decided whether I want a file or a folder.
Pete O’Connell

Here’s a script by Kai Edwards that shows files and folders for choosing – perhaps it will work for you.

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 *)

Yowza, thanks Adam
Pete O’Connell