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