macvim-dev / macvim

Vim - the text editor - for macOS
https://macvim.org
Vim License
7.47k stars 680 forks source link

Applescript `document` method not recognized #1266

Open marcopolo4k opened 1 year ago

marcopolo4k commented 1 year ago

Steps to reproduce

  1. Create an Applescript that opens MacVim and calls document 1. Eg:
    tell application MacVim
    activate
    set my_path to get path of document 1
    end tell
  2. Execute script with osascript script_name.
  3. Find the error: Can’t get document 1. Invalid index. (-1719)

See more details here.

Expected behaviour

get path of document 1 should return the file path of the first document being currently displayed. I believe it's also called the 'front document'.

Version of Vim and architecture

9.0.65 (173)

Environment

Operating System: Bug Sur version 11.6.8 (20G730) Terminal: Using MacVim GUI, not a terminal. It's version shows the Vim version 9.0.65 (173). It also says c2007 Bjorn Winckler. Value of $TERM: xterm-256color

How MacVim was installed

brew install macvim

Logs and stack traces

Don't find any log or other error besides what's displayed.  Maybe I don't know how to turn on verbose logging?

Vim configuration where issue is reproducable

No response

Issue has been tested with given configuration

Issue has been tested with no configuration

Other conditions

eirnym commented 1 year ago

The problem is not in MacVim, but in the script file itself as some conditions hasn't been met. This error is quite common as you Google it

ychin commented 1 year ago

This would be a useful thing to support but I think it requires MacVim to be a document-based program. It's a little more tricky than it seems because the actual files opened are being managed by Vim. We could just expose the currently edited document but that wouldn't be really supporting the full document API.

Marking this as a feature request for now. It's doable, but just not very high priority.

Just curious though @cPMarco what are you trying to do here? What do you want to accomplish by writing that script?

marcopolo4k commented 1 year ago

We could just expose the currently edited document but that wouldn't be really supporting the full document API.

I'd vote for that - I'm only interested in this one functionality.

Marking this as a feature request for now.

Makes sense.

Just curious though @cPMarco what are you trying to do here?

I often open/take quick notes with TextEdit, while Vim is used for development. I'd like to be able to toggle those quick notes into MacVim, and back out into TextEdit if I need.

ychin commented 1 year ago

I see. And you are only interested in the file path right? Can you show me what an example script would look like and how you would use the path you get from MacVim to send to TextEdit? AppleScript is always one of those things that I didn't feel like learning too much of. That would cover MacVim -> TextEdit. As for TextEdit -> MacVim, I would imagine the easiest way is just to use the mvim:// protocol.

I kind of need to think through more how to expose MacVim functionality to other apps anyway. There are kind of too many ways to do this right now and Apple seems keen on pushing more and more without supporting each one fully. For example, I was going to suggest doing this via the Shortcuts app instead, but it doesn't seem like TextEdit even supports it (correct me if I'm wrong). There are Services, Apple Script, Share, Shortcuts w/ SiriKit Intents and App Intents and Action extensions, and more, and they all have overlapping functionality. Anyway I digress.

marcopolo4k commented 1 year ago

Yes, the file path is what I need. The full script I'm using is here.

  1. Paste that script into a file, example: ~/toggle-text-editor.apple.scpt
  2. Open a file in MacVim
  3. From Terminal, run osascript ~/toggle-text-editor.apple.scpt e

If I had to do any setup to install Applescript in Mac osacript, I don't remember any.

ychin commented 1 year ago

Ok, I can take a look. Judging from current features down the line, I am guessing this will be investigated around r176. AppleScript support is kind of one of those things that haven't been touched in a while and I want to take a cohesive look into what kind of functionality MacVim should expose to other applications via Shortcuts, intents, and whatnot as well.

ychin commented 1 year ago

Just don't listen to that StackOverflow comment about switching to Emacs :)