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:

Open a file in your editor of choice

[Update]
Chris Stone has a nice follow-up on the BBEdit user forum.


You know how it is. You double-click on a file thinking it will open in the application that you're working with at the moment and you forget that the file type was associated with a different application...

Back to Finder, right-click on the file, select "Open with..." and you're good. But mousing around macOS can be tedious at times so here is a simple script that I was pretty much given by an ASUL co-lister and that I barely had to adapt to my workflow to make it more general.


property targetApplication : "BBEdit"

tell application "Finder" to set mySelectionList to selection as alias list
if length of mySelectionList = 0 then error "No files were selected in the Finder!"

tell application targetApplication
repeat with myFile in mySelectionList
open myFile
end repeat
activate

end tell

You notice right away that the only reference to the opening application is in the first line. You can change the application name to anything you want and have multiple copies of the script, one for each application, with an appropriate name so that you can open any file with any supporting application you want.


Here is the exact same code for TextEdit:


property targetApplication : "TextEdit"

tell application "Finder" to set mySelectionList to selection as alias list
if length of mySelectionList = 0 then error "No files were selected in the Finder!"

tell application targetApplication
repeat with myFile in mySelectionList
open myFile
end repeat
activate

end tell


The same for Script Editor:


property targetApplication : "Script Editor"

tell application "Finder" to set mySelectionList to selection as alias list
if length of mySelectionList = 0 then error "No files were selected in the Finder!"

tell application targetApplication
repeat with myFile in mySelectionList
open myFile
end repeat
activate

end tell


The same for Script Debugger:

property targetApplication : "Script Debugger"

tell application "Finder" to set mySelectionList to selection as alias list
if length of mySelectionList = 0 then error "No files were selected in the Finder!"

tell application targetApplication
repeat with myFile in mySelectionList
open myFile
end repeat
activate
end tell


The same for Word:


property targetApplication : "Microsoft Word"

tell application "Finder" to set mySelectionList to selection as alias list
if length of mySelectionList = 0 then error "No files were selected in the Finder!"

tell application targetApplication
repeat with myFile in mySelectionList
open myFile
end repeat
activate

end tell


You get the drift.

Some applications don't work that way, so here is the code for Emacs.app:


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

tell application "Finder" to set fileSelection to selection as alias list

try
tell application "System Events" to tell process "Emacs" to set frontmost to true
on error
tell application "/Users/suzume/Documents/Code/emacs/nextstep/Emacs.app"
activate
delay 1
end tell
end try

# return

repeat with selectedFile in fileSelection
set the clipboard to (POSIX path of selectedFile)
tell application "/Users/suzume/Documents/Code/emacs/nextstep/Emacs.app"
tell application "System Events"
delay 0.5
keystroke "x" using {control down}
keystroke "f" using {control down}
keystroke "a" using {control down}
keystroke "v" using {command down}
keystroke "k" using {control down}
key code 36 # Escape
delay 0.1
end tell
end tell

end repeat

The code makes use of UI scripting because Emacs does not support Applescript, but it works just as well.

What you do now, is create one script for each application you want to open your files with, save the script with a name that Spotlight will easily call first (something like ">BB" for opening in BBedit) and you're done!

Thank you Chris Stone on ASUL (and all the others) for your help !

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 MacPort 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 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