Activating Vim with Applescript - macos

Using MacOS Mojave, I recently intalled the last version of vim thanks to this easy tutorial : https://medium.com/swlh/vim-is-vulnerable-update-vim-on-macos-66402e5ab46a
I am now trying to synchronize it with Skim to do backward search. To do so, I am using a script in which vim is activated using AppleScript with the following command :
osascript -e 'tell app "Vim" to activate'
I get the following error when executing:
18:26: execution error: Il est impossible d’obtenir application "Vim". (-1728)
(translation from French : It is impossible to obtain application "vim")
macerror -1728 gives me:
Mac OS error -1728 (errAENoSuchObject): e.g.,: specifier asked for the 3rd, but there are only 2. Basically, this indicates a run-time resolution error.
I have tried changing "vim" to "Vim" in the command; creating a symlink from vim in /Applications to vim in /usr/local/bin ; changing owner of vim to $USER. Nothing worked.
Anyone has any idea what is going on ?

AppleScript is unfortunately only for GUI apps - therefore you won't be able to manage CLI Vim at all. I can see that any other solutions won't make you happy, since the tutorial is about using particular vulnerability. You can make
brew install macvim
to operate on the functionalities that vim provides, so you can still record keystrokes, print stuff and so on. You can also read about vim +clientserver, it might provide a solution for your needs, but it'd be still just a workaround.

Since vim in this use case is a command line executable, not a standard GUI app, the following example AppleScript code should work:
osascript -e 'tell app "Terminal"' -e 'do script "/usr/local/bin/vim"' -e 'activate' -e 'end tell'
Note: If necessary, change the fully qualified pathname of vim to where you installed it.

Related

How to ask Cocoa app to run an action from the terminal and then return the result

My Cocoa MacOS app has an action that modifies some files in the disk.
I want to make that actions available to run from a terminal.
For example, if I run:
$ echo `myApp runAction`
that would open the app, execute some code associated with "runAction", and then print a result to the console.
Unfortunately, I can't just make a Command Line Tool because of its limitations (can't include dynamic frameworks).
Any tips how to make it?
You should make your app scriptable with Apple Script. With this you "speak" to your app in bash (or zsh) no matter if it is running or not by :
osascript -e 'tell app "myApp" to runAction'
or by an AppleScript script written in the Script Editor app.
When app is not running it will launch first.
There are a few examples/documentations in the web which are sufficient for basic tasks like executing a command and returning the result:
Mac Scripter link
Making A Mac App Scriptable Tutorial (raywenderlich.com) link
In my case these docs were not sufficient for complicated tasks like passing parameters to the command, but for simple tasks like:
osascript -e 'tell app "myApp" to login'
osascript -e 'tell app "myApp" to logout'
— and myApp returns a literal "0" or "1" if the action failed for some reason or was successful —
it worked.
And osascript -e 'tell app "myApp" to quit' even came for free.
Regards,
Robert
Even if your application is an .app bundle, its executable is still the same kind of binary file that you would get in a Command Line Tool.
You can execute it in Terminal, pass arguments, print output. E.g.
$ ./MyApplication.app/Contents/MacOS/MyApplication --some-argument
Depending on what your app is for, it might be not perfect solution, but it’s a completely valid way to use it.

Set path to applications for vim on mac capitan

I have used vim to make small scripts in python and typesetting things in LaTeX. So it is very useful to run applications from vim by typing :!python or :!pdflatex etcetera. But after upgrading to el capitan, this seems not to work anymore, get message like /bin/bash: pdflatex: command not found. But the funny thing is that it is possible to run the applications directly from terminal. Anyone that know how to set the correct that for vim as well?
$PATH variable not properly set in gvim/MacVim when it is opened from the finder
first check :!echo $SHELL and see if SHELL is set to bash or not
then try to set your path in your ~/.bash_profile?
export PATH=<dir_contains_pdflatex>:$PATH

AppleScript: execution error -10810 when launching certain applications from shebang'ed scripts

