Straw 是一个在 macOS 系统下跨应用程序, 实现划词翻译的工作流(workflow).
- 打开
Automator
, 选择新建一个服务
Service receives selectd
选text
, inany application
- 拖进来一个
Run Shell Script
,Pass input
选择stdin
- 把下面这段 shell 脚本粘进去
#! /bin/bash
# stdin
input=""
while read line ; do input+=$line ; done
# exit if no input
# if [ $input="" ]; then
# exit 2
# fi
# get total length of input
# input_length=${#input}
# urlencode input
input=$(echo -ne ${input} | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g')
# static params
static_params="client=gtx&ie=UTF-8&oe=UTF-8&otf=2&ssel=0&tsel=3&kc=1"
# dt array of params
dt_array=("at" "bd" "ex" "ld" "md" "qca" "rw" "rm" "ss" "t")
for item in ${dt_array[@]};
do
static_params+="&dt=$item"
done
# TODO: auto selct `tl`, `hl` and `sl`
dynamic_params="sl=auto&tl=zh-CN&hl=zh-CN"
curl --request GET \
--url "https://translate.google.cn/translate_a/single?${static_params}&${dynamic_params}&q=${input}" \
--header 'cache-control: no-cache' \
--header 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36' \
--header 'referer: https://translate.google.cn/?source=osdd'
- 拖进来一个
Run JavaScript
, 把下面这段 js 脚本粘进去
/**
* filter json from google translate api
*
* @param {Array} json
* @returns
*/
function filter(json) {
json = json || []
var baseTranslate = json[0];
var _pronunciation = baseTranslate.pop()[3] || '';
var _source = '', _result = '';
baseTranslate.forEach(function(item) {
_result += item[0]
_source += item[1];
});
var partsOfSpeech = json[1] || []
var _pos = partsOfSpeech.map(function(item) {
return {
name: item[0],
values: item[1]
}
})
var listOfSpeech = json[11] || []
var sentenceSamples = json[12] || []
var _los = listOfSpeech.map(function(item) {
return {
name: item[0],
values: item[1][0][0]
}
})
var _ss = sentenceSamples.map(function(item) {
return {
name: item[0],
value: item[1][0][0] + ' ' + item[1][0][2]
}
})
return {
pronunciation: _pronunciation,
result: _result,
source: _source,
pos: _pos, // ?
los: _los,
ss: _ss
}
}
/**
* main function
*
* @param {String} input from shell
* @param {any} parameters
* @returns
*/
function run(input, parameters) {
var resultJson;
var splitLine = '—————————————————————————————';
try {
resultJson = JSON.parse(input);
} catch (e) {
return;
}
var filterdJson = filter(resultJson);
var dialogText = '';
dialogText += filterdJson.source + ' ';
if (filterdJson.pronunciation) {
dialogText += filterdJson.pronunciation + '\n';
}
dialogText += '\n' + splitLine + '\n';
dialogText += '\n' + filterdJson.result + '\n';
if (filterdJson.los.length !== 0) {
dialogText += '\n' + splitLine + '\n源同义词:\n';
filterdJson.los.forEach(function(item) {
dialogText += ' ' + item.name + ' => ' + item.values.join(' ') + '\n';
});
}
if (filterdJson.pos.length !== 0) {
dialogText += '\n结果同义词:\n';
filterdJson.pos.forEach(function(item) {
dialogText += ' ' + item.name + ' => ' + item.values.join(' ') + '\n';
});
}
if (filterdJson.ss.length !== 0) {
dialogText += '\n例句:\n';
filterdJson.ss.forEach(function(item) {
dialogText += ' ' + item.name + ' => ' + item.value + '\n';
});
}
var app = Application.currentApplication();
app.includeStandardAdditions = true;
app.displayDialog(
dialogText,
{
buttons: ["打开网页", "确定"],
defaultButton: "确定"
}
);
return;
}
- 打开
System Preferences
=>Keyboard
=>Shotcuts
=>Service
=>Text
=>Straw
, 给它设置一个快捷键
- 根据选中文字, 自动判断 source language 和 target language.
-
打开网页
功能