Finder selected files, or a scratchpad, in Emacs

# This script a huge improvement on my original script that appeared here
# about 3 years ago: Open a file in your editor of choice.

# This script is a way to either
#   • open in Emacs the files selected in Finder
#    or
#   • open a scratchpad in Emacs when you need to jot down information.

# The script can be adapted to work with any text editor.

# It tests whether Finder was front when the script was launched *and*
# whether it had selected files.
# If either is negative, it opens a scratchpad with a name that's set here.
# Otherwise is opens the files selected in Finder.

# Personal settings:
# Obviously, your file is located in a different place:
set myEmacsAppPath to "/Users/******/Documents/Repositories/emacs/nextstep/"
# This is the name of the empty default Emacs buffer you want to open:
set myScratchBufferName to "turlututu"

# This part is unlikely to change, it assumes that you use the emacsclient
# that corresponds to the selected And that you run Emacs as a server.
set myEmacsclient to myEmacsAppPath & ""
set myEmacsApp to myEmacsAppPath & ""

# saved by:
# This makes sure that this script is not mistaken for the frontmost application.
# The delay works at 0.2 on my machine, 0.1 is not enough.

tell application "System Events"
    tell (first application process whose frontmost is true)
        set frontmost to false
        set visible of it to false
        delay 0.2
    end tell
    set previously_active_app to item 1 of (get name of processes whose frontmost is true and visible is true)
end tell

# Now that we know whether Finder was previously front or not, we can put Emacs to the front.

    tell application "System Events" to tell process "Emacs" to set frontmost to true
on error
    tell application myEmacsApp
        delay 1
    end tell
end try

# The script's main logic is here.

if previously_active_app is "Finder" then
    # If Finder is the active application when the script is launched, either there is
    # or there is no selection.
        # This is the weird part. There are cases when there is no selection but the assignment
        # is not considered an error, hence I test whether item 1 of the selection exists.
        tell application "Finder" to set fileSelection to its selection
        item 1 of fileSelection exists
    on error
        # If item 1 of selection does not exist, there is no selection and the error branches out here.
        # A scratchpad is opened in Emacs.
        set myCommand to myEmacsclient & " -n -e \"(switch-to-buffer \\\"" & myScratchBufferName & "\\\")\""
        do shell script myCommand
    end try
    # Now that we have checked for that non-selection error, we can work on the selection.
    repeat with selectedFile in fileSelection
        set selectedFile to (POSIX path of (selectedFile as alias))
        set myCommand to myEmacsclient & " -n -e \"(find-file " & "\\\"" & selectedFile & "\\\"" & ")\""
        do shell script myCommand
    end repeat
    # And if Finder was not frontmost when the script was launched, then we just intended to
    # open a scratchpad in Emacs.
    set myCommand to myEmacsclient & " -n -e \"(switch-to-buffer \\\"" & myScratchBufferName & "\\\")\""
    do shell script myCommand
end if

# Et voilà.
# Now, save the script in the format that corresponds to the way you usually launch scripts.
# I prefer the "" way so that I can launch my script by calling it from Spotlight.
# I call this one ">".

Popular posts (last 30 days):