Nov 25, 2010

Character Code and Different System Language

When AppleScript Editor saves a script file including Japanese words as TEXT file format, that character code is UTF-16LE. I figure out so. But in a different system language, the character code is not the same one. Would you look at following a screenshot;


A file that was saved in Japanese language environment is Shift-JIS. The same contents’ file that was saved in English language is UTF-16LE. Each file was saved as different character code.

I’m working usually in English language system. When I receive files that were saved in Japanese language environment, I have to be wary though the scripts are able to run.

Nov 24, 2010

UTF16-BE or UTF16-LE?


I hope that character code of files AppleScript Editor saves would be only UTF16-LE. I hate a mixed state of ascii and it. Honestly speaking, UTF8 is the best. I tweeted like so.

After I tweeted, there was a person who send message me about my tweet. He said that the character code of AppleScript Editor as text file format is UTF16-BE. Uhhh... but the result of hexdump shows little-endian BOM.


What am I missing here? Which is correct?

Sep 29, 2010

Associated Script With Emacs Again

I remade a script to open selected files on Finder with Emacs. The script has two mode; one is after making a new process then add files there and another one is add files to already existing process. When selecting “make new process mode,” if there were any process in the most front of terminal window, will prepare for a new tab.
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