Folder that contains a file

I have some problem to detect a folder that contains a file

on run
	set these_items to choose file with prompt "Choose your file/s" with multiple selections allowed without invisibles
end run

on process_item(these_items)
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set this_info to info for this_item
		set current_name to the displayed name of this_info
		set current_extension to the name extension of this_info
		set current_hidden to the extension hidden of this_info
		if current_hidden is true then
			set default_name to current_name
			set default_name to (current_name & "." & current_extension)
		end if
		set current_path to quoted form of POSIX path of this_item
		set the new_path to text 1 thru -((length of default_name) - 2) of current_path
		return new_path
	end repeat
end process_item

This script give me right name of the folder not always, ad it appears to much complicated.

For exemple, command

set current_path to quoted form of POSIX path of this_item


/Users/matteo/Desktop/Ambient 62.jpg

But I want


(path is the same: I have to use later with command save fo Image Events)

(Many) Regards in advance

Hi Matteo,

why not the easy version?

on process_item(these_items)
	repeat with i in these_items
		tell application "Finder" to set a to container of i as alias
		return POSIX path of a
	end repeat
end process_item

Your calculation of name, extension and hidden extension doesn’t work.
The name of a file contains always the extension, no matter if it’s displayed or not.
So you must calculate the length always with the full name.

Name extension has missing value if there is no extension so you cannot use this value either

The Finder gives you the parent folder with container, that’s easier :wink: