Create a script to log into webpage and do action?

Discussion in 'Mac Programming' started by Quantum-DK, Nov 13, 2014.

  1. Quantum-DK, Nov 13, 2014
    Last edited: Nov 13, 2014

    Quantum-DK macrumors newbie

    Nov 12, 2014
    Hi out there!

    Im entirely new in here, but i hope you guys can help me in any way possible with a project i would like to start on. This also mean that i don't even know what I'm getting into, or how hard/easy it's going to be.

    I would like to create a script to run in terminal each night of saturday, sunday monday, tuesday and wednesday for this part iv'e been told to use cron.

    Now for the script i would like to run through cron:
    It has to log into a certain webpage, where my username/password has already been typed in (thought it would make the process easier if i made the page remember my logon-info)
    - Then i want it to book a room for my (it's at my school), i need to specify which kind of room, for how long (max = 4 hours, so i always want to book 4 hours), which date (When booking saturday morning at 00.00 it's possible to book 48 hours ahead, so it books the room for monday morning), sunday at 00.00 i want it to book tuesday and so on.
    - Lastly i want it to log out after booking ofc.

    My question to you guys is... Is this possible? (I guess yes, but) how hard is it? My only coding experience is in Excel VBA and a program called OX (sort of like matlab, a bit more rigid code). I know it isn't much, but that doesn't mean it's impossible, i like coding and would enjoy to learn something about using the Terminal app.

    - Any kind of help will be appreciated beyond the skies!!! If you know a smarter (Easier) way than what I'm talking about, i would like to hear it too! :)
  2. timbos macrumors member

    Jan 2, 2009
    There's a tool called curl which is designed to get webpages. It can also post data to a form on a webpage.

    The curl manual describes the syntax here:

    You'd create a command that sends the relevant data, and then use cron, which is a tool that's designed to automate running commands at a specific time.

    There's a bit of learning involved in doing this, but it's not too hard and you'll be better for having worked it out ;-)
  3. Quantum-DK thread starter macrumors newbie

    Nov 12, 2014
    Thanks timbos ill start reading and trying!!! :)
    - If anybody else have some suggestions of where to start, i always appreciate more help! :)

  4. DennisBlah macrumors 6502


    Dec 5, 2013
    The Netherlands
    Indeed, curl fits the best to your question like.. you want to run from terminal.

    Other way is, let applescript inject some javascript or jquery into a open page in safari, however you need the page open. (You can let applescript open the page for you etc ofcourse) and again use a cronjob to trigger it whenever you want it.

    One way or another, you need to know how forms are working. Variables to set and where to 'post' them to.

    <form action=">>>URL Where the post goes<<<">

    <input type="text" name=">>>Variable name that contains value for the post<<<">

    Here a short sample of one of my old applescript codes
    tell application "Safari"
       set doc to front document
       tell doc
          do javascript "document.getElementsByName('login_user')[0].setAttribute('value','dennisBlah');
       end tell
    end tell
    However I like the jquery way more :)

    set startJqueryInjection to do shell script "curl"
    tell application "Safari"
       set doc to front document
       tell doc
          do JavaScript startJqueryInjection
          do JavaScript "$(function() {
       end tell
    end tell
    In the end you don't need the activate and can let this run in the background.

    Anyways, this is just a nice to learn thingy.
    No need to save session/cookie data to call back with a new curl command etc as you are working in a actual browser.

    Maybe someone will ever find this usefull as it took a while for me to get this all working right ;-)
    Some function to wait till pages are done loading etc.
  5. Quantum-DK thread starter macrumors newbie

    Nov 12, 2014

    Thank you much! At least now i know where to spend my time i guess ;)

Share This Page