I want safari to download and save a web page using apple automator. With a Safari window open, I run the following script in AppleScript Editor:
tell application "Safari"
set URL of document 1 to "http://www.python.org"
delay 6
get document 1
delay 6
save document 1 in "/mydir/" & "python" & ".htm"
end tell
Safari displays the correct url, but then I get the following error in safari:
"The document “Python Programming Language – Official Website” could not be saved as “python.htm”."
I click "ok" on that error box and then AppleScript Editor gives me this error in a dialog box:
"AppleScript Error
Safari got an error: AppleEvent handler failed."
AppleScript Editor has a box at the bottom called "Result" and it also displays the following message:
"error "Safari got an error: AppleEvent handler failed." number -10000"
And the web page won't save. I don't know what's causing this b/c I'm 99% sure that I was using this same (or similar) code to save pages in the past.
Strangely, I moved the "get document 1" and "save document 1" lines outside of the "tell application safari" block and it saved the applescript as "python.htm" and it looked all goofy when opened with Firefox. So the problem seems to be that Safari cannot save and not osx generally.
also, I used chmod to change all permissions on the target save directory to all (used 777 argument in bash).
I updated the software restarted several times.
Here's my versions:
Safari: Version 5.1 (6534.50)
OSX Snow Leopard: Version 10.6.8
AppleScript 2.1.2
AppleScript Editor: Version 2.3 (118)
I'm tethered to my cell phone, using it as a mobile access point. I don't know if that matters.
I think this guy got the same error but no answers:
Why does this AppleScript get an "AppleEvent handler failed" error on Mac OS X 10.5 Leopard?
Anybody got a guess? Thanks.
I don't know the real answer, but it has 50+ views and a tumbleweed, so I figured I'd post something.
apparently you're supposed to use curl:
tell application "Safari"
set URL of document 1 to "http://www.python.org"
delay 6
get document 1
set my_html to URL of document 1
end tell
set my_new_file to "/Users/your_user_name/python.htm"
set my_script to "curl " & my_html & " >> " & my_new_file
do shell script my_script
(I saw a post on macscripter and they said this was "the only way to go" so maybe there is no way to use "save as" in Safari and this is a common applescript error but it was not answered b/c this is not an applescript board? Anyway, maybe the original post is supposed to work and I just don't know any better. I figured if people were finding this on the board or through google, an answer may be helpful or maybe the mods just want to delete it if this is an obvious solution).
Related
(newbie here) I'd to be able to save an applescript file as an app to use to open Safari, I cobbled together this from some old posts I found, it's not exactly what I want but it worked ok, however today it stopped working giving an error "The variable theURL is not defined."
If anyone could help me out with a better script that would work on Monterey I would be really grateful. Also, when I save as an app - should I select 'Stay open after run handler' or 'Run-only'? It just needs to open Safari when it's clicked, I don't need it to really open any particular page. Thanks in advance!
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell
I cobbled together this from some old posts I found:
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab with properties {URL:"https://google.com.au"}
on error
open location theURL
end try
end tell
A lot (virtually all) applescripts you see online make overuse of try blocks, which, apart from being completely unnecessary, are problematic in a number of ways, including making debugging more difficult. I'd advise taking out the line try, and everything including and between the lines on error, and end try.
Without those lines, you'll probably get a different error message, which was being masked by the try block. It most likely that window 1 didn't exist, which would be the case if Safari had already been running and didn't have any windows open. It would have then thrown an error, forwarded the script on to the on error block, where open location theURL then, itself, threw an error because theURL hasn't been defined.
I don't need it to really open any particular page
Then all you need is this:
tell application "Safari" to activate
If Safari is already running without any open windows, all this does is bring Safari to the front; it doesn't open a new window for you. If you need to create a new window you can usually do this:
tell application "Safari"
make new document
activate
end tell
However, this will create a new window even if there's already a window open, which might not be what you want. To create a window but only if a window doesn't already exists, you can do this:
tell application "Safari"
if documents = {} then make new document
activate
end tell
I suspect this is the script you will want to use.
Should I select 'Stay open after run handler' or 'Run-only'?
No and no. The first option prevents your app from quitting without explicit instruction to do so, which isn't needed in this case: you simply need your app to open, start Safari, then quit. The last option prevents you from making any edits to the script in the future, which, again, probably isn't what you want.
I'm using Selenium IDE to automate some browser processes, I need to do a test from which I open the source code of a page (view-source:https://www.youtube.com/watch?v=JeQuelXsUYA) and trying to find a certain word, I am using youtube as an example, however when clicking on "Select target in page" the following error appears "Tab not found / No guide is available for this test case, please keep recording or play. " I've tested it with several pages, and command recording works fine, but every time I open the page's source code, the selenium IDE doesn't record the commands.
Thanks in advance.
[![This is the image of the error, the first script I ask to open the page's source code works, however when I try to select target in page, the error appears, as if the page were not open.1
I'm using Chrome to get the HTML of a webpage which is generated by javascript. The applescript which does all this needs to run every 2 minutes. Everything is working perfectly, except that I obviously need Chrome to do this completely in the background. My script contains the following uses of Chrome (as well as a block to set theTab, which doesn't seem to ever cause Chrome to come to the front):
set URL of theTab to theURL
set isLoading to (loading of theTab)
execute front window's active tab javascript javascriptLocation
set theSource to execute front window's active tab javascript "document.documentElement.outerHTML"
Putting this line:
tell application "Finder" to set visible of process "Google Chrome" to false
after each of the above lines either produces no hiding at all, or at best Chrome flashes onscreen and then goes away. I find this very distracting.
Is there any way to have an application run reliably and permanently in the backround? Or, failing this, is there an invisible way to get javascript executed server-side so an applescript can get hold of its generated source?
Chrome 66.0.3359.181 running on Mac OS 10.11.6, Applescript 2.5.
The following is not an answer to the question I posted, but it is an answer to the problem I was trying to solve which is why I posted the question.
As stated in my question, I need to get hold of the HTML which some javascript generates on a site (which is not under my control). I can't do a client-side scrape because of CORS restrictions on the site. I tried the cross-domain tools listed here and couldn't get them to work.
So I was using Chrome's applescript command, execute, to first execute the javascript (to produce the HTML), and then a second time to grab hold of the HTML with document.documentElement.outerHTML. But having Chrome flash onto the screen every 2 minutes throughout the day was doing my head in.
Turns out Chrome can also run in headless mode, from the command line, and just happens to have an option to run javascript and return the generated HTML!
So my code got a whole lot simpler and I don't have to have Chrome in my apps list all the time. Happy coder am I :-)
Here's the one line that gets me the HTML generated on the site I need:
set theSource to (do shell script ((quoted form of POSIX path of googlePath) & " --headless --dump-dom " & theURL))
Thanks, once again, #matt. I'd never heard of headless mode and would never have found this without your suggestion of PhantomJS!
Background: Our production environment runs (legacy) vbscript both client- and server-side. So we are still on IE8. We're looking into upgrading client PCs to IE11. One problem we found is our file-downloads-for-display-in-browser no longer works. When clicking on, say, "test.xlsx" on the screen, which then calls a vbscript that does the following:
...
(read blob from database which can be pdf, xls, etc.)
....
If Not objRec.BOF And Not objRec.EOF Then
lngSize = objRec("document").ActualSize
objBlob = objRec("document").GetChunk(lngSize)
Response.ContentType = "application/download"
Response.AddHeader "Content-disposition","attachment; filename=""" + objRec("file_name") + """"
Response.BinaryWrite objBlob
...
we get the prompt:
"Do you want to open or save download_asp?id=1616 from dev.ourintranet.com?"
with options to "Open", "Save", and "Cancel".
Clicking Open does nothing.
Clicking Save opens directory common dialog but hitting Save does nothing.
Clicknig Cancel does nothing.
Why does it not work on IE11 anymore? Thanks a lot!
Client-side VBScript no longer works in IE11 edge mode:
As of Internet Explorer 11, VBScript is considered deprecated and should no longer be used as a scripting language for IE11. Webpages displayed in IE11 mode won't execute VBScript code.
Because VBScript is no longer supported for IE11 mode, the following API features are no longer available to webpages:
The execScript function.
The VBArray object.
The "text/vbs" and "text/vbscript" MIME types (as supported type values for script elements).
Update any pages that currently rely on VBSscript to use JavaScript. If this isn't feasible, try replacing the webpage with an app written in Visual Basic.
For compatibility reasons, VBScript does execute for legacy document modes (IE10 standards mode and earlier), however, this should be considered a temporary solution.
I've confirmed that this is only happening in Lion. I've uncovered what appears to be a bug where you need to focus a window twice in order for Finder to return the correct result over the scripting bridge or AppleScript. This only happens when opening a folder from the desktop or another space.
Here's how to duplicate:
Close, hide or minimize all windows until the desktop is showing.
Open any folder on the desktop, a new finder window will appear. Select any file in that finder window by clicking on it once.
Open your AppleScript Editor and run the following script:
tell application "Finder"
return selection
end tell
The script will return only the path to the folder on the desktop, not the selected file in the finder window.
If you refocus the window, or click on the file again, everything works as expected and the script returns the correct path.
This seems like a bug in Lion. Snow Leopard returns the correct path 100% of the time.
I've tried the following methods with no success:
Using the scripting bridge and https://github.com/davedelong/BetterInfo/blob/master/Finder.h
Using "System Events" apple script to click "Edit"->"Copy"
Using AppleScript to get the current selection from the finder.
All seem to have have the same issue.
Am I missing something obvious or is this truly a problem with Lion?
Wow, I can confirm (at least through applescript) most of what you describe. I was getting an empty list returned, not even the desktop path you mention. I was testing this using a script in the applescript menu. The only way I could get the selection from a fresh Finder window was like this...
tell application "System Events" to activate
tell application "Finder"
activate
set a to (get selection) as text
display dialog a
end tell