forked from indiscripts/extendscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInGoogle.jsx
113 lines (101 loc) · 3.99 KB
/
InGoogle.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***********************************************************************/
/* */
/* InGoogle :: Invoke Google from InDesign, based on the selection */
/* */
/* [Ver: 1.1] [Author: M. Fischer & M. Autret] [Modif: 01/13/15] */
/* [Lang: EN|FR|DE] [Req: InDesign CS4-CC] [Creat: 01/13/15] */
/* [Type: FREE] */
/* */
/* Based on Martin Fischer's googleSelection.jsx script (oct. 2010) */
/* http://bit.ly/1waenhR */
/* Details on Google search parameters: */
/* http://bit.ly/1yd4lAP */
/* */
/* Installation: */
/* */
/* 1) Place the current file into the Scripts/Scripts Panel/ */
/* folder */
/* */
/* 2) Start InDesign, select some text in a document */
/* */
/* 3) Exec the script from Window>Automatisation>Scripts (CS4) */
/* or Window>Utilities>Scripts (CS5/CS6/CC) */
/* + double-click on the script file name */
/* */
/* Bugs & Feedback : marc{at}indiscripts{dot}com */
/* www.indiscripts.com */
/* */
/***********************************************************************/
// Set the below param, FORCE_EXACT_MATCH, as follows:
// 1 <=> always send a "word1 word2 word3..." query to Google (exact match)
// 0 <=> send an exact-match query *only* if non-breaking spaces are used
// ---
const FORCE_EXACT_MATCH = 1;
const ENGINE = 'http://www.google.com/search?%1=%2&hl=%3&lr=lang_%3';
const LOC2LANG = {
ARABIC: 'ar',
CZECH: 'cs',
DANISH: 'da',
ENGLISH: 'en',
FINNISH: 'fi',
FRENCH: 'fr',
GERMAN: 'de',
GREEK: 'el',
HEBREW: 'he',
HUNGARIAN: 'hu',
INTERNATIONAL_ENGLISH: 'en',
ITALIAN: 'it',
JAPANESE: 'ja',
KOREAN: 'ko',
POLISH: 'pl',
PORTUGUESE: 'pt',
ROMANIAN: 'ro',
RUSSIAN: 'ru',
SIMPLIFIED_CHINESE: 'zh',
SPANISH: 'es',
SWEDISH: 'sv',
TRADITIONAL_CHINESE: 'zh',
TURKISH: 'tr',
UKRAINIAN: 'uk',
};
(function(s,lg,t,q,qType,url,doc,src,dst,hyper)
{
// Language in 'xx' form (lowercase) based on the ID Locale
// ---
lg = LOC2LANG[(function(L,k){for( k in Locale )if( L==+Locale[k] ) return k.replace(/_LOCALE$/,'')})(+app.locale)]||'en';
// Is there some (selected) text?
// ---
if( !s.length || !(s=s[0]).hasOwnProperty('baseline') || !s.characters.length )
{
alert( {
fr: "Aucun texte s\xE9lectionn\xE9.",
de: "Es wurde kein Text ausgew\xE4hlt",
}[lg]||"No text selected." );
return;
}
// Format the query string
// ---
t = s.texts[0].contents;
qType = (FORCE_EXACT_MATCH || /[\u00A0\u202F]/.test(t)) ? 'as_epq' : 'q';
q = t.split(/\s+/g).join('+');
url = localize(ENGINE, qType, q, lg);
// Create and call a 'ghost' hyperlink based on the url
// ---
doc = app.properties.activeDocument;
try {
src = doc.hyperlinkTextSources.add({ sourceText:s });
dst = doc.hyperlinkURLDestinations.add({ destinationURL:url });
hyper = doc.hyperlinks.add({destination:dst, source:src});
}
catch(e)
{
alert(e);
return;
}
hyper.showDestination();
// Cleanup
// ---
hyper.remove();
dst.remove();
src.remove();
})(app.selection);