01. Keyboard Viewerをトグル表示する2013-04-19

よくKeyboard Viewerを利用しますが、標準の仕様ではキーボードショートカットがなく呼び出し易いとは言えないと感じていました。

’Applescript Keyboard Viewer’の検索結果、
https://discussions.apple.com/thread/3311601?start=0&tstart=0
の、lleeさんの2つのコメントを参照してkeyboardViewerをインストール(場所は任意で良さそうです)、Applescriptは

tell application "System Events"
if exists (process "Keyboard Viewer") then
click button 1 of window 1 of process "Keyboard Viewer"
else
-- インストールしたpathに書き換えてください
do shell script "/Applications/System/keyboardViewer"
end if
end tell

上記をAppleScript Editorでアプリとして編集保存すれば、呼び出し/非表示がトグルで可能です。

02. Finder Windowの選択内容を特定アプリで開く2013-04-20

ツールバー上の呼び出しアプリ例
Finder Window内でフォルダやファイルを選択しておいて、赤枠内のようなアイコンを押すとそれぞれの特定アプリで開かれる、そんな用途です。AppleScriptで書いた呼び出し用アプリをWindowツールバーにアイコン登録し、スイッチボタンのように使います。

基本的なスクリプト例は2つあって、
tell application "Finder"
open (selection as alias) using "500GB:MyApplications:Picture:ComicViewer.app"
end tell

このようにアプリをパス指定するのがどんなアプリにも使える万能タイプです。

2つめは
tell application "Finder"
set theTarget to selection as alias list
end tell
tell application "VLC"
activate
open theTarget
end tell

こちらはアプリがAppleScriptに対応している場合の例です。VLCにはFinder上のselectionは直接渡せないので、theTargetという変数を設定します。なおこのスクリプトで複数ファイルを選択していると、全てプレイリストに渡されます。
最近のOSXではAutomatorでこのようなフローが作成できますが、コンテクストメニューやサービス経由となります。操作感の好みの問題なのですが、アイコンを押す方が直感的かなと思います。

AppleScriptを使うと柔軟に応用できるケースがあって、
tell application "Finder"
set theTarget to selection as alias list
end tell
tell application "GraphicConverter"
activate
slideshow theTarget
end tell

上記は複数選択項目のGraphicConverterによるスライドショーを行います。
本項の用途はアプリの機能と選択内容(単数/複数、ファルダ/ファイル)によって工夫が必要(selectionの処理)ですが、使用頻度に応じて作り甲斐はあると思われます。ただし、選択内容の参照方法がマックのガイドラインに沿っていないと思うので、個人使用にとどめるべきでしょう。

03. Macの起動・再起動時に補助動作を行う2013-04-21

補助と書きましたが、実際にどんな作業を行うかはユーザーによりますので、一例としてご覧ください。

まず起動時の方から。保存したアプリをログイン項目に登録して使っています。
tell application "Finder"
quit
delay 1
launch
open application file "XtraFinder.app" of folder "Applications" of startup disk
end tell
do shell script "killall -HUP KeyRemap4MacBook"

Finderをわざわざ再起動させていますが、これは主要なアプリを置いている外付HDを読み込み直させるためです。起動時の外付HDの読み込みタイミングがどうも遅いために使っているランチャーアプリの表示が変になる症状が続いたので、こんな羽目に(笑)。必然的にXtraFinderも起動し直しです。
KeyRemap4MacBookは立ち上がっていても設定を読み込んでいないようで、再起動すれば直ることがわかりました。
他にここでできることは、ログイン項目で扱うようなことを好みの順番で行わせる、などでしょうか。

次に再起動時、です。
set msg to "Ready to restart Mac now ?"
display dialog msg buttons {"Cancel", "Forward"} default button "Forward"
if (button returned of result) is "Forward" then
tell application "System Events"
set currentProcessNames to name of every application process whose visible is true and name is not "Finder"
repeat with i from 1 to count of currentProcessNames
try
tell application (item i of currentProcessNames) to quit
end try
end repeat
if login item "XtraFinder" exists then
delete login item "XtraFinder"
end if
end tell
tell application "Finder"
delete every item of folder "MainBoot:Users:darzilin:Library:Caches:"
delete every item of folder "MainBoot:Users:darzilin:Library:Preferences:iCab:Databases:"
restart
end tell
end if

