Apr 10, 2010

Select Files in Finder And Open Them Using Emacs

At a mention message on Twitter, I was asked a question that whether there is or not a way to open a text file that was selected on Finder using with Emacs editor on a terminal. I thought that perhaps it may be able to do that if someone write an Applescript for it. And I answered so. After tweeting that, I tried to write a script to implement it.

A feature of the following script is to open some text files you selected within a Finder window. At the time, if a process is running in the current tab, a new tab is made and a file is opened by emacs in the new tab. If there are some folder items in items you selected, they are excluded from the list.

property editor : "emacs"

(* Selecting some text files. *)
tell application "Finder" to set file_objects to selection
filefilter of me given selected_items:a reference to file_objects
if file_objects is {} then
  set msg to "Would you select some text files." & return & return & ¬
    " If there are some folder items in items you selected, they are excluded from the list."
  display alert "There are no text files in items you selected." message msg buttons {"Close"}
  return
end if

(* Openning using with Editor. *)
tell application "Terminal" to activate
repeat with f in file_objects
  new_open_tab() of me
  open_file_withEditor given file_path:f
end repeat

------------------------------------------------------------------------------------------
-- Terminal
------------------------------------------------------------------------------------------
on new_open_tab()
  tell application "Terminal"
    tell window 1
      try
        if selected tab is busy then
          add_newTab() of me
        end if
      on error
        -- This handling is a case of no window of Terminal.
        (* I didn’t get the how to make a new window.
            So I’m using a command of making a new tab. *)
        add_newTab() of me
      end try
    end tell
  end tell
end new_open_tab

on open_file_withEditor given file_path:posix_path
  set cmd to editor & " " & posix_path as Unicode text
  tell application "Terminal"
    tell window 1
      do script cmd in selected tab
    end tell
  end tell
end open_file_withEditor

------------------------------------------------------------------------------------------
-- System Events
------------------------------------------------------------------------------------------
on add_newTab()
  tell application "System Events"
    tell window 1 of application "Terminal" to activate
    key code 17 using {command down}
  end tell
end add_newTab

on view_tab() -- unused subroutine
  tell application "System Events"
    tell window 1 of application "Terminal" to activate
    key code 17 using {shift down, command down}
  end tell
end view_tab

------------------------------------------------------------------------------------------
-- Filefilter
------------------------------------------------------------------------------------------
on filefilter given selected_items:file_objects_ref
  set swap to contents of file_objects_ref
  set contents of file_objects_ref to {}
  repeat with f in swap
    set the_alias to f as alias
    if not (folder of (info for the_alias)) then
      set contents of file_objects_ref to contents of file_objects_ref ¬
        & quoted form of (POSIX path of (the_alias as Unicode text))
    end if
  end repeat
end filefilter

0 comments: