0

I have problem with special characters in AppleScript (service in Automator). The selected text (title of a book) is the input (titre in the script), and the goal is to display in safari the result of the advanced research of this book on noosfere.org

It's ok when there is no accent characters in my selected text. But if titre is sphère d'influence : In the display box (only used for testing), "sphère d'influence" is correctly written with the "è". But in safari, in the research field in the website, I have "sphère d''influence".

  on run {titre, parameters}
       set url_noosfere_titre to "https://www.noosfere.org/livres/noosearch.asp?Mots=" & titre & "&Envoyer=Envoyer&livres=livres&ModeRecherche=AND&ModeMoteur=MOTSCLEFS&recherche=1"

     display dialog (url_noosfere_titre as text) buttons {"OK", "annulé"}
     set retour to button returned of result
     if retour is equal to "OK" then
         open location url_noosfere_titre
     end if
  end run
braaterAfrikaaner
  • 1,072
  • 10
  • 20

1 Answers1

0

URL with accents is very messy.

Basically URL's should be converted using 2 hexadecimal values with escape character '%'.For instance, "é", should be converted in %C3%A8.

However, result depends of web browser (IE, Safari, Chrome, Firefox) and versions. Sometime the browser is doing its own conversion which you can't avoid.

In your case, it is probably more efficient to change your mind and don't try to fill URL with the word you are searching for, but instead, just display the web page and set your word directly into the search area of the page, then click on the submit button ("Envoyer" in French).

In Applescript, you can manipulate a web page by using Javascript commands. The script bellow displays your web page, add your word to search in the correct field, select the search area and submit the request by clicking on the submit button.

Doing that, no accent issue at all !

set BaseURL to "https://www.noosfere.org/livres/noosearch.asp?Mots"
set MyWord to "sphère"
tell application "Safari"
    open location BaseURL
    activate
    delay 1 -- time to open the new window. Could be replaced by checking     javascript load = complete
    tell document 1
        -- fill the search field of the page with expression to search
        do JavaScript "document.getElementsByClassName('liste')[0].firstElementChild.children[1].value = '" & MyWord & "'"
        -- check boxes for search area are 1, 3, 5,...17, 19 in "littérature" block
        -- in this example, select the 2nd check box, so index is 3 (="Livres")
        do JavaScript "document.getElementById('litt').children[3].click()"
        -- click on the search button of the page (="Envoyer")
        do JavaScript "document.getElementsByClassName('liste')[0].firstElementChild.children[2].click()"
    end tell
end tell

fyi, I have been forced to add the selection of a search area (here I click on check box for all books ='Livres') because at least 1 selection in that area is mandatory.

pbell
  • 2,965
  • 1
  • 16
  • 13