行っている順番に
・再起動の確認
そもそも通常終了の確認ダイアログへの応答が面倒で作り始めたのですが、これは無いとやはり痛い目に遭いました(笑)。なのでせめてデフォルトをOKで設置。
・Finder以外のアプリを終了
・ログイン項目XtraFinder削除(現在は不要)
初期のXtraFinderはログイン項目に自動で登録される仕様でした。それがあると上の起動時スクリプトとバッティングすることを発見するのが大変でした。現在は仕様が改良されたので不要な作業ですが、まあ記念ですね(笑)。
・ユーザーキャッシュ、ブラウザの不要ファイル削除
残したFinder担当業務。ブラウザが肥大化するのを好まないので捨てています。

スクリプトからのrestartは待ち時間がありません。起動時にはゴミ箱に排出されたファイルをチラッと見るのが恒例です。

04. ファイルやフォルダのリネームと1階層上移動2013-04-22

リネームも移動も02.で述べたようなFinder上で選択しているファイルやフォルダが対象です。どちらも一時期何度も手作業に迫られて書きました。

まずリネームですが、クリップボードのテキストを対象の名前とします。

set new_name to the clipboard as text
tell application "Finder"
set select_item to (selection as alias)
if class of select_item = folder then
set name of select_item to new_name
else
set item_ext to name extension of select_item
if item_ext = "" then
set name of select_item to new_name
else
set name of select_item to new_name & "." & item_ext
end if
end if
activate
tell application "System Events"
keystroke return
end tell
end tell

最後のkeystroke return部分は変更後の名前を短くすることが多かったので付け加えたのですが、不要なら外してください。
この名前変更はredoが利きませんのでご注意ください。
これは単数の対象向けですが、複数対象ならpropertyを設定して(同一タスク名+日付や連番)のような名前変更ができますね。

次に1階層上移動ですが、親フォルダ内で子フォルダの▶マークをクリックした時に見えた対象を選択して実行する、そんな用途です。

tell application "Finder"
set alist to selection
repeat with theAlias in alist
set tgtFolder to parent of (parent of theAlias as alias) as alias
move theAlias to tgtFolder
end repeat
end tell

複数対象可能です。マウス操作でもできますが案外難しいので作りました。
こんな小さなスクリプトでも必要なものをちょこちょこ書き貯めておくと、いざという時助かります。

05. Home Pictureを設定する2013-04-23

頻繁にdesktop pictureを手動切替される方向け。切り替えた後で「いつもの絵がいいかな」という場合のためにHome Pictureを設定、戻れるようにしました。
本スクリプトでは、「メモ書き倉庫」さんの「AppleScriptであれこれする」から以下のハンドラを拝借して外部参照しています。
本スクリプトを動作確認される前に、当該ハンドラを皆さんのマックにスクリプトとして保存する必要があります。
http://memogakisouko.appspot.com/AppleScript.html#getModifierKeys

拙作スクリプトは以下。selection物なのでアプリとした場合は01.のようにウィンドウツールバーやランチャーから使って下さい。

property HomePict : ""
--次のファイルパス指定を書き換えてください
property ExHandler : load script file "MyHD:MyScripts:library:getModifierKeys.scpt"

on setDTP(xxx)
tell application "System Events"
tell current desktop
if picture rotation = 1 then
set picture rotation to 0
end if
set picture to xxx
end tell
end tell
end setDTP

tell application "Finder"
if ExHandler's getModifierKeys() contains "shift" then
set HomePict to selection as alias
set ppp to name of HomePict
display dialog "Home Picture set to " & ppp giving up after 2
my setDTP(HomePict)

else
if (selection is not {}) then
set tgtpict to (selection as alias)
my setDTP(tgtpict)

else
set ppp to name of HomePict
display dialog "Back to Home Picture " & ppp giving up after 2
my setDTP(HomePict)

end if
end if
end tell

動作はスクリプトの内容順に、起動時に
・シフトキーを押しながら画像選択→Home Pictureとして設定
・単純に画像選択→通常のdesktop pictureとして設定
・何もファイル選択無し→Home Pictureへ復帰
エラー処理を全く入れていませんが、わかって使う分にはそれでいいかなと。
ハンドラを使うと、予備処理や繰り返し部分を外に出してスクリプトをわかり易く短縮できますね。