I am new to AppleScript and would really appreciate it if anyone with experience could help me solve my problem. Below is basically what I want the script to do. This script I attached works, however I am not getting the search results from the text I am searching for. I read somewhere that I would need to to search InnerHTML with added javascript which I have no Idea how to do.
This is what I would like the script to do:
Open webpages one at a time from list in txt file
when webpage is loaded, then search for "text"
if "text" is not found
then
close tab, open next URL in list
if "text" is found
then
copy URL to a text file
once url is copied and saved
then
close tab, open next url in list
repeat
If anyone has the time to help me with this or point me in the right direction, I would be beyond grateful. Thank you so much for your time.
on run {input, parameters}
read (item 1 of input)
set ps to paragraphs of the result
set tot to count ps
tell application "Safari"
reopen
activate
end tell
repeat with i from 1 to tot
set p to item i of ps
if p is not "" then
try
tell application "Safari"
tell front window
set r to make new tab with properties {URL:p}
set current tab to r
delay 1
if i = tot then exit repeat
repeat
delay 1
tell application "Safari"
activate
--the source code of current Tab
set sourceOfTab to source of front document
set urlOfTab to URL of front document
if sourceOfTab contains "search term" then
--copy of the url in textFile
do shell script "echo " & urlOfTab & " >> ~/Desktop/test.txt"
else
close current tab of front window
end if
end tell
get URL of r
end repeat
end tell
end tell
end try
end if
end repeat
end run
I don’t think the problem is with your search routine. If I cut down the script to just the search routine, it appears to work:
tell application "Safari"
--the source code of current Tab
set sourceOfTab to source of front document
set urlOfTab to URL of front document
if sourceOfTab contains "search term" then
--copy of the url in textFile
do shell script "echo " & urlOfTab & " >> ~/Desktop/test.txt"
else
display dialog "Search term not found"
--close current tab of front window
end if
end tell
That is, if I have this question open, then the file gets created on the Desktop, because the text “search term” does appear on this page. If I change the search term to something that does not appear on this page, I get “Search term not found”.
To track down the problem, you’ll probably want to break the script into steps, and see which step is not doing what you wish. For example,
Make a version of the script that just steps through the URLs and displays them to you, so that you know that it is using the correct URLs.
Make a version of the script that steps through the URLs and opens them in a browser window, but does nothing else, so that you know that Safari is opening the correct web pages.
Only once the first two steps are doing what they should be would you add the search routine back in.
You may also wish to provide sample data within your script, so that we can run our experiments on the same data that you’re having trouble with. Currently, I’m not sure how you’re getting the data to the run handler, for example.
Here’s one example of doing this as a drag-and-drop application, that is, an application that accepts files dropped on top of its icon in the Finder (the files should be text files, one URL per line):
property searchTerm : "search term"
property matchFilePath : (path to desktop folder as text) & "Matching URLs.txt"
on open droppedFiles
get path to desktop folder as text
set matchFile to open for access file matchFilePath with write permission
repeat with droppedFile in droppedFiles
read droppedFile
repeat with webURL in paragraphs of the result
--ignore empty lines, which contain one character (the return)
if the (count of webURL) is greater than 1 then
tell application "Safari"
open location webURL
delay 1
set theRealURL to the URL of the front document
if the source of the front document contains searchTerm then
write theRealURL & return to matchFile starting at eof
end if
close current tab of front window
end tell
end if
end repeat
end repeat
close access matchFile
end open
To test your script and provide advice, we’ll need to know how the URLs are getting to your run handler, and what format those URLs take. Also, while I think we have a pretty good idea of what you’re expecting a successful run to look like, you may also wish to describe more clearly the results that you’re seeing that you don’t want to see.