How to support this blog?

To support this blog, you can hire me as an OmegaT consultant/trainer, or you can send translation and project management jobs my way.

Search the site:

Launch files in Emacs.app

I'll eventually find the time to combine this script with the one that I describe in An even newer version of my Capture app. For now, I call this script by saving it as an AppleScript application that I named ">Emacs.app".



####################################

use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions


#########

# This is standard AppleScript with no extra. 

# No need to declare anything more than the scripting additions.

#########



property myEmacs : "/Users/suzume/Documents/Repositories/emacs/nextstep/Emacs.app"

property myEmacsclient : "/Users/suzume/Documents/Repositories/emacs/nextstep/Emacs.app/Contents/MacOS/bin/emacsclient"

property myEmacsFront : false


#########

# I decided to use properties instead of variables in this script.

#########



tell application myEmacs

activate

end tell


#########

# Here, I activate Emacs, which has the effect to either put it at the front

# or to launch it.

#########



repeat while myEmacsFront = false

try

tell application "System Events" to tell process "Emacs" to set frontmost to true

set myEmacsFront to true

on error

delay 1

end try

end repeat


#########

# If Emacs is not launched, it may take some time to start it.

# In which case the script will error because Emacs won't be here

# when the script needs it.

# So I loop every second on the myEmacsFront property that

#  will eventually be set to true once Emacs is set to the front.

#########



tell application "Finder"

set fileSelection to selection as alias list

end tell


#########

# The Finder gives us the list of selected files.

#########



try

repeat with selectedFile in fileSelection

set myEditCommand to myEmacsclient & " -e \"(find-file \\\"" & (POSIX path of selectedFile) & "\\\")\" -n"

try

do shell script myEditCommand

on error

display alert "Emacs can't open the file"

end try

end repeat

on error

display alert "The script can't find the file"

end try


#########

# This is the core of the script. It asks emacsclient to evaluate

# (find-file "file path").

# The error messages try to describe the error location.

#

# The only difficult part in this script was the escape sequences

# needed to create myEditCommand.

#########

Automator: Finder → Launch Application

I don’t know why it never occurred to me, but there is an Automator action from Finder that’s called Launch Application and that you can use to:

  • register an Applescript (or any other) application in the Services menu,
  • assign a shortcut to it,
  • launch it without having to call it with Spotlight.

That saves the necessity to separately manage your AppleScript code, plus debugging from Automator is really not fun. . .




And it also gives you the ability to automatically call the application instead of using either Spotlight or the running application navigator (I don’t know what it is called, the thing that you trigger when you hit Command+Tab).


Anyway, now I have my Emacs Capture app from this weekend directly available with a shortcut, the only problem now being to make sure that the shortcut is particular enough that it does not conflict with existing application shortcuts. . .



Haha! That shortcut seems to work anywhere but in text fields, where it calls the spellchecker dialog...


Et voilà!


ps: Shortcuts has something similar, but it looks like it won’t accept applications that are not installed in /Applications, which is a pity.

Popular, if not outdated, posts...

.docx .NET .pptx .sdf .xlsx AASync accented letters Accessibility Accessibility Inspector Alan Kay alignment Apple AppleScript ApplescriptObjC AppleTrans applications Aquamacs Arabic archive Automator backup bash BBEdit Better Call Saul bug Butler C Calculator Calendar Chinese Cocoa Command line CSV CSVConverter database defaults Devon Dictionary DITA DocBook Dock Doxygen EDICT Emacs emacs lisp ergonomics Excel external disk file formats file system File2XLIFF4j Finder Fink Font français Free software FSF Fun Get A Mac git GNU GPL Guido Van Rossum Heartsome Homebrew HTML IceCat Illustrator InDesign input system ITS iWork Japanese Java Java Properties Viewer Java Web Start json keybindings keyboard Keynote killall launchd LISA lisp locale4j localisation MacPorts Mail markdown MARTIF to TBX Converter Maxprograms Mono MS Office NeoOffice Numbers OASIS Ocelot ODF Okapi OLPC OLT OmegaT OnMyCommand oo2po OOXML Open Solaris OpenDocument OpenOffice.org OpenWordFast org-mode OSX Pages PDF PDFPen PlainCalc PO Preview programming python QA Quick Look QuickSilver QuickTime Player Rainbow RAM reggy regular expressions review rsync RTFCleaner Safari Santa Claus scanner Script Debugger Script Editor scripting scripting additions sdf2txt security Services shell shortcuts Skim sleep Smultron Snow Leopard Spaces Spanish spellchecking Spotlight SRX standards StarOffice Stingray Study SubEthaEdit Swordfish System Events System Preferences Tags TBX TBXMaker Terminal text editing TextEdit TextMate TextWrangler The Tool Kit Time Capsule Time Machine tmutil TMX TMX Editor TMXValidator transifex Translate Toolkit translation Transmug troubleshooting TS TTX TXML UI Browser UI scripting Unix VBA vi Virtaal VirtualBox VLC W3C WebKit WHATWG Windows Wine Word WordFast wordpress writing Xcode XLIFF xml XO xslt YAML ZFS Zip Zotero