Previous 1 2 Next 15 Replies Latest reply: May 13, 2012 12:55 AM by ChangeAgent
ChangeAgent Level 1 (140 points)

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 (11,510 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 (11,510 points)

    Ooops, note the missing colon, should be

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

  • ChangeAgent Level 1 (140 points)

    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

  • Austin Kinsella1 Level 6 (11,510 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.

  • ChangeAgent Level 1 (140 points)

    Sorry some succes, this means, I get the file on my HD but can not open it with anything.  normaly when I do a save in Safari it saves it as .webarchive. Adding this to it also does not solve it once I find the file doe not allow me ot open it. 

     

    Message was edited by: ChangeAgent

  • ChangeAgent Level 1 (140 points)

    Did some further testing and added .webarchive to the name but it does not save it as a webarchive. 

  • Austin Kinsella1 Level 6 (11,510 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.

  • ChangeAgent Level 1 (140 points)

    ha, .html did it!  Did not think about that.  Ta.

  • ChangeAgent Level 1 (140 points)

    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


  • Austin Kinsella1 Level 6 (11,510 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 5 (4,235 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.

  • ChangeAgent Level 1 (140 points)

    Austin Kinsella1 wrote:

     

    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.

     

     

    Brilliant it works a dream. Thanks a million!

  • ChangeAgent Level 1 (140 points)

    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

      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

  • Pierre L. Level 5 (4,235 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 ?

Previous 1 2 Next