My_Play_Lists ~BlackCatWhiteTail’s blog~

Talk About My Favorite Songs

applescriptでgoogle検索してみる(do JavaScriptの利用)。

はじめに

 google検索をapplescriptにやらせたい人はそれなの数いると予想され(1)、今回やってみた。

(1)javascript - How to automatically login to the website by Chrome with Applescript? - Stack Overflow, https://stackoverflow.com/questions/33602525/how-to-automatically-login-to-the-website-by-chrome-with-applescript

  ウェブページ内のボタンをクリックするといったことは、applescript内でdo JavaScriptコマンドを用いるのが自然だと思う(2)。 (2)Safariを自由自在に操作するためのAppleScript集 | Macの手書き説明書, https://mac-tegaki.com/basic-usage/safari-applescript.html

 以下の理由により、かなりぼんやりした知識なので(他のSNSではなく)ここに書くことにした。 ・googleの仕様変更で使えなくなる可能性がある。
・私自身がweb関係の知識に乏しい。

準備

do JavaScriptコマンド

 applescriptを開き、shift + command + oで用語説明を開き、safariの用語説明にdo JavaScriptがある。

 なので、do JavaScriptを使う場合、tell application "Safari"end tellに挟まれたブロック内で行う。

tell application "Safari"
    do JavaScript ~略~
end tell

要素の詳細を表示する方法

 要素とは、たとえば押下したいボタンがあるとして、そのボタンがhtml内のどのように書かれているかということである。

 ここに書かれているように、safariにおいては調べたいボタン上で右クリックし、コンテクストメニューの要素の詳細を表示で調べることができる(3)。

(3)Safariであらゆるページに自動ログインする方法 - ザリガニが見ていた…。, https://zariganitosh.hatenablog.jp/entry/20110920/auto_login_to_every_page

ボタンをクリックする

 上記の「要素の詳細」でボタンのid(またはname、class)を調べ、その要素を.clickでクリックする方法を見かける。しかし、google検索では使えない。

~略~
~~ document.getElementById('" & theId & "').click()
~略~

google検索では、.submit()を用いる。

~略~
~~ document.forms[0].submit()
~略~

フォームに文字列を入力する。

「要素の詳細」でボタンのid(またはname、class)を調べ、そのフォームのvalueに文字列を代入する。

~略~
~~ document.forms[0].term.value=1
~略~

実践

フォームに文字列 'test' を入力。

 google検索のページで、検索フォーム(検索したい文字列をいれるところ)にポインタをあわせ、要素の詳細を調べると、htmlは以下のようになっている。

<input class="gLFyf gsfi" jsaction="paste:puy29d;" maxlength="2048" name="q" type="text" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="検索" value="" aria-label="検索" data-ved="0ahUKEwjP9YvWse7xAhWF1GEKHdJwDccQ39UDCAY">

 このことから、class名が"gLFyf gsfi"ということがわかり、今回はこれを用いる。

do JavaScript "document.getElementsByClassName('gLFyf gsfi')[0].value='test'" in document 1

検索実行

 上記のごとく、「google検索」のボタンをクリックするのではなく、submitするという形で行う。

do JavaScript "document.forms[0].submit()" in document 1

コード全体

tell application "Safari"
    
    do JavaScript "document.getElementsByClassName('gLFyf gsfi')[0].value='test'" in document 1
    
    do JavaScript "document.forms[0].submit()" in document 1
    
end tell

初めにgoogleのトップページを開いていないとなりません。

f:id:BlackCatWhiteTail:20210721085308g:plain

Translated with DeepL

Introduction

 There are probably a lot of people who would like to have applescript do their google searches (1), so I decided to give it a try.

I think it is natural to use the do JavaScript command in applescript for things like clicking on buttons in a web page (2).

 For the following reasons, I decided to write about it here (instead of other social networking sites) since my knowledge is quite vague. I'm not sure if this is a good idea or not. This is the first time I've ever used this site.

Preparation

do JavaScript command

 Open applescript, open the glossary with shift + command + o, and find do JavaScript in the glossary of Safari.

 So, if you want to use do JavaScript, do it in the block between tell application "Safari" and end tell.

How to show the details of an element

 An element is, for example, a button that you want to press, and how the button is written in html.

 As described here, in Safari, you can right-click on the button you want to check and select `show element details' from the contextual menu (3).

Clicking on a button

 I see a way to look up the id (or name, or class) of a button in the "element details" section above, and then click on that element with .click. However, it doesn't work for google search.

For google search, use .submit().

Enter a string into the form.

Check the id (or name, or class) of the button in the "element details", and assign the string to the value of the form.

Practice

Enter the string 'test' in the #### form.

 On the google search page, move the pointer to the search form (where you put the string you want to search for) and examine the details of the element.

From this, we know that the class name is "gLFyf gsfi", and we will use it in this case.

Execute search

 As above, do not click on the google search' button, butsubmit'.

 このウェブサイトについて

 練習用のサイトです.

 自分の好きな曲について感想を述べます.

 また,世に役に立つかもと考えたことも書きます.

 

TOP 1 To 5

Title Artist Album
ロックバンド 中村中 私を抱いて下さい
犬の生活 SEX MACHINEGUNS SEX MACHINEGUNS
Megalovania Toby Fox UNDERTALE Soundtrack
Bokura No Fly Away Base Ball Bear Bokura No Fly Away - single
人魚の檻 陰陽座 迦陵頻伽(Ka Ryo Bin Ga