My setup is 2020 MacBook Pro M1 (Apple silicon), Big Sur 11.2.2, MacPorts, emacs-mac-app, AucTeX, and PDF Expert. I am trying to get C-c C-c (View) to open PDF Expert at the right page. I'm almost there, but my AppleScript wrapper for PDF Expert is not completing properly. The AppleScript wrapper, ~/bin/pdfe, is below. It is invoked as "pdfe filename page-number". It works fine from the command line, but does not seem to execute the "tell" portion when executed through Emacs (setq TeX-view-program-list '(("PDFExpert" "pdfe %o %(outpage)")))
.
#!/usr/bin/osascript
use framework "Foundation"
use scripting additions
on run argv
set arguments to (current application's NSProcessInfo's processInfo's arguments) as list
do shell script "open -a \"PDF Expert\" " & item 3 of arguments
activate application "PDF Expert"
tell application "System Events"
tell process "PDF Expert"
click menu item "Go to Page..." of menu 1 of menu bar item "Go" of menu bar 1
keystroke item 4 of arguments
key code 36
end tell
end tell
end run
I have taken some pains to ensure that Emacs is running in my usual login environment including PATH, so I'm reasonably confident that isn't the problem (but I can supply my Emacs startup script if requested). I have tried it with and without dbus running. I have tried putting delays all over. I have tried putting popup messages at various places, and found that the script just seems to stop dead before or during the first tell
.
I suspect there might be a permissions issue. Emacs I think has all the permissions it needs, but I don't understand enough about the situation to know how exactly the script is forking from Emacs, or how to enable the necessary permissions.
EDIT: I tried Emacs in an iTerm2 windows, invoking with /Applications/MacPorts/EmacsMac.app/Contents/MacOS/Emacs -nw
, and the pdfe
script runs perfectly, after I enabled Accessibility for iTerm2. I have now also enabled Accessibility in Terminal, and my Emacs GUI version still does not have the right permissions.
EDIT 2: I tried running GUI Emacs by invoking with /Applications/MacPorts/EmacsMac.app/Contents/MacOS/Emacs
, and the pdfe
script again runs perfectly, so it seems the issue is with the way I start up GUI Emacs. So here is my startup script, which I based on Emacs Wiki with a small modification to stop the Automator gear from turning indefinitely:
EMACS=/Applications/MacPorts/EmacsMac.app/Contents/MacOS/Emacs
EMACSCLIENT=/Applications/MacPorts/EmacsMac.app/Contents/MacOS/bin/emacsclient
cd # start in my home directory
if pgrep -U $(id -u) $(basename $EMACS) >/dev/null; then
if [ "$#" -eq 0 ]; then
# set -- -e '(select-frame-set-input-focus (car (or (visible-frame-list) (frame-list)))'
echo "\"$EMACSCLIENT\" -e '(select-frame-set-input-focus (car (or (visible-frame-list) (frame-list))))'" | bash --login -s "$@" ;
else
echo "\"$EMACSCLIENT\" -n \"\$@\"" | bash --login -s "$@" ;
fi
else
nohup bash --login -c "$EMACS "$@"" >/dev/null 2>&1 &
fi