Search and say

Hello all!

I try for a few days to write a script that:

1- Ask for a word (display dialog)

2- look on Wikipedia for the definition

3- ? copy and paste the definition (in textedit?) or else??? (Better Idea???)

4- say the definition with the voice by default

5- ask if you want another search

6- and if not, just say thanks and goodbye

the main problem is that i dont know how to get the text out of the wikipedia web site to put it in a text file.

i have a script that i have work on, but cannot go very much than this:




try
	
	say "Bonjour! Entrez un mot pour votre recherche sur Wikipédia" using "Chantal"
	
	set mot to text returned of (display dialog "Entrez un mot pour votre recherche sur Wikipédia" default answer "" buttons {"Rechercher", "Annuler"} default button 1)
	
	say "Voyons ce que veut dire le mot ,," & mot & ",,,sur Wikipédia" using "Chantal"
	
	open location "https://fr.wikipedia.org/wiki/" & mot
	
	repeat
		
		delay 10
		
		
		activate
		
		say "Veux-tu faire une autre recherche?" using "Chantal"
		
		if button returned of (display dialog "Veux-tu faire une autre recherche?" buttons {"Oui", "Non"} default button 1 cancel button 2 with title "Fait un choix" with icon caution) is "Oui" then
			
			say "Entrez un nouveau mot" using "Chantal"
			
			set mot to text returned of (display dialog "Entrez un nouveau mot" default answer "" buttons {"Rechercher", "Annuler"} default button 1)
			
			say "Voyons ce que veut dire le mot ,," & mot & ",,,sur Wikipédia" using "Chantal"
			
			open location "https://fr.wikipedia.org/wiki/" & mot
			
			
		end if
		
	end repeat
	
end try

delay 1

say "Bonne journée et à la prochaine!" using "Chantal"

thank you very much if you can help me!

and have a nice day!

You may try :


set p2d to path to desktop folder as text # Adjust the destination folder to fit your needs


say "Bonjour! Entrez un mot pour votre recherche sur Wikipédia" using "Virginie" --"Chantal"
set the clipboard to ""

repeat
	tell application (path to frontmost application as string) to set mot to text returned of (display dialog "Entrez un mot pour votre recherche sur Wikipédia" default answer "" buttons {"Annuler", "Rechercher"} default button 2)
	
	say "Voyons ce que veut dire le mot « " & mot & " » sur Wikipédia" using "Virginie" --"Chantal"
	
	open location "https://fr.wikipedia.org/wiki/" & mot
	delay 5
	try
		tell application "Safari" to activate
		tell application "System Events" to tell process "Safari"
			set frontmost to true
			tell window 1
				set theTitle to its title
				keystroke "a" using {command down}
				keystroke "c" using {command down}
			end tell
		end tell
		delay 2
		tell application "Safari"
			--close window theTitle
			quit
		end tell
		tell application "TextEdit"
			activate
			make new document
		end tell
		tell application "System Events" to tell process "TextEdit"
			set frontmost to true
			tell window 1
				keystroke "v" using {command down}
			end tell
		end tell
		set theName to theTitle & ".rtfd"
		if theName contains "/" then set theName to my remplace(theName, "/", "_")
		set thePath to p2d & theName
		tell application "System Events"
			if not (exists disk item thePath) then make new file at folder p2d with properties {name:theName}
		end tell
		tell application "TextEdit"
			save document 1 in alias thePath
			close document 1
		end tell
		
		#=====
		# Instructions added to say the grabbed text.
		# Code saying the text
		with timeout of 3600 seconds
			set theText to the clipboard as text
			say theText using "Virginie" -- "Chantal"
		end timeout
		#=====
		
	on error
		# Here if Wikipédia found nothing
		tell application "Safari" to quit
	end try
	say "Veux-tu faire une autre recherche?" using "Virginie" --"Chantal"
	
	try
		tell application (path to frontmost application as string) to set choix to button returned of (display dialog "Veux-tu faire une autre recherche?" buttons {"Non", "Oui"} default button 2 cancel button 1 with title "Fais un choix" with icon caution)
	on error number errNbr
		if errNbr is -128 then exit repeat
		error "Unknown error"
	end try
	set the clipboard to ""
	say "Entrez un nouveau mot" using "Virginie" --"Chantal"
end repeat

delay 1

say "Bonne journée et à la prochaine!" using "Virginie" --"Chantal"



#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#=====

(1) I changed the voice because Chantal isn’t available on my machine.
(2) I dislike that but I was forced to urge Safari to quit after displaying a page from Wikipedia.
I don’t understand why but without that, at a second question the copy from the page failed.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 7 mars 2016 12:41:59

Wow that was fast !

Thank you …i gonna take a look on it and studie it soon!

Thanks again!

Ok the only thing that is missing is how to take the text in the buffer (in memory) and tell the script to SAY the text or take the file created by the script and read it…

or maybe it’s not possible?

Thanks again…

RixMax

As is, the script saved the complete page from Wikipedia.
I’m not sure than saying it would be really useful.

I added instructions saying it in the script.

Look at the block :

		#=====
		# Instructions added to say the grabbed text.
		# Code saying the text
		with timeout of 3600 seconds
			set theText to the clipboard as text
			say theText using "Virginie" -- "Chantal"
		end timeout
		#=====

I tested searching for “cheval”.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 10 mars 2016 12:03:38

:slight_smile: Thanks again!!! do you know a good book ( in french ) on AppleScript? :confused:

Have a nice Day/Bonne Journée!

RixMax

Thanks for the feedback.

It seems that french editors aren’t interested by AppleScript.
I don’t remember ever seeing a book in french about it.

In my library I have I have two books dedicated to AppleScript:

AppleScript: The Definitive Guide, 2nd Edition - 2006
and
Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X (Learn (Apress)) 3rd Edition : 2009

As you may see, they aren’t really up to date but I don’t know more recent books.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) samedi 12 mars 2016 16:40:58

Thank you

By the way what is the line:

with timeout of 3600 seconds

   end timeout

?

The AppleScript User Guide is your friend :

[i]with timeout
Specifies how long AppleScript waits for a response to a command that is sent to another application.
Syntax
with timeout [ of ] integerExpression second[s] [ statement ]…
end [ timeout ]
Placeholders
integerExpression
The amount of time, in seconds, AppleScript should wait before timing out (and interrupting the command).
statement
Any AppleScript statement.
Examples
The following script tells TextEdit to close its first document; if the document has been modified, it asks the user if the document should be saved. It includes the statement with timeout of 20 seconds, so that if the user doesn’t complete the close operation within 20 seconds, the operation times out.
tell application “TextEdit”
with timeout of 20 seconds
close document 1 saving ask
end timeout
end tell
Discussion
When a command fails to complete in the allotted time (whether the default of two minutes, or a time set by a with timeout statement), AppleScript stops running the script and returns the error “event timed out”. AppleScript does not cancel the operation”it merely stops execution of the script. If you want the script to continue, you can wrap the statements in a try (page 262) statement. However, whether your script can send a command to cancel an offending lengthy operation after a timeout is dependent on the application that is performing the command.
A with timeout statement applies only to commands sent to application objects, not to commands sent to the application that is running the script.

Control Statements Reference
In some situations, you may want to use an ignoring application responses statement (instead of a with timeout statement) so that your script needn’t wait for application commands to complete. For more information, see “considering and ignoring Statements” (page 244).[/i]

I used it here because when I tested the script I got huge Web pages which require a lot of time to be said. So I was afraid that we may get : error “event timed out” if the task require more than the default duration of two minutes.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 14 mars 2016 10:43:36