loading...

Open a file in your editor of choice

brandelune profile image Jean-Christophe Helary Originally published at mac4translators.blogspot.com on ・3 min read

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

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 !


Mac for Translators, the discussion group...


Discussion

pic
Editor guide
Collapse
tedhagos profile image
Ted Hagos

Cool. I usually just do these things on Terminal

open myfile.md  -a Typora  # if I want to open it with Typora and not the default markdown editor
open myfile.adoc -a textmate # if I want to open it in TextMate
open myfile.docx -a pages # if I want to open an MS word in Pages

But this guide is good for doing in the GUI

Collapse
brandelune profile image
Jean-Christophe Helary Author

Yes, doing that from Terminal is is totally possible. It's when you're in Finder that you need to fiddle with the keyboard. Copying a file in Finder and pasting it in Terminal gives you an absolute path, so you can use that in a script where you wait for a path argument, but I find that when I'm on the GUI side it's faster to call a GUI side script with Spotlight (or any other utility launcher that takes a selection as argument).