Archive for the 'Terminal.app' Category
Terminal.appの全Window内のTabのうち、指定タイトルのものをクローズするAppleScriptです。
Mac OS X 10.7上のTerminalの各ウィンドウには、任意のタイトルを指定できるようになっています。Mac OS X 10.6上では、カスタマイズしたタイトルについてはAppleScriptからは参照するだけでした。

| スクリプト名:teminal.appでtabを操作する |
tell application "Terminal" tell window 1 tell tab 1 set custom title to "ぴよまる" end tell tell tab 2 set custom title to "ぴよぴよ" end tell end tell end tell
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|


Tabのクローズについては、Window内のTabを指定してclose命令を実行してみても、クローズはできない状況です。GUI Scriptingで行うのもアレなので(最前面に持ってこないと実行できないというのは、ちょっとめんどう)、別の方法でクローズを実現することに。
Terminal.appの環境設定で、シェル終了後にWindowをどうするか指定することができます。


「ウィンドウを閉じる」「シェルが正常終了した場合には閉じる」「ウィンドウを閉じない」の3つから選択可能。

これを「ウィンドウを閉じる」に設定しておき、指定Tabに「exit」コマンド(シェルコマンド)を送ることで、指定Tabをクローズできるようになります。
まあそんなわけで、Tabのカスタム名称を文字列で指定して該当するTabをすべてクローズできるようになったわけですが、これがどの程度有用かは…………ちょっと分かりません。
| スクリプト名:Terminalで全Window中の全Tabのうち、指定タイトルのものをクローズする |
set targTabTitle to “ぴよまる”
tell application “Terminal” set tabRef to a reference to (every tab of every window whose custom title is equal to targTabTitle) repeat with i in tabRef do script “exit” in i end repeat end tell |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app, 10.7対応 | No Comments »
miの最前面のドキュメント上で選択中のテキストをファイルに書き出して、perlのプログラムとしてTerminalで実行するAppleScriptのバグ修正版です。
最初のバージョンでは、改行コードがCRの状態で書き出されていたため、改行コードをLFに置換してから実行するようにしました。
初版では、選択範囲にコメント行が含まれているとエラーになっていたりしましたが、このバージョンではそういうことはありません。
| スクリプト名:miで選択中の内容をファイルに書き出してperlのプログラムとしてterminalで実行 v2 |
tell application “mi” tell front document set this_data to selection –エラー対策 if this_data = “” then display dialog “文字列が何も選択されていません” buttons {“OK”} default button 1 with icon 1 with title “エラー” return end if set this_data to this_data as Unicode text end tell end tell
–改行コードがCRになっている部分をLFに置換(v2で追加) set this_data to repChar(this_data, ASCII character 13, ASCII character 10) of me
set tmpPath to path to temporary items from user domain set fStr to (do shell script “date +%Y%m%d%H%M%S”) & “.pl” set fPath to (tmpPath as string) & fStr
write_to_fileUTF8(this_data, fPath, false) of me do shell script “sync”
set sText to “perl -w “ & quoted form of POSIX path of fPath doComInTerminalWindow(sText) of me
on doComInTerminalWindow(aCMD) tell application “Terminal” set wCount to count (every window whose visible is true) – By wayne melrose – Re: New window in Terminal.app if wCount = 0 then –ウィンドウが1枚も表示されていない場合 do script aCMD else –すでにウィンドウが表示されている場合 do script aCMD in front window end if end tell end doComInTerminalWindow
–ファイルの追記ルーチン「write_to_file」 –追記データ、追記対象ファイル、boolean(trueで追記) on write_to_fileUTF8(this_data, target_file, append_data) try set the target_file to the target_file as text set the open_target_file to open for access file target_file with write permission if append_data is false then set eof of the open_target_file to 0 write this_data to the open_target_file as «class utf8» starting at eof close access the open_target_file return true on error error_message try close access file target_file end try return error_message end try end write_to_fileUTF8
–文字置換ルーチン on repChar(origText, targStr, repStr) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app, 10.5対応, 10.6対応, mi, 10.7対応 | 2 Comments »
miでオープン中の最前面のドキュメントで選択中のテキストを書き出して、Terminalでperlのプログラムとして実行するAppleScriptです。
コメント行を含む内容を実行するとエラーになるのはなぜなんでしょう?(Perl詳しくないもんで)


