unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* macos: Finder's alias handling
@ 2021-09-26 15:42 Jean-Christophe Helary
  2021-09-26 16:31 ` Daniel Martín
  0 siblings, 1 reply; 8+ messages in thread
From: Jean-Christophe Helary @ 2021-09-26 15:42 UTC (permalink / raw)
  To: Emacs developers

Finder's aliases are binary files that macOS recognizes as something akin to Unix soft links, but they're really very different, except that in dired, they don't look different, they just look like normal files, but opening them just displays a binary blob instead of following the link.

It's probably a problem that has already found a solution, and I'm trying to reinvent a wheel...

So, what I am trying to do is:

1) when I open such a file by mistake, I run a function that uses Applescript's ability to find the target file and I "find-file" that target in Emacs

That would go like this:

(shell-command (format "osascript -e \"tell application \\\"Finder\\\" to return quoted form of POSIX path of (original item of file (POSIX file \\\"%s\\\") as alias)\"" "/path/to/my/alias"))

This produces something like:

’/path/to/the/target/file’

but in *Shell Command Output*, so that I can't just to (find-file (shell-command...)).

There is probably a way to use the contents of *Shell Command Output* and insert that as an argument to find-file, but I don't find that very elegant, and I'm probably missing something in the shell-command because I can't see how I can use the output as a direct argument to find-file...

2) now, I'd like to generalize that so that Emacs automatically triggers that code when entering such a file. So I guess that would encompass dired and the likes, "find-file" and similar commands, etc. Is that even possible? I guess the answer is yes, but at what cost in terms of performance? And how would I go about that?

-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-26 15:42 macos: Finder's alias handling Jean-Christophe Helary
@ 2021-09-26 16:31 ` Daniel Martín
  2021-09-26 23:30   ` Jean-Christophe Helary
  0 siblings, 1 reply; 8+ messages in thread
From: Daniel Martín @ 2021-09-26 16:31 UTC (permalink / raw)
  To: Jean-Christophe Helary; +Cc: Emacs developers

Jean-Christophe Helary <lists@traduction-libre.org> writes:

>
> There is probably a way to use the contents of *Shell Command Output*
> and insert that as an argument to find-file, but I don't find that
> very elegant, and I'm probably missing something in the shell-command
> because I can't see how I can use the output as a direct argument to
> find-file...

Did you try shell-command-to-string? I found it by doing C-h d "shell
output string".

>
> 2) now, I'd like to generalize that so that Emacs automatically
> triggers that code when entering such a file. So I guess that would
> encompass dired and the likes, "find-file" and similar commands,
> etc. Is that even possible? I guess the answer is yes, but at what
> cost in terms of performance? And how would I go about that?

If you see, macOS aliases always start with the same byte pattern, so
you could modify magic-mode-alist and add a specific mapping between the
regular expression that extracts that byte pattern and a new major mode
that you create (macos-alias-mode, for example).  This new major mode
will ask osascript for the real path of the alias and then show its
contents in the buffer, either on demand or automatically.  That's how
Emacs handle other binary file types like images or PDFs.

This approach would work when visiting Finder aliases in general, not
only from Dired.



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-26 16:31 ` Daniel Martín
@ 2021-09-26 23:30   ` Jean-Christophe Helary
  2021-09-27 10:19     ` Alan Third
  2021-09-27 10:34     ` Jean-Christophe Helary
  0 siblings, 2 replies; 8+ messages in thread
From: Jean-Christophe Helary @ 2021-09-26 23:30 UTC (permalink / raw)
  To: Daniel Martín; +Cc: Emacs developers



> On Sep 27, 2021, at 1:31, Daniel Martín <mardani29@yahoo.es> wrote:
> 
> Jean-Christophe Helary <lists@traduction-libre.org> writes:
> 
>> 
>> There is probably a way to use the contents of *Shell Command Output*
>> and insert that as an argument to find-file, but I don't find that
>> very elegant, and I'm probably missing something in the shell-command
>> because I can't see how I can use the output as a direct argument to
>> find-file...
> 
> Did you try shell-command-to-string? I found it by doing C-h d "shell
> output string".

Yes, but it adds a "..." around the ’...’ string and that doesn't work.

>> 2) now, I'd like to generalize that so that Emacs automatically
>> triggers that code when entering such a file. So I guess that would
>> encompass dired and the likes, "find-file" and similar commands,
>> etc. Is that even possible? I guess the answer is yes, but at what
>> cost in terms of performance? And how would I go about that?
> 
> If you see, macOS aliases always start with the same byte pattern, so
> you could modify magic-mode-alist and add a specific mapping between the
> regular expression that extracts that byte pattern and a new major mode
> that you create (macos-alias-mode, for example).  This new major mode
> will ask osascript for the real path of the alias and then show its
> contents in the buffer, either on demand or automatically.  That's how
> Emacs handle other binary file types like images or PDFs.
> 
> This approach would work when visiting Finder aliases in general, not
> only from Dired.

Excellent ! Thank you !

-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-26 23:30   ` Jean-Christophe Helary
@ 2021-09-27 10:19     ` Alan Third
  2021-09-27 12:27       ` Jean-Christophe Helary
  2021-09-27 10:34     ` Jean-Christophe Helary
  1 sibling, 1 reply; 8+ messages in thread
From: Alan Third @ 2021-09-27 10:19 UTC (permalink / raw)
  To: Jean-Christophe Helary; +Cc: Emacs developers, Daniel Martín

On Mon, Sep 27, 2021 at 08:30:43AM +0900, Jean-Christophe Helary wrote:
> 
> 
> > On Sep 27, 2021, at 1:31, Daniel Martín <mardani29@yahoo.es> wrote:
> > 
> > Jean-Christophe Helary <lists@traduction-libre.org> writes:
> > 
> >> 2) now, I'd like to generalize that so that Emacs automatically
> >> triggers that code when entering such a file. So I guess that would
> >> encompass dired and the likes, "find-file" and similar commands,
> >> etc. Is that even possible? I guess the answer is yes, but at what
> >> cost in terms of performance? And how would I go about that?
> > 
> > If you see, macOS aliases always start with the same byte pattern, so
> > you could modify magic-mode-alist and add a specific mapping between the
> > regular expression that extracts that byte pattern and a new major mode
> > that you create (macos-alias-mode, for example).  This new major mode
> > will ask osascript for the real path of the alias and then show its
> > contents in the buffer, either on demand or automatically.  That's how
> > Emacs handle other binary file types like images or PDFs.
> > 
> > This approach would work when visiting Finder aliases in general, not
> > only from Dired.
> 
> Excellent ! Thank you !

Potentially we could also add a function that would resolve them
directly using NSFoundation, rather than parsing the files with a
regex. The regex may be better if we want to support terminal only
builds, though.

(It's also unclear to me whether NSUrl resolves the final path or
not... I think it must but the documentation doesn't mention it
afaics.)
-- 
Alan Third



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-26 23:30   ` Jean-Christophe Helary
  2021-09-27 10:19     ` Alan Third
@ 2021-09-27 10:34     ` Jean-Christophe Helary
  1 sibling, 0 replies; 8+ messages in thread
