在MacOS X底下的簡單BBS選台器

pashua.jpg

雖然說,MacOS X使用BSD核心,只要在任何一種終端機環境底下,調整一下色彩以及文字編碼,然後打一下telnet -8,就可以連到台灣普遍使用的文字介面BBS,不過還是有一點麻煩。所以我用了Apple Script以及Pashua,弄了一個簡單的BBS選台介面。這個介面是用Pashua畫出一個畫面,然後用Apple Script自動呼叫終端機(Terminal.app),執行連上BBS站的動作,所以,第一步,還是要先在終端機底下,做好可以正常上BBS的相關設定。

然後就是下載Pashua。Pashua是一套德國人寫的,可以與Perl、PHP、Python、Tcl、Rexx、Shell Script以及Apple Script配合的工具,可以用以上各種語言,快速在MacOS X中繪製程式圖形介面。下載之後,再用「應用程式」->「Apple Script」裡頭的「工序指令編寫程式」,輸入以下程式碼(這段程式碼也是根據Pashua的範例修改而成的):

(*

ATTENTION:

1. If you run this script from Script Editor and had a previous version of

Pashua installed, probably this script will use the older version instead of the

newer one. To avoid this misbehaviour, either run the script by doubleclicking it,

or remove any older versions. (Instead of removing, you can also rename it or put

in a non-standard location.)

2. If you run this script from Script Editor and never used Pashua before, you

will get an error message saying that Pashua could not be found. In this case,

either run this script by doubleclicking its icon, or copy the Pashua application

to /Applications or ~/Applications.

*)

— Create a configuration string which will be used to

— tell Pashua what types of GUI elements you need and

— which default values (if any) should be used

set config to "

# Lines starting with a hash character are

# comments, empty lines are ignored

# Set transparency: 0 is transparent, 1 is opaque

transparency=0.95

# Set window title

windowtitle=打BBS

# Define some introductory text

txt_type=text

txt_text=BBS快速連接器

# A separator line

# Define a password field

host_label=請輸入您所要連線的BBS的位置

host_type=combobox

host_width=390

host_option=ptt.cc

host_option=ptt2.cc

host_default=ptt.cc

# Add a cancel button – if you like, you can set the

# button label by uncommenting the second line below

cancel_type=cancelbutton

#cancel_label=If you click here, no values will be returned

# Define a default button – if you like, you can set the

# button label by uncommenting the second line below

default_type=defaultbutton

#default_label=Click here to return the values

"

— Call Pashua and save the resulting record in pashuaResult

set pashuaResult to pashua_run(config)

if cancel of pashuaResult is not "1" then

   tell application "Terminal"

      do script "telnet -8 " & host of pashuaResult & ";exit"

   end tell

end if

— Glue code for interfacing from AppleScript to Pashua. Written by

— Carsten Bl?m <bluem@q41.de>, 10/2003-12/2003, with an improvement

— contributed by Eddy Roosnek. You can use or modify this handler

— any way you like in your own scripts.

on pashua_run(config)

   

   — Create path for temporary file

   set AppleScript’s text item delimiters to ""

   set tmpfile to ((path to temporary items folder as string) & "Pashua_" & (characters 3 thru end of ((random number) as string)) as string)

   

   — Write temporary file and fill it with the configuration string

   set fhandle to open for access tmpfile with write permission

   write config to fhandle

   close access fhandle

   

   — Get temporary file’s POSIX path

   set posixtmpfile to POSIX path of tmpfile

   

   set diskPath to (path to startup disk as string)

   tell application "Finder" to set userPath to path to "cusr" as string

   set myself to (path to me as string)

   tell application "Finder" to set myParentPath to (container of alias myself as string)

   tell application "Finder" to set myPath to (myself as string)

   

   — Try to find Pashua application

   tell application "Finder"

      — Try to find it in this script application bundle

      if item (myPath & "Contents:MacOS:Pashua") exists then

         set pashua to myPath

         — Try to find it in this script’s parent’s path

      else if item (myParentPath & "Pashua.app") exists then

         set pashua to (myParentPath & "Pashua.app:")

         — Try to find it in global application folder

      else if item (diskPath & "Applications:Pashua.app") exists then

         set pashua to (diskPath & "Applications:Pashua.app:")

         — Try to find it in user’s application folder

      else if item (userPath & "Applications:Pashua.app") exists then

         set pashua to (userPath & "Applications:Pashua.app:")

      else

         display dialog "I can’t find the Pashua application." & return & "It looks like Pashua is neither in one of the standard locations nor in the folder this AppleScript is in." buttons {"OK"} default button 1 with icon stop

         return -1

      end if

   end tell

   

   — Append binary position inside app bundle to "regular" path

   — and convert path from HFS to POSIX representation

   set pashuabinary to (POSIX path of pashua) & "Contents/MacOS/Pashua"

   

   — Execute pashua and get the string returned

   set pashuaResult to do shell script ("’" & pashuabinary & "’ ‘" & posixtmpfile & "’")

   

   — Delete the temporary file

   tell application "Finder" to delete tmpfile

   

   — Check whether the dialog was submitted at all.

   — If this is not the case, return an empty list

   if pashuaResult = "" then

      return {}

   end if

   

   — Parse the result

   set AppleScript’s text item delimiters to return

   set resultLines to text items of pashuaResult

   set AppleScript’s text item delimiters to ""

   set recordComponents to {}

   repeat with currentLine in resultLines

      set eqpos to offset of "=" in currentLine

      if eqpos is not 0 then

         set varKey to (characters 1 thru (eqpos – 1) of currentLine as string)

         try

            set varValue to (characters (eqpos + 1) thru end of currentLine as string)

            — Quote any quotation marks in varValue with a backslash.

            — The proper way to do this would be a handler, but as

            — all code for interfacing to Pashua should be as compact

            — as possible, we rather do it inline

            set AppleScript’s text item delimiters to """

            set textItems to every text item of varValue

            set AppleScript’s text item delimiters to "\""

            set varValue to textItems as string

            set AppleScript’s text item delimiters to ""

         on error

            set varValue to ""

         end try

         copy (varKey & ":"" & varValue & """) to end of recordComponents

      end if

   end repeat

   

   — Return the record we read from the tmpfile

   set AppleScript’s text item delimiters to ", "

   return (run script "return {" & (recordComponents as string) & "}")

   

end pashua_run

輸入之後,然後存成「應用程式」,放在與Pashua同一個目錄下,就可以使用了。而如果想要方便使用的話,可以放在「Script資料夾」底下,另外也將Script Menu安裝起來,那麼就可以在螢幕上方的小選單中,快速使用這個BBS選台器了。執行的畫面如本文最上方的圖片。而如果想要新增站台選單的話,請在這個地方,加入站台列表即可:


# Define a password field

host_label=請輸入您所要連線的BBS的位置

host_type=combobox

host_width=390

host_option=ptt.cc

host_option=ptt2.cc

host_default=ptt.cc

2 thoughts on “在MacOS X底下的簡單BBS選台器

  1. 你說Pashua啊。簡單來說,就是你可以用一些Script語言,將某些參數傳入pashua, 這個小工具可以幫你畫出一個GUI畫面,然後在畫面當中的動作,可以傳出參數,傳回到原本的程式,或是呼叫其他的程式。簡單來說,不算是什麼開發環境,不過算是一個不錯的個人偷懶小工具。

Comments are closed.