Duplicate the functionality of an alias file

Don’t know and maybe someone else knows that but if you save the file open it with another script the alias is still resolved. If you save it as an application (and don’t re-open it) the alias is set all the time.

If you have this in a script:


try 
myAlias
on error --alias is not created yet
set myAlias to ((path to desktop folder) & "testfile.txt" as string) as alias
end try

display dialog myAlias

save it, run it, move the file (even reboot system) and running it a second time and you’ll see it resolves your file again. If you read the documentation about an alias in applescript you also see that every time you run the script, the alias is tried to be resolved and every time you compile, a new alias is created.

Your script doesn’t work for me - it produces an error “Can’t make alias . into type string”. It doesn’t really matter, because all the facts that you mention do not warrant your conclusion. True, IF you save your file, the alias is saved with it. That only shows that an alias CAN be saved to a file - it doesn’t follow that it MUST.

I am sorry - I have only now noticed your edit. I’ve taken a look at the man page for locate. IIUC, it stores the paths to the files to a database. Actually, I can do the same in my own database - but that’s where the problem started.

You see, I need to organize a large collection of files. During this process, files will be moved and renamed - perhaps more than once. At the same time, metadata (e.g. ISBN, title, year of publication, etc.) will be collected and entered into the database.

I need a quick way to open the file directly from its record in my database. That wouldn’t be a problem if the files stayed put (since I could use the path stored in the record). But with the files being moved and/or renamed (not always from within the database), the path’s usefulness is limited. That’s how I started thinking about aliases.

Sorry but couldn’t you figure that error out yourself? it’s the display dialog. If you coerced it into a string it would work. Sorry but hadn’t had the time to test it and didn’t wrote in script editor either.

There’s maybe yet another approach.
I had a similar problem, long time ago. I used Mori (RIP), and a lot of file links in its entries. They broke when their target was moved. So I wrote a script to make an alias, and a link to the alias in Mori. And it worked.
I have since discovered that not all apps open the original file from a linked-to alias. But worth a try? Make aliases in a safe location, and store file URLs to the aliases in the database?

It looks like there’s no solution without aliases…

[later]
Will paths to aliases not work?
[/later]

Yes, I thought about that - but if I have to create the alias files, I can then store them directly in a field of my database (so I don’t need to worry about the link between the DB and the aliases folder). Same thing, actually. It just drives me crazy that I cannot eliminate the file stage and pass the data directly from Applescript into the DB and back.

Not sure what you mean by that.

You said earlier:

And I said:

I’d assume that the same problem might surface with paths to aliases.

(Mori and DEVONthink will open the original, but Safari will not).

I’m still not sure I follow you. If I store a path to an alias file in my database, opening the path will open the original file (provided, of course, that the alias file itself has not been moved or renamed).

I believe that if I store the actual alias file (as binary data), exporting it (with auto-open exported file) will also open the original file. This is assuming I will figure out how to store the alias file instead of the original. Otherwise I’ll have to fall back to the “huge folder of aliases” idea.

Another option I am exploring is reading the resource fork of the alias file, then writing it back. But this too requires making an alias file to begin with.

I realise I omitted the core point. Apologies.

You can make file URLs in a variety of text-capable apps, but not all of them will open the linked file, be it an alias or not. Mori and DEVONthink will open the file, but TextEdit and Omnifocus will only reveal it in Finder.
I was guessing that databases might behave similarly in this respect: some could open the file, but others only reveal it. But you might be using a script to read the path, and open the file…

I see. Well, that’s not a problem: the DB passes a URL in the form of “file://Macintosh HD/private/…/TemporaryItems/MyFile.pdf alias” to the OS and this opens the original file.

I am actually surprised no organizing application of the type you mention has chosen to use aliases in this way. At least I don’t know of any - it seems you can either insert the entire file (thus bloating the organizer document) or a link (that will break if the original is moved/renamed).