Create list of symbolic-link-names and original-item-names?

I am writing a script in which I want to display a list of symbolic links in a folder, together with the name of the original item of the link, something like this:

link1 → /Users/myname/Documents
link2 → /Users/myname/FolderA
link3 → /Users/myname/Folder B

I want the user to be able to select an item on the list and delete the “link1” symbolic link.

All the files in the folder are symbolic links; no other files will be present. I think the most efficient way is to filter the output from “ls -a” but I don’t know how to reduce the output to this simplified form.

I imagine that unix experts can answer this question in thirty seconds. I’ll be very grateful for any help.


try this, it puts all selected items into the trash. If an alias could not be resolved, “n/a” is displayed

set theFolder to (choose folder)
tell application "Finder" to set aliasFiles to alias files of theFolder
set aliasList to {}
if (count aliasFiles) > 0 then
	repeat with anAliasFile in aliasFiles
			tell application "Finder" to set originalItem to POSIX path of (original item of anAliasFile as text)
		on error
			set originalItem to "n/a"
		end try
		set end of aliasList to name of anAliasFile & tab & originalItem
	end repeat
	set chosenItems to choose from list aliasList with multiple selections allowed
	if chosenItems is false then return
	set {TID, text item delimiters} to {text item delimiters, tab}
	repeat with anItem in chosenItems
		set fileName to text item 1 of anItem
		tell application "Finder" to delete item ((theFolder as text) & fileName)
	end repeat
	set text item delimiters to TID
	display dialog "no alias files found"
end if

Alias is better than using symbolic links in this situation. Symbolic links can contained absolute or relative path references. A 30 second Unix solution but with a chance containing relative paths is not better.

Stefan - that solution worked beautifully, far better than anything I could come up with. Thank you!

Unfortunately, the application that I’m working with (Wineskin Winery) uses symbolic links in a special folder, and I have no way of changing that. It’s built into the application that I’m including in my AppleScript application.

Then, if you want to make sure if the symbolic link contains relative and absolute paths and make an distinction between them you can stat like this:

set theFolder to POSIX path of ((choose folder) as string)
do shell script "stat -f '%T%N -> %Y'  " & quoted form of theFolder & "* | grep '^@' | cut -b " & ((length of theFolder) + 2) & "-"