Applescript: execute Javascript in Safari in the background

Discussion in 'Mac Programming' started by sergiobaschi, Dec 18, 2014.

  1. sergiobaschi macrumors regular

    Nov 30, 2012
    Gothenburg, Sweden
    I have an Applescript that works flawlessly. It opens a Safari window, goes to a specific URL. Then I use this code to log in:
    set loginForm to "document.getElementById('username').value = '" & login1 & "'; document.getElementById('password').value = '" & login2 & "'; document.getElementById('commonlogin').submit();"
    do JavaScript loginForm in current tab of first window
    (login1 and login2 are declared earlier)

    On the inside, I search for a numeric value in the source code using som other Javascript. Then I close the Safari window.

    This works just fine – but I would like it to run without me seeing it. I need it to run like every 30 minute, and if I'm using the computer at the time, it would be really annoying to have a Safari window opening up twice every hour.

    Any solutions?

  2. iSee macrumors 68040


    Oct 25, 2004
    I don't know how to get Safari to run like that.

    But if I were doing something like what you're doing I'd try using a headless browser -- like PhantomJS. It runs from the command line and doesn't popup a app window so you can automate it various standard ways. You control the browser using Javascript, not Applescript.
  3. 960design macrumors 68030

    Apr 17, 2012
    Destin, FL
    Applescript probably not the best choice for your needs.

    Not knowing the full details, I could guess that a cron job posting form data; retrieving the results to a database or emailing / texting a notification if what you were looking was or wasn't found would be a better choice.
  4. Alrescha, Dec 19, 2014
    Last edited: Dec 19, 2014

    Alrescha macrumors 68020

    Jan 1, 2008
    Another possibility would be to use wget (or curl, which comes with OS X) to do the fetch and avoid using a GUI solution for a non-GUI problem.


    Addendum: In my enthusiasm, I ignored the title of the thread which clearly says 'Javascript', which is not supported by wget or curl as far as I know. Mea Culpa.

Share This Page