My blog have stopped updating for a long time. The last subject was the same one; about associated script with Emacs. I thought that it’s good subject to proceed with my blog again.
property editor : "emacs"
on run
(* 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 return
(* Make user select mode which adds buffers to an new process or already existing process? *)
set msg to "Which do you intend to add buffers to a new process or already exists process?"
display alert "Select a mode." message msg ¬
buttons {"Cancel", "New process", "Existing process"} default button 3 cancel button 1
set the_mode to button returned of result
(* Launching a terminal. *)
tell application "Terminal" to activate
if the_mode is "Existing process" then
(* Change mode to find file on Emacs. *)
repeat with a_file in file_objects
find_file() of me
enter_filePath(a_file) of me
end repeat
else if the_mode is "New process" then
repeat with a_file in file_objects
set contents of a_file to quoted form of (a_file as Unicode text)
end repeat
(* Serializing file names. *)
set default_delimit to text item delimiters of AppleScript
set text item delimiters of AppleScript to " "
set posix_paths to every item of file_objects as text
set text item delimiters of AppleScript to default_delimit
(* I didn't get the how to make a new window.
So I’m using a command of making a new tab. *)
new_open_tab() of me
(* Openning files. *)
set cmd to editor & " " & posix_paths as Unicode text
tell application "Terminal"
tell window 1
do script cmd in selected tab
end tell
end tell
else
-- do nothing
end if
file_objects
end run
------------------------------------------------------------------------------------------
-- 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)) ¬
and (name of (info for the_alias)) does not end with ¬
"~" and (name of (info for the_alias)) does not start with "." then
set contents of file_objects_ref to contents of file_objects_ref ¬
& POSIX path of (the_alias as Unicode text)
end if
end repeat
end filefilter
------------------------------------------------------------------------------------------
-- 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
------------------------------------------------------------------------------------------
-- 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
------------------------------------------------------------------------------------------
-- Change mode on Emacs
------------------------------------------------------------------------------------------
on find_file()
tell application "System Events"
tell window 1 of application "Terminal" to activate
key code 7 using {control down} -- cntrol + x
key code 3 using {control down} -- cntrol + f
key code 0 using {control down} -- cntrol + a
key code 40 using {control down} -- cntrol + k
end tell
end find_file
------------------------------------------------------------------------------------------
-- Enter the file path
------------------------------------------------------------------------------------------
on enter_filePath(a_file)
set the clipboard to a_file as Unicode text
tell application "System Events"
tell window 1 of application "Terminal" to activate
key code 9 using {command down} -- command + v
key code 36 -- return
end tell
end enter_filePath
0 comments:
Post a Comment