我使用firefox作为我在Pc或Mac上的唯一浏览器很长一段时间。 用几句话我的问题:我想在mac上使用automator创建一个服务 使用translate.google.com进行即时翻译的Applescript。 什么适用于Safari或Chrome(低于4或5行脚本)
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
同样的事情(脚本)根本不适用于Firefox,我尝试了不同的方式 绕过不可能的问题
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
通过复制和粘贴,操作在完全加载页面之前进行,即使通过减慢复制和粘贴过程也是如此。 经过多次观察后,存在一个问题,即使用URL的关联来格式化剪贴板中包含的文本,我对此进行了改进,但它还不完美。
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
无论如何,如果它适用于Safari而不修改任何内容,问题出在Firefox条目中,所以如果你能看到这个问题,那对我们所有人来说都是非常有用的。 感谢您的关注。 谢谢你的回答。
答案 0 :(得分:2)
Safari和Chrome会为您执行网址中的necessary encoding保留字符,但Firefox不会。
因此,您需要显式执行查询字符串值(要嵌入URL中的文本)的编码。
最简单(虽然不是很明显)的方法是通过shell命令使用perl
,感谢here改编:
# 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
上述方法适用于所有三种浏览器:Firefox,Safari和Google Chrome。
注意:
从(至少)Firefox v50开始,默认情况下,Firefox会在当前前窗中的新标签中打开URL。
您可以通过在Firefox首选项的Open new windows in a new tab instead
标签上取消选中General
,让Firefox在新的窗口中打开网址。
但请注意,这是一个持久性设置,会影响从Firefox外部打开的所有网址。
对于在新窗口中打开而不依赖于更改设置的临时解决方案,请参阅我的this answer。
答案 1 :(得分:0)
你好在服务Automator下面有一些版本可能会遇到问题,我修改了脚本以便几乎无处不在。 频繁的系统错误是应用程序控制计算机的权限,由系统首选项选项卡安全和隐私处理,系统会询问您是否允许Firefox,TexEdit和其他人使用此服务作为其键盘快捷方式。 另外在Automator创建服务(一般(并出现在所有应用程序中)没有条目(直到Yosemite自El Capitan我看到用Firefox例如所有服务都可用),选择执行脚本Applescript粘贴下面的脚本分为2个脚本或仅1个脚本。
on run
set Sn to ""
tell application "System Events"
set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service
tell process Sn
set frontmost to true
try
click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
end try
end tell
end tell
return the clipboard
end run
In the script following the entry is done with the contents of the Clipboard
On run {input}
on run {input}
set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
try
set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com
tell application id "org.mozilla.firefox"
activate
open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
end tell
end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text