-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSafari.applescript
More file actions
102 lines (86 loc) · 2.38 KB
/
Copy pathSafari.applescript
File metadata and controls
102 lines (86 loc) · 2.38 KB
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
(*
Safari Library
v1.0
Dov Frankel, 2013
property LibLoader : load script file ((path to scripts folder from user domain as text) & "Libraries:Library Loader.scpt")
property SafariLib : LibLoader's loadScript("Libraries:Safari.applescript")
*)
-- Test
--my LoadUrlInNewWindow("http://chaseonline.chase.com")
(*
tell SafariLib to CloseWindow()
*)
on CloseWindow()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Close Window" of menu "File" of menu bar 1
end tell
end tell
end CloseWindow
(*
tell SafariLib to LoadUrlInFrontWindow( "http://www..." )
*)
on LoadUrlInFrontWindow(theUrl)
tell application "Safari"
activate
delay 0.5
set theDoc to front document
set theOldUrl to ""
set theOldUrl to theDoc's URL
if theOldUrl = null then set theOldUrl to ""
set theDoc's URL to theUrl
my WaitForFrontWindowToLoadIgnoringUrlContainingString(theOldUrl, ":")
end tell
end LoadUrlInFrontWindow
(*
tell SafariLib to LoadUrlInNewWindow( "http://www..." )
*)
on LoadUrlInNewWindow(theUrl)
tell application "Safari"
activate
make new document
end tell
my LoadUrlInFrontWindow(theUrl)
end LoadUrlInNewWindow
(*
tell SafariLib to WaitForFrontWindowToLoadIgnoringUrl()
*)
on WaitForFrontWindowToLoadIgnoringUrlContainingString(OldUrl, UrlStr)
set maxAttempts to 15
tell application "Safari"
repeat
my WaitForFrontWindowToLoad()
set theUrl to front document's URL
set correctPageLoaded to theUrl ≠ OldUrl and theUrl contains UrlStr
log "Old URL:
" & OldUrl & "
New URL:
" & theUrl & "
URL String:
" & UrlStr
log "Correct page is loaded: " & correctPageLoaded
set maxAttempts to maxAttempts - 1
if correctPageLoaded or maxAttempts = 0 then exit repeat
end repeat
end tell
end WaitForFrontWindowToLoadIgnoringUrlContainingString
(*
tell SafariLib to WaitForFrontWindowToLoad()
*)
on WaitForFrontWindowToLoad()
tell application "Safari"
activate
set maxAttempts to 15
set doneLoading to false
repeat until doneLoading = true or maxAttempts = 0
delay 3
set theDoc to front document
set maxAttempts to maxAttempts - 1
set doneLoading to ((do JavaScript "document.readyState" in theDoc) is "complete") ¬
and theDoc's URL does not start with "topsites"
set maxAttempts to maxAttempts - 1
end repeat
log "done loading: " & theDoc's URL
end tell
end WaitForFrontWindowToLoad