I don’t think this is possible, but perhaps someone knows a way to do it:
If I know the location of a file or folder, is there any way to find all the aliases TO that file or folder that may exist on my system?
I am writing a script that will make an alias to a known location, but I want to make that alias only if an alias does not already exist in the user’s home folder or its subfolders. So I would want to test whether such an alias might already exist.
Thanks for any help.
AFAIK files do not store info about any aliases they might have, so that leaves a brute-force approach as the only possibility: collect all aliases, and see if any are for the file in question.
Here’s a script I’ve used for years to clean up orphaned aliases:
(*
Problem: aliases to offline items (disks, disk images)
Solution: none. Keep them elsewhere
*)
-- mind yer spaces & quotes!
property searchLoc : "-onlyin ~/Library -onlyin ~/Documents "
property prefixCmd : "mdfind "
-- 'Alias' is in (escaped) double quotes
-- the command is in single quotes
-- the string is in double quotes
property suffixCmd : "'kMDItemKind == \"Alias\"'"
-- use Spotlight to find aliases
set myCommand to prefixCmd & searchLoc & suffixCmd
set theList to paragraphs of (do shell script myCommand)
tell application "Finder"
repeat with p in theList
set thisAlias to p as POSIX file as alias -- coerce Unicode text to POSIX file path to AppleScript alias
try
original item of thisAlias
on error -- it's an orphaned alias
delete thisAlias
end try
end repeat
end tell
Change the try block to find out if the current alias is for the current file. If yes, abort. If no, continue. When script lives thru entire list no matching alias was found; create one.
That is perfect - and uses a technique (Spotlight) that I had not thought of at all. Thank you!!
As you suggested, this was easy to adapt for my purposes. Here is code that simply displays a dialog with the result, though, of course, I’ll change this so that it offers to create an alias if one is not found:
(*
Problem: aliases to offline items (disks, disk images)
Solution: none. Keep them elsewhere
*)
-- mind yer spaces & quotes!
property searchLoc : "-onlyin ~/ "
property prefixCmd : "mdfind "
-- 'Alias' is in (escaped) double quotes
-- the command is in single quotes
-- the string is in double quotes
property suffixCmd : "'kMDItemKind == \"Alias\"'"
-- use Spotlight to find aliases
set myCommand to prefixCmd & searchLoc & suffixCmd
set theList to paragraphs of (do shell script myCommand)
set foundAlias to 0
tell application "Finder"
repeat with p in theList
set thisAlias to p as POSIX file as alias -- coerce Unicode text to POSIX file path to AppleScript alias
try
if original item of thisAlias is folder "ANONYMOUS" of folder "cups-pdf" of folder "spool" of folder "var" of item "private" of startup disk then
set foundAlias to 1
exit repeat
end if
end try
end repeat
end tell
if foundAlias is 0 then
display dialog "Could not find an alias."
else
display dialog "Found an alias."
end if
Thank you again for this excellent code!
By the way, I am sure there is an easier way to specify the location of the original file (perhaps using a POSIX path), but I haven’t been able to figure out what it is. If the answer to that is obvious, I would be very grateful to know it!
I assumed you would run the script with a file/folder selected, and have it act on that item:
tell application "Finder"
set {theSel} to selection -- ONE ITEM ONLY
end tell
Or use choose file (folder), and pass the result to the script:
tell application "Finder"
set theFile to choose file
-- or
set theFolder to choose folder
end tell
The Satimage OSAX has navchoose object to select files AND folders, but that is 32bits, sadly.
Those are very useful additions. Thank you.
In the script I’m writing, I know in advance what the folder is that ought to have an alias to it (because the folder is created by the CUPS-PDF printer driver automatically when running in the way my script uses it), but that won’t always be the case. Your code solves that problem.