| スクリプト名:miで選択中の内容をファイルに書き出してperlのプログラムとしてterminalで実行 |
tell application “mi” tell front document set this_data to selection –エラー対策 if this_data = “” then display dialog “文字列が何も選択されていません” buttons {“OK”} default button 1 with icon 1 with title “エラー” return end if set this_data to this_data as Unicode text end tell end tell
–set this_data to “print ‘test’;”
set tmpPath to path to temporary items from user domain set fStr to (do shell script “date +%Y%m%d%H%M%S”) & “.pl” set fPath to (tmpPath as string) & fStr
write_to_fileUTF8(this_data, fPath, false) of me do shell script “sync”
set sText to “perl -w “ & quoted form of POSIX path of fPath doComInTerminalWindow(sText) of me
on doComInTerminalWindow(aCMD) tell application “Terminal” set wCount to count (every window whose visible is true) – By wayne melrose – Re: New window in Terminal.app if wCount = 0 then –ウィンドウが1枚も表示されていない場合 do script aCMD else –すでにウィンドウが表示されている場合 do script aCMD in front window end if end tell end doComInTerminalWindow
–ファイルの追記ルーチン「write_to_file」 –追記データ、追記対象ファイル、boolean(trueで追記) on write_to_fileUTF8(this_data, target_file, append_data) try set the target_file to the target_file as text set the open_target_file to open for access file target_file with write permission if append_data is false then set eof of the open_target_file to 0 write this_data to the open_target_file as «class utf8» starting at eof close access the open_target_file return true on error error_message try close access file target_file end try return error_message end try end write_to_fileUTF8
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app, 10.6対応, 10.7対応 | 2 Comments »
Terminal.appでコマンドを指定するAppleScriptです。コマンドの文字列を渡すとTerminal.app上で実行します。
ふつう、shell commandを実行する場合にはAppleScriptのdo shell scriptコマンドを使うのが常ですが、どーーーーーーーしても仕方のない場合に、Terminal.appをコントロールすることが、100%ないとも言い切れません。ただ、実際に使っている例を見ても「ああ、do shell scriptを知らなかったんだな〜」というものばかりで、必然性が感じられたケースは皆無でした。
Terminal.appのAppleScript用語辞書を調べると、do scriptコマンドがあり、パラメータとしてWindowを指定する必要があります。返り値はとくにありません。Terminal.appのウィンドウから結果を取り出すなど、別のAppleScriptを併用するなどしてみてください。
しかし、Windowがすべてクローズされた状態であれば、新規にWindowをオープンしなくてはなりませんが、make new windowなどの記述を行ってもwindowは新規作成されません。
USのAppleScript Users MLの過去ログを調べてみたところ、非常に微妙な記述によって新規Windowの作成指定が行えることが判明。あまりに微妙なので、プログラムを見ただけでは(コメントがないと)何を意図しているのか分からないほどです。
いまのところ、お世話になったことのない本プログラムですが、どこかで役立ったらコメント欄で教えてください。
| スクリプト名:Terminal.appで指定コマンドを実行する |
set aCMD to “ls -la” doComInTerminalWindow(aCMD) of me
on doComInTerminalWindow(aCMD) tell application “Terminal” set wCount to count (every window whose visible is true) – By wayne melrose – Re: New window in Terminal.app if wCount = 0 then –ウィンドウが1枚も表示されていない場合 do script aCMD else –すでにウィンドウが表示されている場合 do script aCMD in front window end if end tell end doComInTerminalWindow |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app, 10.5対応, 10.6対応, 10.4対応 | 1 Comment »
Terminal.appの最前面のWindowで、その内容を取得するAppleScriptです。
一般的には、do shell scriptでshellコマンドを実行して結果を取得する方が有用性が高いですが、まれにTerminal.appをコントロールする必要が出てきたりします(3年に1度ぐらいかな、、、)。
とりあえず、指定ウィンドウ中の、
表示されている範囲の内容を取得するならcontents
コマンド履歴すべてを取得するならhistory
でアクセスできます。ただ、本当にTerminal.appをコントロールする機会は日常的に発生しないので、活用できるのかどうか謎ですが、、、、
| スクリプト名:Terminalから内容を取得する |
tell application "Terminal" tell window 1 set aCon to history –すべてのヒストリの内容を取得 set bCon to contents –Window内に表示されている文字(表示されている分だけ) end tell end tell
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app, 10.5対応, 10.6対応 | No Comments »
Finderの最前面のWindowで表示しているパスをTerminalでオープンするというものです。ただし、Finder上のフォルダ名の中には……AppleScriptで処理しようとしただけでクラッシュを誘発するような種類の文字を含んでいるパターンもあるので、それに十分に備えた処理を行うようにしています。
| スクリプト名:Finderの最前面のパスをTerminalで開く(強化ver4) |
–OSバージョンおよびUI Element Scriptingのチェック tell application “System Events“ get system attribute “sysv“ if result is greater than or equal to 4149 then – Mac OS X 10.3.5 if UI elements enabled then – else display dialog “UI Element Scriptingが動作していません。” & return & return & “「システム環境設定」の「ユニバーサルアクセス」で、「補助装置を使用可能にする」のチェックボックスをオンにしてください。” with icon stop if button returned of result is “OK” then tell application “System Preferences“ activate set current pane to pane “com.apple.preference.universalaccess“ return end tell end if end if else beep display dialog “このコンピュータでは本スクリプトを実行できません。” & return & return & “Mac OS X 10.3.5以降が必要です。” with icon caution buttons {”Quit“} default button “Quit“ return end if end tell
– Terminal.appのウィンドウ表示エンコーディング設定のチェック set aRes to do shell script “defaults read com.apple.terminal StringEncoding“ set aRes to aRes as number if aRes is not equal to 4 then display dialog “ターミナルの文字エンコーディングはUnicodeに設定しておいてください。” buttons {”OK“} default button 1 return end if
– ここからメイン tell application “Finder“ tell Finder window 1 set a to target end tell set aa to a as alias end tell
set aP to POSIX path of aa method2(aP) of me
– 以下、サブルーチン
– 指定のフォルダをTerminalでOpenする on method2(aPath) set the clipboard to “cd “ activate application “Terminal“ tell application “System Events“ tell process “Terminal“ –先行してコマンドラインに入力されていた内容をキャンセル keystroke “u” using {control down} delay 0.5 keystroke “v” using {command down} end tell delay 0.5 end tell set the clipboard to aPath delay 0.5 –エスケープテキストをペースト(フルパス一括で) activate application “Terminal“ tell application “System Events“ tell process “Terminal“ click menu item 5 of menu 1 of menu bar item 4 of menu bar 1 end tell keystroke return end tell tell application “Terminal“ do script “clear” in window 1 end tell end method2
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Finder, Terminal.app, GUI Scripting, shell script | 2 Comments »
Finderの最前面のウィンドウのパスをカレントにしたTerminalをオープンするというScriptはよく見かけるものですが、その逆に……Terminal上のカレント・ディレクトリをFinderでオープンするというAppleScriptです。Script Menuに入れて使っています。
| スクリプト名:ターミナルのcurrentをFinderでひらく.scpt |
tell application “Terminal“ do script “pwd” in window 1 tell window 1 set a to contents end tell set aList to paragraphs of a set aItm to length of aList repeat set tmpLine to item aItm of aList if tmpLine is “” then set aItm to aItm - 1 else exit repeat end if end repeat set targetDir to item (aItm - 1) of aList set targetDir to targetDir as Unicode text –do script (”cd ” & targetDir) end tell
tell application “Finder“ activate set macOStargetDir to retMacOSpath(targetDir) of me (* set tmpPath to macOStargetDir as Unicode text display dialog tmpPath *) set sDisk to (name of startup disk) & “:“ try make new Finder window to alias sDisk set the current view of the front Finder window to column view end try tell window 1 set target to macOStargetDir end tell end tell
on retMacOSpath(aFile) set aliasFile to {} set aPath to POSIX file aFile set aPath to aPath as alias set aPath to aPath as string set aPath to aPath as Unicode text set aliasFile to aPath return aliasFile end retMacOSpath |
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Finder, Terminal.app | No Comments »
Script Menuに入れて使うことを前提に作ったAppleScriptです。Terminalで作業を行っていて、もう一枚同じパスを表示しているウィンドウを作成したい場合、このScriptをScript Menuから呼び出せば、同じパスをカレント・ディレクトリにしたTerminalが開きます。
| スクリプト名:同じパスの画面をもう1枚ひらく.scpt |
tell application “Terminal“ do script “pwd” in window 1 tell window 1 set a to contents end tell set aList to paragraphs of a set aItm to length of aList repeat set tmpLine to item aItm of aList if tmpLine is “” then set aItm to aItm - 1 else exit repeat end if end repeat set targetDir to item (aItm - 1) of aList do script (”cd ” & quoted form of targetDir) end tell
|
|
▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に
|
Posted in アプリケーション操作(app control), Terminal.app | No Comments »