From: Jean-Christophe Helary @ 2021-09-27 10:34 UTC (permalink / raw)
  To: Daniel Martín; +Cc: Emacs developers



> On Sep 27, 2021, at 8:30, Jean-Christophe Helary <lists@traduction-libre.org> wrote:
>>> There is probably a way to use the contents of *Shell Command Output*
>>> and insert that as an argument to find-file, but I don't find that
>>> very elegant, and I'm probably missing something in the shell-command
>>> because I can't see how I can use the output as a direct argument to
>>> find-file...
>> 
>> Did you try shell-command-to-string? I found it by doing C-h d "shell
>> output string".
> 
> Yes, but it adds a "..." around the ’...’ string and that doesn't work.

The Applescript's "quoted form of" part added the original quoting so removing it allowed me to work around the issue.

Now, for some reason, shell-command-to-string adds a line break at the end of the string to I had to split and car the thing to get the proper path:

(defun jc-macos-alias ()
  (interactive)  
  (find-file
   (car (split-string (shell-command-to-string (format "osascript -e \"tell application \\\"Finder\\\" to return POSIX path of (original item of file (POSIX file \\\"%s\\\") as alias)\"" (buffer-file-name))) "[\n\r]+" t))))

If there are cleaner ways to do this, I'm a taker :-)

