deek5 deek5 - 16 days ago 5
AppleScript Question

AppleScript: encoding arbitrary query-string values in URLs passed to Firefox

I use firefox for a long time as my only browser on Pc or Mac.
In a few words my problem: I want to create a service on mac with automator and
Applescript for quite instant translation using translate.google.com.
What works great with Safari or Chrome (below the 4 or 5 lines of script)

On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run


The same thing (script) does not work at all with Firefox, I try by different ways
To circumvent the impossible problem

On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0

Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"

end if
end repeat
end tell
end run


By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure.
After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.

tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
set frontmost to true
set sentence to text of (the clipboard)
set thesentences to paragraphs of sentence
set thenewsentences to thesentences as string
set the clipboard to thenewsentences
keystroke "t" using command down
keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell


Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all.
Thank you for your attention.
Thank you for your answers.

Answer

Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.

Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.

The easiest (though not obvious) approach is to use perl, via a shell command:

# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"

# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
  "perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)

# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
    activate
    open location "https://translate.google.com/#auto/en/" & encodedText
end tell

The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.