-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchForTitle.applescript
75 lines (63 loc) · 2.21 KB
/
SearchForTitle.applescript
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
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
set theArray to every text item of theString
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
tell application "Safari"
tell the front document
do JavaScript "function getEditor() {
return CKEDITOR.instances['edit-body-und-0-value'];
}
function getHTML() {
return getEditor().getData();
}
function setHTML(html) {
return getEditor().setData(html);
}
function firstParagraph(html) {
return html.split('</p>', 1)[0].slice(3);
}
function setTitle(text) {
return document.querySelector('#edit-title').value = text;
}
function setStandfirst(text) {
return document.querySelector('#edit-field-lead-und-0-value').value = text;
}
function codePointsJSON(text) {
return JSON.stringify(
Array.from(text).map(c => c.codePointAt(0))
);
}
function setReadyForPublishing() {
// 4 is the index of the `Ready for Publishing` state
var el = document.querySelector('#edit-workbench-moderation-state-new');
return el.selectedIndex = 4;
}
function saveAndClose() {
document.querySelector('#edit-submit-1').click();
}
"
set firstParCodePoints to do JavaScript "codePointsJSON(firstParagraph(getHTML()))"
set fromDb to do shell script "(export LC_ALL=en_GB.UTF-8 && echo " & firstParCodePoints & " | /usr/local/bin/python3 /Users/admin/projects/sentence-search/db_search.py -j)"
set articleDetails to my theSplit(fromDb, "|")
set title to the first item of articleDetails
set standfirst to the second item of articleDetails
if title is not "Title not found" then
do JavaScript "setTitle(" & quote & title & quote & ")"
do JavaScript "setStandfirst(" & quote & standfirst & quote & ")"
do JavaScript "setReadyForPublishing()"
if button returned of (display dialog "Save and close?") is "OK" then
do JavaScript "saveAndClose()"
end if
else
display dialog "Could not find a title in the database"
end if
end tell
end tell