I'm running OS X 10.10.2. I'm facing a weird issue where AppleScript won't launch applications from shebang'ed scripts while working fine everywhere else (Script Editor, piping to osascript, etc.). Specifically, consider the following example script named launch-app:
#!/usr/bin/osascript
launch application "TextEdit"
When TextEdit is not running and I do
./launch-app
I get
./launch-app:0:29: execution error: An error of type -10810 has occurred. (-10810)
When I do
<launch-app osascript
Well, it works just fine; which means the following Bash script will also work:
#!/usr/bin/env bash
osascript <<EOF
launch application "TextEdit"
EOF
Really weird. (By the way, a tell ... activate ... end tell block results in the same error. I'm using launch here just to keep to example minimal.)
I have some old scripts that involve activating an application (well, practically all my old scripts involve tell ... activate ... end tell) that definitely worked in the past. I can't tell when things began to fall apart because when I run those scripts, most often the applications to activate are already launched. I have the impression that the issue dates back at least to 10.10.1.
I have looked at several related posts here on SO, e.g., this one, but they don't help. I also tried to understand error -10810 by reading articles like this one, but my problem definitely doesn't look like a filled process table (otherwise why does directly calling osascript works while running osascript from a shebang doesn't?).
Update: The bug has been fixed in OSX 10.10.3.
Just to provide a state-of-the-union post:
The behavior observed is a bug in OSX 10.10 still unresolved as of OSX 10.10.2 (as of 10 Mar 2015):
Anyone interested in getting this fixed should file their own bug at http://bugreport.apple.com.
It applies to executable scripts that are directly or indirectly passed to osascript - whether:
explicitly (osascript launch-app)
or implicitly, via the shebang line, by direct invocation (./launch-app)
The specific form of the shebang line is irrelevant (whether #!/usr/bin/osascript or #!/usr/bin/env osascript or #!/usr/bin/env osascript -l JavaScript or ...), what matters is whether the file has the executable bit (permission) set (e.g., via chmod +x).
Workarounds:
As suggested by the OP, feed the file to osascript via stdin: osascript < launch-app
This has side effects; for instance, name of me will report msng instead of the name of the script.
Remove the executable bit from the script and invoke it explicitly with osascript:
chmod -x launch-app # a one-time operation
osascript launch-app # with the executable bit unset, this should work
Looking at the man page for osascript, when you send lines of applescript code you should put the "-e" option infront of each separate line.
So here's what I tested. I made a bash script with the -e option...
#!/bin/bash
osascript -e 'launch application "TextEdit"'
And one without.
#!/bin/bash
osascript 'launch application "TextEdit"'
The one without the -e option does not run. As such I think this could be a cause of your problem... there's no -e option in your code.
Note that I tested your code too and got the same error as you. There's a command line utility "/usr/bin/macerror" and I entered your error code into that. Here's the result.
Unknown error (-10810) at /usr/bin/macerror5.18 line 40, <DATA> line 1.
Good luck.
There is no need for using osascript to launch applications. There is a built in command line utility named open, that will open your app from the terminal commandline, or a shebanged script. For doucumentation, enter "man open" in a terminal window. It is a really nifty utility, with a lot of options. :)
The open utility, will lauch applications that are not running, but I also wonder out of curiosity: have you tried "tell application appname to run", or just "tell application appname to activate"?
The osascript below, works for me, on 10.9
#!/usr/bin/osascript
tell application "TextEdit" to launch
I guess you'll have to commmand the app to do something, and not just try to "launch" it. Maybe "tell me to launch application appname also works".
Edit
I prefer to use open -b "com.apple.textedit", because then I also get the front window of textEdit, brought to front.
By the way, with the open -e command, you can open a textfile directly into TextEdit from the commandline. open is not totally as good as the plumb utility of plan-9, but it is really nifty.

Osascript always returns error Cannot find executable for CFBundle 0x7fa3f42032e0

Osascript in terminal always returns this error for the simplest function.
When i run the script in ApplescriptEditor it runs fine…
e.g.:
$: osascript -e 'tell application "iTerm" to display dialog "Hello World"'
2014-01-22 11:59:19.822 osascript[665:707] Cannot find executable for CFBundle 0x7fa3f42032e0 </Library/ScriptingAdditions/AeroFSFinderExtension.osax> (not loaded)
osascript: OpenScripting.framework - scripting addition
"/Library/ScriptingAdditions/AeroFSFinderExtension.osax" declares no loadable handlers.
button returned:OK
I already reinstalled XCode and CLL (Using Appcleaner)
Does anybody know how to fix this error? (Or at least silence it…)
I'm on 10.8.5
What is /Library/ScriptingAdditions/AeroFSFinderExtension.osax?
Looks like it's a broken scripting addition, and if it's not actually needed, simply try to remove the file (you'll need admin privileges), then run osascript again.

Mac OS X: Bring (non-bundle) GUI applications to foreground when launched from the command line

When a GUI process is launched from the OS X terminal, the window shows up in the background, and you have to use command-tab to give it focus.
Is there a way to make the terminal automatically give such GUIs focus after they are launched?
For example (assuming gitk is installed):
% gitk
should launch the GUI and then switch to it.
Note: For several reasons, using open as this answer suggests is not a general solution.
Update: To better explain why the open method isn't satisfactory, here's a sample bash session (with witty commentary).
% cd /my_repo
% gitk
Waiting for the GUI to appear ... any day now ... oh wait -- it's already open. I just didn't notice because it opened a window BEHIND my terminal. I wonder how long I was going to sit here waiting....
% open gitk
The file /my_repo/gitk does not exist.
Ah, of course.
% which gitk
/usr/bin/gitk
% open /usr/bin/gitk
What the ... it opened a new terminal window to run gitk, and it did so in my home directory, not /my_repo, so gitk complains that the current directory isn't actually a repository...
Do you need to invoke it synchronously? If not, you could start it asynchronously with & and then activate it with osascript -e 'tell application "gitk" to activate'.
If you are dealing with gitk specifically you can edit the gitk file to achieve this, I posted an answer on the apple stack exchange: https://apple.stackexchange.com/a/74917/35956
You can find the gitk file using the following command from the terminal
which gitk
In my gitk file I found a line that looks like the following near the top (line 3)
exec wish "$0" -- "$#"
I changed it to this
exec wish "$0" -- "$#" & exec osascript -e "tell application \"Wish\" to activate"
When I execute gitk from the command line the gitk window comes to the foreground, another side effect of this is that it executes asynchronously
You can wrap up #chris page's answer in a bash function and drop it in your .bashrc
function gitk() {
command gitk "$#"&
command osascript -e "delay .5" -e "tell application \"wish\" to activate"
}
There should be a way to get rid of the delay by looping and looking for 'wish' with a timeout.
NOTE: 'Wish' is the window title that shows up on my Mac for gitk.

Resources