AppleScript C Perl Shell Xcode Other

Collect Google results on keywords without any APIs or special settings

Post Reply
coding / applescript     Views: 1548Prev .. Next
Collect Google results on keywords without any APIs or special settingsPosted: Thursday, December 9, 2010 [22:16:19] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 356
If you need to log a quick Google search on Mac you either do not own or just do not have time to setup Google API etc. - here is simple AppleScript that'll do the search and log for you:
View Codetell application "Safari" to activate
delay 3
set totData to ""
tell application "Safari"
make new document
tell application "System Events"
tell process "Safari"
keystroke "l" using {command down}
delay 1
keystroke tab
delay 1
keystroke "YOUR KEYWORDS GO HERE"
delay 1
keystroke return
end tell
end tell
delay 1
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat
delay 1
end tell
do shell script "echo " & quoted form of totData & " > /FILE_TO_SAVE_PATH.txt"
set pageNext to 1
-- SET A NUMBER OF GOOGLE RESULT PAGES TO SPIDER
set maxexit to 10
repeat with kl from 1 to maxexit
set pageNext to pageNext + 1
delay 1
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat
delay 1
my readPages(pageNext)
end repeat

tell application "System Events"
tell process "Safari"
keystroke "w" using {command down}
delay 1
end tell
end tell

on readPages(pageNext)
set totData to ""
tell application "Safari"
set LinkPresent to ""
set AllLinks to ""
set TransMatched to ""
set exitNmbr to 5
set LinkPresent to (do "document.links.length" in document 1)
set LinkPresent to LinkPresent - 1
set Split to ""
repeat with n from 0 to LinkPresent
set linkURL to (do "document.links[" & n & "].href" in document 1)
set linkName to (do "document.links[" & n & "].text" in document 1)
set Lnkdata to do shell script "echo " & quoted form of linkName & "NL" & pageNext & " | perl -n -e '($r,$k)=split(/NL/,$_);chomp$r;if($r=~ m/\\D/ || length($r) < 1 ){print \"10000\";}elsif($r == $k){s/\\D//g;print\"GOOD LINK\";}'"
if Lnkdata is equal to "GOOD LINK" then
do shell script "echo " & quoted form of totData & " >> /FILE_TO_SAVE_PATH.txt"
do "document.links[" & n & "].focus();" in document 1
delay 1
tell application "System Events"
tell process "Safari"
keystroke return
delay 1
end tell
end tell
return (pageNext)
exit repeat
end if
if linkName contains "YOUR MATCHING KEYWORDS ON GOOGLE RESULT PAGE" then
do "document.links[" & n & "].focus();" in document 1
tell application "System Events"
tell process "Safari"
keystroke return using {command down}
delay 1
end tell
end tell
set web_page_is_loaded to false
set myCounter to 0
set maxCounter to 30
set my_delay to 1
repeat until web_page_is_loaded is true
if name of window 1 contains "Loading" or name of window 1 contains "Untitled" then
delay my_delay
else
set web_page_is_loaded to true
end if
set myCounter to myCounter + 1
if myCounter is maxCounter then
set web_page_is_loaded to true
end if
end repeat
delay 1
set pageURL to URL of document 1
delay 1
set pageTitle to name of document 1
delay 1
if pageURL does not contain "google.com" then
set totData to totData & pageURL & "" & pageTitle & "
"
end if
tell application "System Events"
tell process "Safari"
keystroke "w" using {command down}
delay 1
end tell
end tell
end if
end repeat
end tell
do shell script "echo " & quoted form of totData & " >> /FILE_TO_SAVE_PATH.txt"
end readPages

Do not forget to change the values in UPPER case file path and keywords.There's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group