My HR department is using an online tool to process our requests.
So I would like to help them with speeding up their searches there.
On that tool (webpage), a lot of tables exist, but the Cmd+F key combination does not work on any browser in order to search for specific text strings.
Thus we have to manually search the entries there.
On a spreadsheet we have a list of some employee names that we would like to search them on the online tool.
So, is there any way to parse all the contents that appear on the screen with apple script?
Tried already to parse the html code, but unfortunately it did not work. And I identified a lot of JavaScripts in the source code.
I have tried this as well, but unfortunately nothing is returned besides only the header of the page.
Moreover, not even the key combination cmd + F is functional.
According to the source code, the table is created via java queries. But nothing is selectable on it.
So we are not able to grab any data. There is only a search field on the top of the column (like the filters in Excel) where we can type the query we want.
use framework "Foundation"
tell application "Safari"
tell current tab of window 1
set URLStr to URL
end tell
end tell
set fileText to getUrlSource(URLStr)
on getUrlSource(URLStr)
set theURL to current application's class "NSURL"'s URLWithString:URLStr
set theData to current application's NSData's dataWithContentsOfURL:theURL
set theString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
set theString to theString as text
return theString
end getUrlSource
If that doesn’t work there’s a few other tricks we can try.
You know, I thought this looked familiar. This is a script I wrote about a year ago and use every day from the the Scripts menu:
Open your page in Safari then run this script. If you don’t have BBEdit then change that to TextEdit.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
tell application "Safari"
tell current tab of window 1
set URLStr to URL
set fileName to the name
end tell
end tell
set fileName to SlugifyText(fileName)
set fileName to fileName & ".html"
set fileText to getUrlSource(URLStr)
set newFile to ((path to desktop) as text) & fileName
set textWritten to my WriteToFile(newFile, fileText)
tell application "Safari"
set pageText to text of fileText
open file newFile
tell current tab of window 1
set pageText to its text
end tell
end tell
tell application "BBEdit"
make new window at beginning
set text of window 1 to pageText
activate
end tell
on getUrlSource(URLStr)
set theURL to current application's class "NSURL"'s URLWithString:URLStr
set theData to current application's NSData's dataWithContentsOfURL:theURL
set theString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
set theString to theString as text
return theString
end getUrlSource
on WriteToFile(myFile, dataToWrite)
--(alias, text; list; record, etc.)
try
set openFile to open for access myFile with write permission
on error errMsg number errNum
try
close access myFile
set openFile to open for access myFile with write permission
on error errMsg number errNum
return {"Error:", errMsg, errNum}
end try
end try
set eof of openFile to 1
write dataToWrite to openFile
close access openFile
return myFile
end WriteToFile
on ReplaceAllInText(findString, replaceString, textToFix)
set saveTID to AppleScript's text item delimiters
repeat
set AppleScript's text item delimiters to findString as list
set textToFix to every text item of textToFix
if (count of textToFix) = 1 then
set textToFix to textToFix as text
exit repeat
end if
set AppleScript's text item delimiters to {replaceString}
set textToFix to textToFix as text
if replaceString is in {findString} then exit repeat -- exits after one pass to avoid infinite loop
end repeat
set AppleScript's text item delimiters to saveTID
return textToFix as text
end ReplaceAllInText
on SlugifyText(textToSlugify)
set newText to {}
set textToSlugify to ReplaceAllInText({"'"}, {""}, textToSlugify)
set textToSlugify to ReplaceAllInText({"."}, {""}, textToSlugify)
set textToSlugify to ReplaceAllInText({","}, {"-"}, textToSlugify)
set textToSlugify to ReplaceAllInText({"!"}, {""}, textToSlugify)
set textToSlugify to ReplaceAllInText({"?"}, {""}, textToSlugify)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {""}
set textToSlugify to text items of textToSlugify
repeat with thisitem in textToSlugify
if (thisitem as text) is in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_" then
set the end of newText to thisitem as text
else
set the end of newText to "-"
end if
end repeat
if newText is not {} then
set newText to newText as text
set newText to my ReplaceAllInText("_", "-", newText)
set newText to my ReplaceAllInText("--", "-", newText)
else
set newText to "slug"
end if
set AppleScript's text item delimiters to saveTID
return newText
end SlugifyText
Create a NSXMLdocument from the URL.
Get the NSXMLNodes you want by performing
A XPath query on the document.
Go thru each of the nodes and get the data from
Those nodes