Hai bisogno di aiuto con l'interfaccia alfa di applecript e wolfram

0

Ok, ecco il mio codice ... La mia domanda è, posso determinare che tipo di dati è prima di afferrare il testo, perché se cerco qualcosa come "quanti cucchiai in un'oncia, mi dà un sacco di codice inutile e non afferrare il testo giusto. Qualsiasi aiuto è apprezzato, grazie!

    tell application "Safari"
    quit
end tell

set defaultAnswer to ""
set cancelButton to "Cancel"
set buttonResearch to "ReSearch"

display dialog "Query: " default answer defaultAnswer buttons {cancelButton, buttonResearch} default button buttonResearch cancel button cancelButton with icon 1
copy the result as list to {button_pressed, text_returned}

tell application "Dragon Dictate"
    set listening to false
end tell


if (button_pressed is buttonResearch) and (text_returned is not "") then
    set a to "http://www.wolframalpha.com/input/?i="
    set b to encode_text(text_returned, true, false)
    set c to a & b

    tell application "Safari"
        activate
        open
    end tell
    repeat
        if application "Safari" is running then exit repeat
    end repeat
    delay 1
    tell application "System Events"
        keystroke "m" using command down
    end tell
    tell application "Safari"
        tell window 1
            set current tab to (make new tab with properties {URL:c})
        end tell
    end tell
end if

say "let me look that up for you now"
delay 6

tell application "Safari" to set theString to (source of document 1)

(* Strip the text and only return the last line*)
set input to do shell script "echo " & (quoted form of theString) & "|sed -n \"/stringified/,/mInput/p\" | sed '$!N;$!D'"



global answer

set offSet1 to "\"stringified\": \""
set offSet2 to "\",\"mInput\""


my strip(offSet1, offSet2, input)

set mywordcount to count of words of answer

if mywordcount is greater than 50 then
    display dialog x
else


end if

tell application "Safari"
    quit
end tell

tell application "Dragon Dictate"
    set listening to true
end tell

on strip(offSet1, offSet2, thedata)
    (* Use the offsets of the pattens to match the text # thru # *)
    set textNumber1 to (offset of offSet1 in thedata)
    set theData1 to text -1 thru (textNumber1 + (count of offSet1)) of thedata
    set textNumber2 to (offset of offSet2 in theData1)
    set textString2 to text from word 1 to (textNumber2 - 1) of theData1
    set thedata to theData1
    set answer to textString2
end strip

-- encoding high-ASCII characters:
on encode_char(this_char)
    set the ASCII_num to (the ASCII number this_char)
    set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set x to item ((ASCII_num div 16) + 1) of the hex_list
    set y to item ((ASCII_num mod 16) + 1) of the hex_list
    return ("%" & x & y) as string
end encode_char

-- TEXT ENCODING: encode spaces and high-level ASCII characters (those above 127)
-- encode_URL_A = encode most of the special characters reserved for use by URLs.
on encode_text(this_text, encode_URL_A, encode_URL_B)
    set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
    set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~'^\|*"
    set the URL_B_chars to ".-_:"
    set the acceptable_characters to the standard_characters
    if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
    if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
    set the encoded_text to ""
    repeat with this_char in this_text
        if this_char is in the acceptable_characters then
            set the encoded_text to (the encoded_text & this_char)
        else
            set the encoded_text to (the encoded_text & encode_char(this_char)) as string
        end if
    end repeat
    return the encoded_text
end encode_text
    
posta Phoenix Ebner 06.06.2015 - 13:22
fonte

1 risposta

1

Utilizza il nome della sezione per fare clic sul pulsante "Copia testo in chiaro":

  • prima sezione = pod_0100 , seconda sezione = pod_0200 , terza    section = pod_0300

Utilizza l'indice del pulsante:

  • ('action subpod-copyablept ')[0] = il primo pulsante "Copia testo in chiaro" in una sezione.
  • ('action subpod-copyablept ')[1] = il secondo pulsante "Copia testo in chiaro" in una sezione.

La risposta della finestra popup viene aggiunta alla fine del documento.

EX (query: " quanti cucchiai in un'oncia "):

  • se lo script fa clic sul primo pulsante Copiabile nella sezione "pod_0200", il ritorno dello script " 2 cucchiai (cucchiai) "
  • se lo script fa clic sul primo pulsante Copiabile nella sezione "pod_0100", il ritorno di script " converti 1 fl oz (oncia fluida) in cucchiai "

-

if (button_pressed is buttonResearch) and (text_returned is not "") then
    set theUrl to "http://www.wolframalpha.com/input/?i=" & encode_text(text_returned, true, false)
    tell application "Safari"
        tell window 1 to set current tab to (make new tab with properties {URL:theUrl})
        tell me to say "let me look that up for you now"
        tell document 1
            repeat -- wait until loaded
                delay 2
                if (do JavaScript "document.readyState") = "complete" then exit repeat
            end repeat
            do JavaScript "document.getElementById('pod_0200').getElementsByClassName('action subpod-copyablept ')[0].click()" -- show the popup window
            set theAnswer to do JavaScript "document.body.lastChild.getElementsByTagName('pre')[0].innerHTML;" -- get the answer in this popup window
        end tell
    end tell
    activate
    display dialog theAnswer
end if
    
risposta data 06.06.2015 - 17:41
fonte

Leggi altre domande sui tag