LoginSignup
67
80

More than 5 years have passed since last update.

AppleScriptで使える技6つ

Last updated at Posted at 2014-02-04

はじめに

AppleScriptを書いた時に使った技をメモしていきます。

ウィンドウ

Finderのウィンドウ情報

以下のワンラナーを実行すると、Finderというアプリの現在開いているウィンドウ情報を表示します。

$ osascript -e 'tell application "System Events" to get properties of first window of application process "Finder"'

AppleScriptは、osascript -eというオプションを使うことで、ワンライナーを実行できます。

Mplayerのウィンドウの表示位置とウィンドウサイズ情報

ウィンドウ情報は、grepなどを駆使して個別に取得することができます。ちなみに、trは文字列変換コマンドで、grepは文字列検索コマンドです。

$ osascript -e 'tell application "System Events" to get properties of first window of application process "mplayer"' | tr ":" "\n" | grep -A 1 -e position -e size

ウィンドウの表示位置とサイズを指定する

開いているウィンドウの表示位置とサイズを指定します。

~/scpt/mplayer_window.scpt
tell application "System Events"
 set position of first window of application process "mplayer" to {xxx,xxx}
 set size of first window of application process "mplayer" to {xxx,xxx}
end tell

通常、AppleScriptは、テキスト上に記述し、osascriptで実行するという手順を使います。

$ osascript ~/scpt/mplayer_window.scpt

Finderのコピーウィンドウが開いているか否かを確認する

~/scpt/finder_copy_window.scpt
-- http://stackoverflow.com/questions/7921378/check-finder-activity
set thestatus to "0"
tell application "System Events"
    set theList to get the title of every window of process "Finder"
    repeat with theItem in theList
        if theItem contains "コピー" then
            set thestatus to "1"
        end if
    end repeat
end tell
thestatus

Finderでウィンドウタイトルがコピーとなっているものが開いている場合は1を返します。開いていない場合は0を返します。

なお、以下のShellScriptを実行すれば、コピーが終了した時点で、Growlに通知することが出来ます。

~/sh/copy_growl.sh
#/bin/bash

while :
do
    var=`osascript ~/scpt/finder_copy_window.scpt`
    case "$var" in
        "1" ) echo copying > /dev/null;;
        "0" ) growlnotify -t 'Finder' -m 'copy done'
            break ;;
    esac
done

exit 0

引数

AppleScriptでは、以下の書き方で引数を渡すことが出来ます。

on run argv
    return item 1 of argv
end run

なお、引数を渡すのが面倒なら、以下のようにShellScriptを介して実行するという手もあります。

~/sh/scpt_appleeditor_key.sh
#!/bin/bash
echo "
tell application \"AppleScript Editor\"
 activate
  tell application \"System Events\"
   delay 0.4
   keystroke \"$1\" using {command down}
  end tell
end tell
" | osascript -

キー入力

アプリにキー入力を渡すなら、以下の様な書き方をします。速度はdelayで指定します。keystrokekey codeで指定することも出来ます。

~/scpt/chrome_key.scpt
tell application "Google Chrome"
  activate
  tell application "System Events"
    delay 0.5
    keystroke "mba-hack"
  end tell
end tell
sample
keystroke "v" using {command down, option down, shift down}

key code 126

key codeを調べるにはxevが使えます。

$ xev | grep -A2 --line-buffered '^KeyRelease' | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'

なお、AppleScript Editorで書くと、保存するときにアプリケーション形式で作成することもできます。

ダウンロード

面白そうなスクリプトがあれば、是非教えて下さい。

https://github.com/Zettt/LaunchBar-Scripts

67
80
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
67
80