Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problems combining JavaScript with AppleScript in Safari

've been dabbling in AppleScript for some time but I still hit a brick wall now and then, especially when trying to interact with webpages through JavaScript, which is what happened to me now.

I learn Chinese and subscribed to a "Word of the Day" service. I wanted to write a script that would automatically check up the definition of the word and download the audio so that I can add the word to my flashcard app.

There is a free dictionary http://linedictionary.naver.com/dict.html#/cnen/search/ with audio files available. However, I'm having problems controlling the page with Javascript. I manage to input the word into the search box, but when I try to make Applescript click the search button it doesn't work. Also, I have no idea how to manipulate the type of search (selecting one of the four dots "all results", "CHI-ENG" "ENG-CHI" or "Usage examples").

This is what my script looks like


set NewWord to "朋友"
  tell application "Safari"
   do JavaScript "document.getElementById('ac_input').value='" & NewWord & "'" in ChinDictWindow -- this enters the search word into the search box
  delay 1
   do JavaScript "document.getElementById('btn_query').click()" in ChinDictWindow -- this should trigger off the search, but doesn't
  delay 1
  end tell


the ChinDictWindow is the corresponding tab of the safari window id#

I don't understand why the "document.getElementById('btn_query').click()" doesn't work. Is there something wrong with the syntax? Also, as you can see, there is nothing in the script to choose the search type. I was at a loss here. Does anyone have any suggestions on how to do it?

I'd appreciate any suggestions.

OS X Mavericks (10.9.5)

Posted on Mar 27, 2015 4:51 PM

Reply

There are no replies.

Problems combining JavaScript with AppleScript in Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.