help with script to copy path to file or folder to the clipboard

i d like to create a unified script to le me copy path to folder of file when selected to the clipboard ,i ve come to a very rudimentyal solution which works if i im inside a folder and i launch the script(form a shortcut) if a select a file or subfolder the script will copy to the clipbord botth the folder and the file path , i d like to only get the FILE path in this case . here s my "rudimental " script:

tell application "Finder"
	set dir to quoted form of (POSIX path of (folder of the front window as alias))
	set the clipboard to dir
end tell
do shell script "pbpaste > /Users/Al3/Documents/clipboard-file.txt"



set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
	set the end of pathList to quoted form of (POSIX path of (anItem as alias))
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
do shell script "pbpaste > /Users/Al3/Documents/1clipboard-file.txt"
do shell script "cat /Users/Al3/Documents/clipboard-file.txt /Users/Al3/Documents/1clipboard-file.txt > /Users/Al3/Documents/combined.txt"
do shell script "pbcopy </Users/Al3/Documents/combined.txt"

please someone suggest a better solution, thank you very much for the help

I’m not quite sure I understand exactly what you’re looking for but here is an option that may work for you.

property filePathsFile : ((path to documents folder) as text) & "clipboard-file.txt"

tell application "System Events" to tell application process "Finder"
	set frontmost to true
	repeat until frontmost
		delay 0.1
	end repeat
	delay 1 -- may need to adjust
	keystroke "c" using {option down, command down} -- Copy Selected Files In Finder, As Path Names
end tell

do shell script "pbpaste > " & quoted form of POSIX path of filePathsFile

I’m also uncertain as to what the OP wants. However, based on the title of the thread, and on the action taken by the last line of his script, the following is my suggestion. To test this script, select one or several files or folders in a Finder window or on the desktop and run the script. Multiple paths placed on the clipboard are separated by linefeeds, although this is easily changed to whatever the user prefers.

on main()
	tell application "Finder"
		try
			set selectedItems to selection as alias list
			if selectedItems = {} then set end of selectedItems to (target of front Finder window) as alias
		on error
			display alert "An error has occurred." message "A path is not available for a selected item." buttons {"OK"} default button 1 as critical
			error number -128
		end try
	end tell
	
	set pathOption to button returned of (display alert "Do you want to place the path of the selected items on the clipboard?" message "The number of selected items is " & (count selectedItems) & "." buttons {"Cancel", "HFS Path", "POSIX Path"} default button 3)
	
	if pathOption = "Cancel" then
		error number -128
	else if pathOption = "HFS Path" then
		repeat with anItem in selectedItems
			set contents of anItem to (anItem as text)
		end repeat
	else
		repeat with anItem in selectedItems
			set contents of anItem to (POSIX path of anItem)
		end repeat
	end if
	
	set text item delimiters to {linefeed}
	set the clipboard to (selectedItems as text)
	set text item delimiters to {""}
end main

main()

thank you very much exactly what i wanted!!!