-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppleScriptGetPageSource.calc
82 lines (77 loc) · 2.45 KB
/
AppleScriptGetPageSource.calc
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
Let ( [
path = Get ( FilePath ) ;
dbName = Middle ( path ; Position ( path ; "/" ; Length ( path ) ; -1 ) + 1 ; 999 ) ;
fieldSplit = Substitute ( resultFieldName ; "::" ; ¶ ) ;
tableName = GetValue ( fieldSplit ; 1 ) ;
fieldName = GetValue ( fieldSplit ; 2 ) ;
fmpVersion = "FileMaker Pro" & If ( Position ( Get ( ApplicationVersion ) ; "ProAdvanced" ; 1 ; 1 ) ; " Advanced" )
];
"set theURL to \"" & url & "\"¶
set dbName to \"" & dbName & "\"¶
set tableName to \"" & tableName & "\"¶
set fieldName to \"" & fieldName & "\"¶
¶
tell application \"Safari\"¶
set newDoc to make new document with properties {URL:theURL}¶
¶
-- Wait until page has loaded¶
if my page_loaded(30) then¶
delay 2¶
set theText to the source of document 1¶
end if¶
close document 1¶
end tell¶
¶
-- Update log (for debugging)¶
--my replaceLog(theURL & return & theText,\"~/Desktop/last_page.html\")¶
¶
-- Set value to field¶
setField(dbName, tableName, fieldName, theText)¶
¶
-- Clear variable (to clear memory)¶
set theText to missing value¶
¶
-- HANDLER: Waits for page to load in Safari¶
on page_loaded(timeout_value) -- in seconds¶
delay 2¶
repeat with i from 1 to timeout_value¶
tell application \"Safari\"¶
if (do JavaScript \"document.readyState\" in document 1) is \"complete\" then¶
return true¶
else if i is timeout_value then¶
return false¶
else¶
delay 1¶
end if¶
end tell¶
end repeat¶
return false¶
end page_loaded¶
¶
-- HANDLER: Sets FileMaker field value¶
on setField(databaseName, tableName, fieldName, theValue)¶
tell application \"" & fmpVersion & "\"¶
tell database (databaseName as text)¶
tell table (tableName as text)¶
set field fieldName to theValue¶
end tell¶
end tell¶
end tell¶
end setField¶
¶
-- HANDLER: Replaces file with timestamped entry¶
-- Alternative file name or location must be modified in the code¶
on replaceLog(logEntry, logPath)¶
do shell script \"TEXT=$(echo \" & quoted form of logEntry & \" );echo \\\"`date '+%Y.%m.%d %H:%M:%S'`¶
$TEXT\\\" > \" & logPath¶
end replaceLog"
)
/* —————————————————————————————— //
NAME:
AppleScriptGetPageSource ( resultFieldName ; url )
PURPOSE:
Returns HTML source of page at specified url and sends ot to specified field in FileMaker.
EXAMPLES:
HISTORY:
Created: 2011-Aug-18 13h28 PST — Donovan A. Chandler
*/