Skip navigation

applescript sometimes not working in saving a Safari website

1181 Views 15 Replies Latest reply: May 13, 2012 12:55 AM by ChangeAgent RSS
1 2 Previous Next
ChangeAgent Level 1 Level 1 (140 points)
Currently Being Moderated
May 11, 2012 12:03 PM

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
  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    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"

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    Ooops, note the missing colon, should be

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

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    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.

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    It is the same data that Safari would get, so try sticking .html on and use Safari File/Open File - but presumably what you need to do is parse it to extract just the data of interest, without involving a browser.

  • Austin Kinsella1 Level 6 Level 6 (11,505 points)

    The file after the -o is the output file and can include a path:

     

    do shell script "curl http://www.ebay.co.uk/sch/i.html?_nkw=mac -s -o /Users/mymac/Desktop/mac.html"

     

    I've included the -s option for silent mode to suppress the progress report.

  • Pierre L. Level 4 Level 4 (3,860 points)

    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

            click thePopUpButton

            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.

  • Pierre L. Level 4 Level 4 (3,860 points)

    I'd just like to know what's the advantage of replacing “delay 1” with “delay 4” in the repeat blocks. Does it change anything ?

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.