[AppleScript] 最初のスクリプトの解説

2016.04.27

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

最初にやったこと

これまで基本的な文法やサンプルを紹介してきました。
なんとなく AppleScript が分かってきたところで、一番最初に書いたスクリプトの解説をしてみたいと思います。

↓最初のやつ
AppleScript で Safari を開く

スクリプト

set urls to {"https://www.google.co.jp", "http://www.yahoo.co.jp", "https://www.apple.com"}
set countOfUrls to count urls

tell application "Safari"
    activate
    tell window 1
        set firstTab to current tab
        repeat with i from 1 to countOfUrls
            set URL of document 1 to (item i of urls)
            if i = countOfUrls then exit repeat
            set current tab to (make new tab)
        end repeat
        set current tab to firstTab
    end tell
end tell

このスクリプトは Safari を開き、以下のホームページをそれぞれのタブで開くというものでした。

  • Google
  • Yahoo
  • Apple

解説

1 行目

set urls to {"https://www.google.co.jp", "http://www.yahoo.co.jp", "https://www.apple.com"}

urls という変数に以下の文字列を格納しています。

  • "https://www.google.co.jp"
  • "http://www.yahoo.co.jp"
  • "https://www.apple.com"

複数の値を含んでいるので、urls は List となります。

2 行目

set countOfUrls to count urls

countOfUrls という変数に urls の要素数を格納しています。
ここでの値は 3 となります。
count は List や Record の要素数を取得するコマンドです。
count の部分を length of に書き換えても同じ結果が得られます。

4, 5 行目

tell application "Safari"
    activate
    ...
end tell

アプリケーション Safari を起動しています。
activate はアプリを起動、もしくは最前面に表示するコマンドでしたね。

6 行目

tell window 1
    ...
end tell

Safari の window 1 に対して以降の命令を送信します。
window は Safari が持つ Class です。
また window は AppleScript 標準の Class としても定義されています。

Safari の window 定義
window n [see also Standard Suite] : A Safari window.

elements
contains tabs.

properties
current tab (tab) : The current tab.
AppleScript 標準の window 定義
window n [inh. item; see also Safari suite] : A window.

elements
contained by application.

properties
bounds (rectangle) : The bounding rectangle of the window.
closeable (boolean, r/o) : Whether the window has a close box.
document (document, r/o) : The document whose contents are being displayed in the window.
floating (boolean, r/o) : Whether the window floats.
id (integer, r/o) : The unique identifier of the window.
index (integer) : The index of the window, ordered front to back.
miniaturizable (boolean, r/o) : Whether the window can be miniaturized.
miniaturized (boolean) : Whether the window is currently miniaturized.
modal (boolean, r/o) : Whether the window is the application's current modal window.
name (text) : The full title of the window.
resizable (boolean, r/o) : Whether the window can be resized.
titled (boolean, r/o) : Whether the window has a title bar.
visible (boolean) : Whether the window is currently visible.
zoomable (boolean, r/o) : Whether the window can be zoomed.
zoomed (boolean) : Whether the window is currently zoomed.

見てさわった感じ、Safari の window は、標準の window を継承しているようでした。
(明示的に書かれているところは発見できず)
Safari の window は tabs という Element と current tab という Property が定義されています。

7 行目

set firstTab to current tab

firstTab という変数に現在のタブを格納しています。
current tab は Safari の window クラスが持つ Property で、その名の通り現在のタブを表します。
中身は tab で、こいつも Class です。

8 行目

repeat with i from 1 to countOfUrls
    ...
end repeat

ループを実行しています。
これは from の後の数字から to の後の数字まで繰り返しを行う表記でしたね。

countOfUrls の値は 3 なので、1 から 3 までの 3 回 ループを実行することになります。

9 行目

set URL of document 1 to (item i of urls)

ちょっと複雑ですね。
item i of urls で List 変数 urls の i 番目の要素を取り出しています。
その値(URL の文字列)を document 1URL Property に設定しています。
document は Class、URL は document の Property です。

document は window のアクティブなタブを表します。
こちらも AppleScript 標準のものと Safari 専用のものが存在します。

この行の命令で現在アクティブなタブの URL が設定され、その URL のページが表示されることになります。

10 行目

if i = countOfUrls then exit repeat

ここではループの制御を行っています。
ループ変数 i が countOfUrls と同じ場合、ループを抜けます。
後続の処理を実行したくないためです。

11 行目

set current tab to (make new tab)

タブを新しく作成し、現在のタブに設定しています(アクティブにしています)。
make は新しいオブジェクトを生成するコマンドです。
make new <Class> で Class のオブジェクトを作ります。 current tab は Safari の window が持つ Property です。

13 行目

set current tab to firstTab

繰り返しの前に設定した firstTab をアクティブにしています。
「タブの選択」と考えればイメージしやすいと思います。

やったね!

これで先頭のタブがアクティブな状態で、全ての URL がそれぞれのタブで開かれているようになります。

おまけ

Google Chrome で同じ処理

set urls to {"https://www.google.co.jp", "http://www.yahoo.co.jp", "https://www.apple.com"}
set countOfUrls to count urls
set startTabIndex to 1 (* タブを固定している場合はここの値を変える。固定していなければ 1 でいい *)

tell application "Google Chrome"
    activate
    tell window 1
        set active tab index to startTabIndex
        repeat with i from 1 to countOfUrls
            set URL of active tab to (item i of urls)
            if i = countOfUrls then exit repeat
            set t to (make new tab)
        end repeat
        set active tab index to startTabIndex
    end tell
end tell

リンク

ミレニアム・ファルコン製作日記 #14

14 号 表紙

mfd_14_1

パーツ

mfd_14_2

mfd_14_3

mfd_14_4

成果

mfd_14_5

今回の作業は以下の 2 つでした。

  • 主船倉ドアのライニング
  • 船倉壁(L)を取り付ける

主船倉左側の壁にも環状通路に続く円形ドアがあるので、第 10 号で作業したライニングと同様の作業を行いました。
また、回路端末室に入るための縦長のドアも取り付けています。

回路端末室は、ハン・ソロとレイアが初めてキスをした場所です。
「手が汚れてる」とか「悪党だからホレた」なんて言ってたところです。
このシーンのバックにはしっかりと、この縦長のドアが映っています。
このあとレイアはドアとは反対の方向に逃げて行ってしまいましたが、そちらは一体どこにつながっているんでしょうか。
今後の組み立てで判明するかもしれませんね。

Web 限定プレゼント

mfd_14_6

今回は Web からの定期購読早期申込みキャンペーンの特典として、オリジナル 作業マット が同梱されていました。
使い道が全然思いつかないけど嬉しい!

それではまた次回。

May the Force be with you!