Friends, I have a headache.
I’m trying to get some text displayed on a web page into a .rtf document that I can save locally. The text I’m trying to get cannot be got from the page source - it’s all done server side and delivered on loading. Looking at the document source it’s all just _javascript_ code. Still...selecting the text displayed on the page manually via Cmd-A and Cmd-C and Cmd-V into a TextEdit document works perfectly. However, trying to automate that has proven...painful, to say the least.
This is clumsy I know, but I’m just trying to get proof-of-concept before tidying this up into decent code. Half the time, I get the result I want. And the other half I either get a blank .rtf doc, a crash or no doc at all. The Delay’s seem to help a little, but there must be a better way to do this? Any thoughts much appreciated My head hurts from wrestling with this over two days, and I’ve been through every post on macscripter and other sites to no avail.
In the following code, assume the tab with the desired data is already loaded and frontmost in Safari when Safari is activated:
set _filename to "Reports" set _dateStamp to (do shell script "date +_%Y-%m-%d_%H.%M.%S") set _newFile to (path to desktop as string) & _filename & _dateStamp & ".rtf"
tell application "Safari" to activate tell application "System Events" tell application process "Safari" click menu item "Select All" of menu "Edit" of menu bar 1 click menu item "Copy" of menu "Edit" of menu bar 1 end tell end tell
delay 0.5
tell application "Safari" to close current tab of window 1
tell application "TextEdit" activate set f to make new document end tell
tell application "System Events" tell application process "TextEdit" click menu item "Paste" of menu "Edit" of menu bar 1 end tell end tell
tell application "TextEdit" save f in file _newFile close window 1 quit end tell
|