OS Ruud

Q: How do I make an Applescript Mouse Clicker with random clicks and random time intervals within a circle on Safari screen.

I know practically nothing about scripting. I tried some examples from the net but they are not specific enough to do the task I want it to. So I'd like it to do the following: click randomly within a specific circle on the Safari screen with random time intervals between clicks. With the result that the whole circle is used. Also I need it to run a separate click cycle every 15 to 20 minutes to turn an item off or on on the screen. I'f red that everything is scriptable! Is there anyone out there who can help me with this?

iMac, OS X Yosemite (10.10.4)

Posted on May 28, 2016 2:40 AM

Close

Q: How do I make an Applescript Mouse Clicker with random clicks and random time intervals within a circle on Safari screen.

  • All replies
  • Helpful answers

  • by leroydouglas,

    leroydouglas leroydouglas May 28, 2016 1:29 PM in response to OS Ruud
    Level 7 (22,887 points)
    Notebooks
    May 28, 2016 1:29 PM in response to OS Ruud

    Wow, that would be some trick shooting

     

    Is this on your local machine only  or web interface??

  • by OS Ruud,

    OS Ruud OS Ruud May 30, 2016 2:50 AM in response to leroydouglas
    Level 1 (18 points)
    Desktops
    May 30, 2016 2:50 AM in response to leroydouglas

    It's a web-interface. The circle is static and stays exactly the same.

  • by leroydouglas,

    leroydouglas leroydouglas May 30, 2016 6:44 AM in response to OS Ruud
    Level 7 (22,887 points)
    Notebooks
    May 30, 2016 6:44 AM in response to OS Ruud

    OS Ruud wrote:

     

    I know practically nothing about scripting. I tried some examples from the net but they are not specific enough to do the task I want it to. So I'd like it to do the following: click randomly within a specific circle on the Safari screen with random time intervals between clicks. With the result that the whole circle is used. Also I need it to run a separate click cycle every 15 to 20 minutes to turn an item off or on on the screen. I'f red that everything is scriptable! Is there anyone out there who can help me with this?

     

    It's a web-interface. The circle is static and stays exactly the same.

     

    This would be HTML5, CSS3, and jQuery or PHP server side coding to implement this undertaking,

     

    This is more website design and development question, not the purview of the Apple Support Communities. 


    The ASC is here to help people use Apple products and technologies more effectively.

     

    Sorry I can not be more helpful. Good luck !

  • by Roote,

    Roote Roote Jul 30, 2016 6:49 PM in response to OS Ruud
    Level 2 (417 points)
    Jul 30, 2016 6:49 PM in response to OS Ruud

    One option is to create a command-line utility for use either from Terminal or in a script such as AppleScript. Before using, backup all data. Use at your own risk.

     

    Keep in mind that the utility is designed to take control of your cursor and will click on anything you see onscreen. Therefore, make sure to plan and input arguments that when run only operate on the element, window, and app you want to target. Press the Shift-Command-4 keys to use the crosshair cursor to gather screen coordinates for the centre position of the click circle and to determine a value for its diameter. For easier viewing, increase the cursor size from the Accessibility pane in System Preferences.

    awwCrosshair.png

    When running, the utility will exit when the Esc key is pressed. When used as part of a script, the Esc key will not exit the script unless coded for.

     

    You will need to grant the application or program under which the utility is running access to your computer in Security & Privacy preferences.

    Accessibility.png

    This would include Terminal, Script Editor, and osascript. Since osascript runs as a background process and not as an application, you either can wait to be prompted when run or add it manually to the Accessibility database using a tool such as tccutil.py.

    osascript.png

    If setting up a schedule to run a script periodically, for instance using a launch agent and a launchd property list file, you’ll need to include code to prevent your computer from sleeping as well as disable the screen saver. You can use command-line tools such as pmset or systemsetup, though may find it simpler to use a third-party scriptable app/menulet such as Amphetamine.

     

    Following is a command example and explanation of the utility’s parameters:

     

    circlick -c 1200 -d 300 -r 0.01 5.0 -p 720,450

     

    -c Clicks. The number of clicks. A positive integer, e.g. 1, 29, 345, etc.

    -d Diameter. The diameter of the circle in pixel units. A positive integer, e.g. 30, 155, 420, etc.

    -r Range. Minimum and maximum random time in seconds between clicks. Two float numbers separated by a space, e.g. 0.05 6.0

    -p Position. The position of the centre of the circle consisting of x and y screen coordinates in pixel units. Two values separated by a comma, e.g. 720,450.

     

    All parameters are required. If a parameter is missing, missing a value or the wrong value type inputed, an exception is thrown and an error message will display upon exit.

     

    Copy and paste the following Swift code into a plain text editor and save it to your Desktop with a .swift file extension using a name such as circlick.swift:

     

    import Foundation

    import Cocoa

     

    var clickCounter : Int32 = 0

    var clicks : Int32 = 0

     

    func ValidateProcessArguments(arguments: [String]) {

        if (arguments.count != 10) {

            print("Not all parameters were supplied or entered correctly")

            exit(1)

        }

    }

     

    func GetValueFromArguments(argKey: String, arguments: [String], index: Int = 0) -> String {

        for argPos in 1...arguments.count {

            if (arguments[argPos] == argKey) {

                return arguments[argPos + index + 1]

            }

        }

        return ""

    }

     

    func ParsePosition(position: String) -> CGPoint {

        let positionSplit = position.componentsSeparatedByString(",")

        let x = Int(positionSplit[0])!

        let y = Int(positionSplit[1])!

      

        return CGPoint(x: x, y: y)

    }

     

    func RandomDouble(min min: Double, max: Double) -> Double {

        return drand48() * (max - min) + min

    }

     

    func ClickMouse(at position: CGPoint) {

        let mouseDown = CGEventCreateMouseEvent(nil, .LeftMouseDown, position, .Left)

        let mouseUp = CGEventCreateMouseEvent(nil, .LeftMouseUp, position, .Left)

      

        CGEventPost(.CGHIDEventTap, mouseDown)

        CGEventPost(.CGHIDEventTap, mouseUp)

    }

     

    func delay(delay:Double, closure:()->()) {

        dispatch_after(

            dispatch_time(

                DISPATCH_TIME_NOW,

                Int64(delay * Double(NSEC_PER_SEC))

            ),

            dispatch_get_main_queue(), closure)

    }

     

    func Click(radius: Double, position: CGPoint, rangeStart: Double, rangeEnd: Double)

    {

        OSAtomicIncrement32(&clickCounter)

      

        let t = RandomDouble(min:0.0, max: 2 * M_PI)

        let r = radius * sqrt(RandomDouble(min: 0.0, max: 1.0))

        let x = r * cos(t)

        let y = r * sin(t)

        ClickMouse(at: CGPointMake(position.x + CGFloat(x), position.y + CGFloat(y)))

      

        let clickDelay = RandomDouble(min: rangeStart, max: rangeEnd)

      

        if (clickCounter == clicks) {

            delay(0.1) { exit(0) }

        }

        else {

            delay(clickDelay) {

                Click(radius, position: position, rangeStart: rangeStart, rangeEnd: rangeEnd)

            }

        }

    }

     

    func ClickUtilityWrapper() {

        srand48(Int(NSDate().timeIntervalSinceReferenceDate));

      

        let processArguments = Process.arguments

        ValidateProcessArguments(processArguments)

      

        let radius = Double(GetValueFromArguments("-d", arguments: processArguments))! / 2

        clicks = Int32(GetValueFromArguments("-c", arguments: processArguments))!

        let positionParameter = GetValueFromArguments("-p", arguments: processArguments)

        let position = ParsePosition(positionParameter)

        let rangeStart = Double(GetValueFromArguments("-r", arguments: processArguments))!

        let rangeEnd = Double(GetValueFromArguments("-r", arguments: processArguments, index: 1))!

      

        Click(radius, position: position, rangeStart: rangeStart, rangeEnd: rangeEnd)

    }

     

    func TryEnableEscHandler() {

        let accessEnabled = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue( ) as String: true])

        if (!accessEnabled) {

            print("Please enable Accessibility settings and restart Terminal")

            exit(0)

        }

      

        let mask = (NSEventMask.KeyDownMask)

        NSEvent.addGlobalMonitorForEventsMatchingMask(mask, handler: handlerEvent)

    }

     

    func handlerEvent(aEvent: (NSEvent!)) -> Void {

        if (aEvent.keyCode == 53) {

            print("Program cancelled by user")

            exit(0)

        }

    }

     

    class ApplicationDelegate: NSObject, NSApplicationDelegate {

        func applicationDidFinishLaunching(notification: NSNotification) {

            TryEnableEscHandler()

            ClickUtilityWrapper()

        }

    }

     

    let application = NSApplication.sharedApplication()

     

    let applicationDelegate = ApplicationDelegate()

    application.delegate = applicationDelegate

    application.activateIgnoringOtherApps(true)

    application.run()

     

    With Xcode installed, compile the above code with the Swift compiler in Terminal using the following command which outputs the binary to /usr/local/bin:

     

    xcrun -sdk macosx swiftc ~/Desktop/circlick.swift -o /usr/local/bin/circlick

     

    Use of the utility in a basic Applescript:

     

    -- Assumes Safari is open and the target webpage is loaded into the current tab of window 1

    tell application "Safari"

      activate

      delay 1

      do shell script "/usr/local/bin/circlick -c 25 -d 50 -r 0.01 5.0 -p 450,720"

    end tell

     

    To visualize clicks when the utility is run from Terminal you can use a graphics application. The following example uses Autodesk SketchBook. Open the app, set up the canvas and brush and run a variation of the following command-line example in Terminal, adjusting values as appropriate:

     

    osascript -e 'tell application "SketchBook" to activate'; sleep 2; circlick -c 1200 -d 300 -r 0.01 0.01 -p 410,365

    SketchBook.png

    To visualize clicks on a webpage when the utility is used in a script, you can use an online web graphics application. The following example uses A Web Whiteboard. Copy and paste the following AppleScript into Script Editor and run:

     

    (*

    NOTE: If applicable, "Allow JavaScript from Apple Events"

    must be selected from Safari Develop menu

     

    Hold down the Esc key to exit

     

    You may want to suppress/disable other audio playing

     

    Music (or Sound Effects) by Eric Matyas

    www.soundimage.org

    *)

     

    set theURL to "https://awwapp.com"

    set audioFile to "'https://dl.dropboxusercontent.com/s/c0ieg57td4tr16t/aww.mp3?dl=0'"

     

    global screenSize

    tell application "Finder"

      set screenSize to bounds of window of desktop

    end tell

     

    global scrptName

    tell application "System Events"

      set scrptName to get name of (path to me)

    end tell

     

    global vol

    set vol to output volume of (get volume settings)

    if vol < 30 then

      set volume output volume 30

    end if

     

    try

      tell application "Safari"

      activate

      open location theURL

      set bounds of window 1 to screenSize

      set isLoaded to false

      repeat while isLoaded is false

      set testString to "Whiteboard"

      set readyState to (do JavaScript "document.readyState" in document 1)

      set pageText to text in current tab of window 1

      if (readyState is "complete") and (pageText contains testString) then set isLoaded to true

      delay 0.5

      end repeat

      end tell

    on error

      set volume output volume vol

      return

    end try

     

    try

      do shell script "curl " & audioFile & " -o /tmp/aww.mp3"

      do shell script "afplay /tmp/aww.mp3 &>/dev/null &"

    end try

     

    delay 2

     

    global circlick

    tell application "Safari"

      set usrCncl to "Program cancelled by user"

      set awwColor to "document.getElementsByClassName('aww-color-icon')"

      do JavaScript awwColor & "[0].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[7].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[1].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[5].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[2].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[6].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[3].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      do JavaScript awwColor & "[4].click();" in current tab of window 1

      my clickUtility()

      if circlick = usrCncl then

      my escPressed()

      else

      display notification scrptName & " is finished"

      say "The circlick script is finished"

      delay 0.5

      set volume output volume vol

      end if

      end if

      end if

      end if

      end if

      end if

      end if

      end if

    end tell

     

    on escPressed()

      do shell script "killall afplay &>/dev/null &"

      display notification "User cancelled " & scrptName sound name "default"

      delay 0.5

      set volume output volume vol

    end escPressed

     

    on clickUtility()

      set params to "/usr/local/bin/circlick -c 300 -d 300 -r .007 .007 -p "

      set abscissa to ((item 3 of screenSize) / 2) as integer

      set ordinate to ((item 4 of screenSize) / 2) as integer

      delay 0.7

      set circlick to do shell script params & abscissa & "," & ordinate

    end clickUtility

     

    Also I need it to run a separate click cycle every 15 to 20 minutes to turn an item off or on on the screen.

     

    As far as a separate click cycle you can use a variation of a basic AppleScript with a repeat loop:

     

    -- Assumes Safari is open and the target webpage is loaded into the frontmost window

    tell application "Safari"

      activate

      delay 1

      repeat 3 times

      tell application "System Events"

      tell process "Safari"

      click at {35, 130} -- x, y screen coordinates

      do shell script “sleep 3" -- for 20 min, use 1200

      end tell

      end tell

      end repeat

    end tell

     

    Anything more elaborate and you may want to create a launch agent to periodically run an AppleScript without a repeat loop. Another option is to use JavaScript in an AppleScript to click on a webpage element:

     

    -- Assumes Safari is open and the target webpage is loaded into the current tab of window 1

    tell application "Safari"

      set myVariable to "document.getElementsByClassName(’target-class-name')"

      repeat 3 times

      -- number below in square brackets corresponds to the class element to click

      do JavaScript myVariable & "[0].click();" in current tab of window 1

      do shell script "sleep 3" -- for 20 min, use 1200

      end repeat

    end tell

     

    Use Safari Web Inspector to obtain information for the specific element you wish to click.

     

    For a general purpose click tool, look at cliclick.

     

    Tested with OS X Yosemite 10.10.5, Script Editor 2.7, AppleScript 2.4, Terminal 2.5.3, Xcode 7.2.1, osascript, tccutil.py, SketchBook 8.1.1, Amphetamine 2.3.1