-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-27 10:19     ` Alan Third
@ 2021-09-27 12:27       ` Jean-Christophe Helary
  2021-09-27 12:43         ` Alan Third
  0 siblings, 1 reply; 8+ messages in thread
From: Jean-Christophe Helary @ 2021-09-27 12:27 UTC (permalink / raw)
  To: Alan Third; +Cc: Emacs developers, Daniel Martín



> On Sep 27, 2021, at 19:19, Alan Third <alan@idiocy.org> wrote:
> 
> (It's also unclear to me whether NSUrl resolves the final path or
> not... I think it must but the documentation doesn't mention it
> afaics.)

I'm seeing this:

https://developer.apple.com/documentation/foundation/nsurl/1416404-urlbyresolvingaliasfileaturl

URLByResolvingAliasFileAtURL:options:error:
Returns a new URL made by resolving the alias file at url.

Is it relevant ?

-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-27 12:27       ` Jean-Christophe Helary
@ 2021-09-27 12:43         ` Alan Third
  2021-09-27 13:36           ` Jean-Christophe Helary
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Third @ 2021-09-27 12:43 UTC (permalink / raw)
  To: Jean-Christophe Helary; +Cc: Emacs developers, Daniel Martín

On Mon, Sep 27, 2021 at 09:27:35PM +0900, Jean-Christophe Helary wrote:
> 
> 
> > On Sep 27, 2021, at 19:19, Alan Third <alan@idiocy.org> wrote:
> > 
> > (It's also unclear to me whether NSUrl resolves the final path or
> > not... I think it must but the documentation doesn't mention it
> > afaics.)
> 
> I'm seeing this:
> 
> https://developer.apple.com/documentation/foundation/nsurl/1416404-urlbyresolvingaliasfileaturl
> 
> URLByResolvingAliasFileAtURL:options:error:
> Returns a new URL made by resolving the alias file at url.
> 
> Is it relevant ?

Yes, it is. I must have missed that. Unfortunately it's only available
on macOS 10.10 and above and I have no idea how many people are still
using old versions.
-- 
Alan Third



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: macos: Finder's alias handling
  2021-09-27 12:43         ` Alan Third
@ 2021-09-27 13:36           ` Jean-Christophe Helary
  0 siblings, 0 replies; 8+ messages in thread
From: Jean-Christophe Helary @ 2021-09-27 13:36 UTC (permalink / raw)
  To: Alan Third; +Cc: Emacs developers, Daniel Martín



> On Sep 27, 2021, at 21:43, Alan Third <alan@idiocy.org> wrote:
> 
> On Mon, Sep 27, 2021 at 09:27:35PM +0900, Jean-Christophe Helary wrote:
>> 
>> 
>>> On Sep 27, 2021, at 19:19, Alan Third <alan@idiocy.org> wrote:
>>> 
>>> (It's also unclear to me whether NSUrl resolves the final path or
>>> not... I think it must but the documentation doesn't mention it
>>> afaics.)
>> 
>> I'm seeing this:
>> 
>> https://developer.apple.com/documentation/foundation/nsurl/1416404-urlbyresolvingaliasfileaturl
>> 
>> URLByResolvingAliasFileAtURL:options:error:
>> Returns a new URL made by resolving the alias file at url.
>> 
>> Is it relevant ?
> 
> Yes, it is. I must have missed that. Unfortunately it's only available
> on macOS 10.10 and above and I have no idea how many people are still
> using old versions.

I've found references to older documentation, but I can't make sense of it:

https://indiestack.com/2017/05/resolving-modern-mac-alias-files/

https://cocoadev.github.io/FSRef/

https://github.com/nathanday/ndalias/issues/3

-- 
Jean-Christophe Helary @brandelune
https://mac4translators.blogspot.com
https://sr.ht/~brandelune/omegat-as-a-book/




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-09-27 13:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26 15:42 macos: Finder's alias handling Jean-Christophe Helary
2021-09-26 16:31 ` Daniel Martín
2021-09-26 23:30   ` Jean-Christophe Helary
2021-09-27 10:19     ` Alan Third
2021-09-27 12:27       ` Jean-Christophe Helary
2021-09-27 12:43         ` Alan Third
2021-09-27 13:36           ` Jean-Christophe Helary
2021-09-27 10:34     ` Jean-Christophe Helary

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).