以下のスクリプトはすべてapplescriptなので、スクリプトエディタ上で実行前にapplescriptが選択されていることを確認してください。

- macOS 10.14.4
- AppleScript 2.7
- windowの数を数える
- documentの数を数える
- documentの作成、計数
- webページを開いてテキストフィールドに文字列を入力
- サブルーティンの作成
- Safariで新しいウィンドウを開く
- Safariのタブを閉じる&移動
windowの数を数える
windowとdocumentの違いがよくわからない。tell application "Safari" to count window
以下も同じ
tell application "Safari"
count window
end tell
documentの数を数える
tell application "Safari" to count document
以下も同じ
tell application "Safari"
count document
end tell
documentの作成、計数
- Safariのdocumentの数を数える
- 新しいdocumentを作成
- 新しいdocumnent上でgoogle.comを開く
tell application "Safari"
count document
make new document
open location "https://google.com"
end tell
以下も同じ
tell application "Safari"
count document
make new document with properties {URL:"https://google.com"}
end tell
webページを開いてテキストフィールドに文字列を入力
class, name, idで指定できる。 classで入力フィールドを絞り込む場合tell application "Safari"
open location "https://google.com"
delay 2
do JavaScript "document.getElementsByClassName('gLFyf')[0].value = 'what is applescript'" in document 1
end tell
nameで入力フィールドを絞り込む場合
tell application "Safari"
open location "https://google.com"
delay 2
do JavaScript "document.getElementsByName('q')[0].value = 'what is applescript'" in document 1
end tell
文字列入力して検索。先ほどと同様、nameで絞り込みもできる。
tell application "Safari"
open location "https://google.com"
delay 2
do JavaScript "document.getElementsByClassName('gLFyf')[0].value = 'what is applescript'" in document 1
do JavaScript "document.getElementsByClassName('gNO89b')[0].click()" in document 1
end tell
classでdivタグを絞りこみ、その中のinputタグのフォームに”しば犬”と入力する
set search_query to "しば犬"
tell application "Safari"
activate
open location "https://www.bing.com/"
delay 2
do JavaScript "document.querySelectorAll('div.b_searchboxForm input')[0].value = " & quoted form of search_query in document 1
end tell
サブルーティンの作成
# 定義されたサブルーティンの実行
openSafariWith("https://www.bing.com")
# サブルーティンの定義
on openSafariWith(myUrl)
tell application "Safari" to open location myUrl
end openSafariWith
稼働中のプロセスの名前とウィンドウ数を列挙
tell application "System Events"
repeat with theProcess in processes
tell theProcess
set processName to name
set theWindows to windows
end tell
set windowsCount to count of theWindows
log processName
log " windows: " & windowsCount
end repeat
end tell
webページ中の特定のタグ内のHTMLを取得・出力する
テキストフィールドを絞り込む時に、idが無い場合はclass名等から何段階かで絞り込んでいくこともあるので、そういう場合にinnerHTMLを出力してみれば絞り込みが間違っていないか確認できる。
tell application "Safari"
open location "https://www.yahoo.co.jp"
delay 2
do JavaScript "document.getElementById('srchtxtBg').innerHTML" in document 1
end tell
キーストローク
- googleへアクセス
- “Shibainu”と入力
- tabキーを押して検索ボタンへ移動
- returnキーを押して検索
tell application "Safari"
activate
open location "https://google.com"
delay 3
tell application "System Events"
tell application process "Safari"
keystroke "Shibainu"
keystroke tab
keystroke return
end tell
end tell
end tell
Safariで新しいウィンドウを開く
アクティベートしてからシステムイベント –> プロセス選択をして、メニュー操作をする。(このサイトのコピペ:http://hints.macworld.com/article.php?story=20031203102553678)# 新しいウィンドウを開く
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Window" of menu "File" of menu bar 1
end tell
end tell
Safariのタブを閉じる&移動
アプリケーションSafariを指定 –> ウィンドウを指定 –> tab 1とかtab 2とかでタブを指定できる。# 最初のタブを閉じる
# 最初のタブに移動する(閉じる前に2番目だったタブへ移動)
tell application "Safari"
activate
tell front window
close tab 1 without saving
set current tab to tab 1
end tell
end tell