Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

applescript sometimes not working in saving a Safari website

I have a script to check if there are certain items on Ebay. The script is on my server and than emails me the archive.


I use the following script that only works haphazardly. sometime it saves it sometimes not. at all times it opens the web-site, no problem. However the saving is a hit and miss. And I do not understand why. anybody see as to why?



tell application "Safari"


activate


delay 5

tell application "System Events"

tell application "Safari" to activate

keystroke ("http://www.ebay.co.uk")


delay 5


keystroke (ASCII character 13)


delay 20


tell process "Safari" to keystroke "s" using command down


delay 2

tell process "Safari" to keystroke "d" using command down


delay 2

keystroke ((("XXX") & (do shell script "date '+%d.%m.%Y'") & (".webarchive")))


delay 2


keystroke (ASCII character 13)

end tell

end tell

MBP, MM, MBP - 10.6 + Windooz XP on a hard partition

Posted on May 10, 2012 12:02 AM

Reply
15 replies

May 10, 2012 3:28 AM in response to ChangeAgent

The old joke springs to mind about the man looking for directions, gets the reply, after a long pause, "If I were you I wouldn't start from here at all".


I am always reluctant to use keystrokes in Applescript, as it can be flakey and hard to debug. For what you are doing, why not use curl? eg

do shell script "curl http//www.ebay.co.uk -o file-to-get-result"

May 10, 2012 3:48 AM in response to Austin Kinsella1

Thanks Austin,


Nice to start somewhere else...


However I tried


tell application "Safari" to activate

delay 1

do shell script "curl http://www.ebay.co.uk -o file-to-get-result"


It actives Safari but does not open the site.


the result is:


tell application "Safari"


activate

end tell

tell current application

do shell script "curl http://www.ebay.co.uk -o file-to-get-result"

--> ""

end tell

Result:

""



Eric

May 10, 2012 5:58 AM in response to ChangeAgent

Umm, not quite right. Safari isn't involved at all, so you don't need to activate it, and no need for a delay. curl will get the web page, without displaying it, and store the result (which will not be interpreted) in the file following -o, so you will probably find a file called file-to-get-result (I assumed you'ld put in your own filename) in your Home directory.

May 11, 2012 10:52 AM in response to ChangeAgent

It works, but it stores it on the HD and I like it on the Desktop.


I Googled some curl stuff and found some info as to how to get it to store elswhere, but it does not seem to work for me. I admit I do not understand what is being done her. I have:


set remFile to ".html"

set locFile to "GotTest"

set locDir to "/Users/mymac/Desktop"


do shell script "curl http://www.ebay.co.uk/sch/i.html?_nkw=mac -o mac" & remFile & ">" & locDir & locFile

May 11, 2012 8:04 PM in response to ChangeAgent

ChangeAgent wrote:


I have a script to check if there are certain items on Ebay. The script is on my server and than emails me the archive. […] sometime it saves it sometimes not. at all times it opens the web-site, no problem. However the saving is a hit and miss. And I do not understand why. anybody see as to why?


Under Mac OS X 10.7.4, the following script seems to do what you are asking for:


open location "http://www.ebay.co.uk"


repeat until application "Safari" exists

delay 1 -- wait until application "Safari" exists

end repeat


tell application "System Events" to tell process "Safari"

set theMenuItem to menu item "Save as…" of menu 1 of menu bar item "File" of menu bar 1

repeat until enabled of theMenuItem is true

delay 1 -- wait until the window is completely loaded

end repeat

keystroke "s" using {command down} -- save

delay 1

keystroke "d" using {command down} -- save to desktop

delay 1

set thePopUpButton to pop up button 1 of group 1 of sheet 1 of window 1

if value of thePopUpButton is not "Web Archive" then

clickthePopUpButton

keystroke "Web" & return

end if

click button "Save" of sheet 1 of window 1

delay 1

if sheet 1 of sheet 1 of window 1 exists then

click button "Replace" of sheet 1 of sheet 1 of window 1

end if

end tell


Message was edited by: Pierre L.

May 12, 2012 6:28 AM in response to Pierre L.

Pierre Bonjour.


It works under 10.6 too. Thanks.


Since both scripts work it gives me a few options.


I amended your script a tick to ad a name to the file. I did this since it gets e-mailed later and as such the name will be constant when scripting to e-mail it.


here it is.


open location "http://www.ebay.co.uk"


repeat until application "Safari" exists


delay 4 -- wait until application "Safari" exists

end repeat


tell application "System Events" to tell process "Safari"

set theMenuItem to menu item "Save as…" of menu 1 of menu bar item "File" of menu bar 1

repeat until enabled of theMenuItem is true


delay 4 -- wait until the window is completely loaded

end repeat


delay 1


keystroke "s" using {command down} -- save


delay 2


keystroke "Name" -- give the file a name


delay 1


keystroke "d" using {command down} -- save to desktop


delay 1

set thePopUpButton to pop up button 1 of group 1 of sheet 1 of window 1

if value of thePopUpButton is not "Web Archive" then


clickthePopUpButton

keystroke "Web" & return

end if


clickbutton "Save" of sheet 1 of window 1


delay 1

if sheet 1 of sheet 1 of window 1 exists then

click button "Replace" of sheet 1 of sheet 1 of window 1

end if

end tell

applescript sometimes not working in saving a Safari website

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.