How do I get an AppleScript reference for a Unix symlink?

the subject pretty much says it all. I need a reference to the symbolic link, not to the linked file. In other words, I want to instruct AppleScript not to follow the link. Let’s say “link-to-file.txt” is a symbolic link (created with ln -s) to “file.txt”. I have tried

POSIX file "/path/to/link-to-file.txt"

but that resolves to

file "HD:path:to:file.txt"


alias "HD:path:to:link-to-file.txt"

results in

alias "HD:path:to:file.txt"

The reason I need this is to be able to set a Spotlight comment for a symlink through a script. I can set the comment manually in the Finder and the comment gets associated to the link, not to the linked file (surprisingly enough, even hardlinks may have comments independent of the comment on the linked file!), So, this is, in principle, possible. Any idea?

Path to the symbolic link: “/Path/To/FIle/File.txt symlink” Is that what you were looking for?

Sorry, I do not understand your suggestion. Maybe I have not explained the problem clearly enough. Consider the following example:

set thePath to POSIX path of (path to the desktop)
set theFile to thePath & "foo.txt"
set theLink to thePath & "foo-symlink"
-- Create a file and a symlink to the file
do shell script "touch " & theFile & " && ln -s " & theFile & " " & theLink

-- Try to set the comment of the symlink (and fail)
tell application "Finder"
	-- This sets the comment on the file pointed to by the symlink, not on the symlink itself
	set the comment of ((POSIX file theLink) as alias) to "I want to go with the symlink!"
end tell

The question is: how can I set the comment of the symlink itself?

Actually, I have noticed that a comment can be set if the link is broken (i.e. it does not point to a valid file). So, a hackish way would be to create a broken link, set the comment and then (somehow) fix the link. Still searching for something less hackish :slight_smile:

This seems to work. (Tested with the files in different folders!)

-- Get the HFS path to the link's folder rather than to the link itself.
set linkFolderHFS to (do shell script "dirname " & theLink) as POSIX file as text
tell application "Finder"
	set the comment of file "foo-symlink" of folder linkFolderHFS to "I want to go with the symlink!"
end tell

Simply brilliant :slight_smile:

A quick check in the Terminal, though, shows that the metadata has been associated with the file, not the link. But, in the Finder, the comment labels the link. Mistery of OS X metadata :confused:

Thanks for the tip!