Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

I need help creating an applescript for autoclicking.

I just started using AppleScript so there is still a lot of things that I do not know. I am currently trying to make a script where I can click in an infinite loop/repeat. I've gotten that part figured out and now I want to step it up a notch by making a condition where in when I right click with my mouse I'll be able to toggle this script on and off. Or even turning off the loop with right click. As an extra I would also like to add a bonus dialog once I have ended the script through the right click.

here is the process of my work.


tell application "System Events"


--looping (repeat, repeat # times)

repeat 10 times


--application trigger system event to use key code 87 (left click on mouse)

tell application "System Events" to key code 87


delay 1

end repeat

end tell


--right click

tell application "System Events" to key code 87 using {control down}


-- closing of script

display dialog "Do you want to exit this script?" buttons {"Exit", "Continue"} default button 1

if result = {button returned:"Exit"} then

return

end if

The upper portion of the script is what i've mentioned first where I can get an infinite loop going for the left click mouse button. The bottom two scripts is what I would like to integrate into the above script to get the result that I want. An infinite loop with a condition (right click) to turn off the script and a message to appear after the script has shut down.

MacBook Pro with Retina display, OS X El Capitan (10.11.4), null

Posted on May 4, 2016 7:38 PM

Reply
Question marked as Best reply

Posted on May 20, 2016 12:20 AM

Hi Cloud4846. One option is to create a small utility to detect a depressed mouse button for use in an AppleScript. Copy and paste the following Swift code into a text editor such as Sublime Text and save it your Desktop with a .swift file extension using a name such as mouse.swift:


import Foundation

import AppKit


func TranslateButtonState(buttonState: Int) -> String {

if (buttonState == 1 << 0) { return "left"; }

if (buttonState == 1 << 1) { return "right"; }


return "none";

}


var buttonState = NSEvent.pressedMouseButtons()

print(TranslateButtonState(buttonState))


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


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


You can test the utility in Terminal by preceding the mouse command with a delay and depressing the left or right mouse button:


sleep 1; mouse


Output returned is one of “left”, “right”, or “none”. The utility also works with a one finger or two finger mechanical depression of a trackpad for the primary and secondary click respectively.


You can use the utility in an AppleScript to display a dialog message when a mouse button is depressed, in this example the right mouse button. Keep in mind that because of AppleScript’s single-threaded nature and that the "do shell script" is in the loop with a delay, this is really only practical with short delays as longer delays necessitate holding down the mouse button longer:


tell application "System Events"

repeat 10 times

set mouseDown to (do shell script "/usr/local/bin/mouse")

if mouseDown = "right" then

key code 87

display dialog "Do you want to exit this script?" buttons {"Exit", "Continue"} default button 1

if result = {button returned:"Exit"} then

return

end if

else

delay 0.5

key code 87

delay 0.5

end if

end repeat

end tell


Rather than use a mouse button down event to trigger a dialog, you can use a key press such as a modifier key. Copy and paste the following Swift code into a text editor and save it to your Desktop with a .swift file extension using a name such as modKeys.swift:


import Foundation

import AppKit


func TranslateModifierFlags(modifierFlags: NSEventModifierFlags) -> String {

let rawModifierFlags = modifierFlags.rawValue

var pressedButtons = [String]()


if ((rawModifierFlags & NSEventModifierFlags.ControlKeyMask.rawValue) != 0) { pressedButtons.append("Control") }


if ((rawModifierFlags & NSEventModifierFlags.AlternateKeyMask.rawValue) != 0) { pressedButtons.append("Option") }


if ((rawModifierFlags & NSEventModifierFlags.ShiftKeyMask.rawValue) != 0) { pressedButtons.append("Shift") }


if ((rawModifierFlags & NSEventModifierFlags.CommandKeyMask.rawValue) != 0) { pressedButtons.append("Command") }


if (pressedButtons.count > 0) {

return pressedButtons.joinWithSeparator(" ")

}


return "none"

}


var modifierFlags = NSEvent.modifierFlags()


print(TranslateModifierFlags(modifierFlags))


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


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


You can test the utility in Terminal by preceding the mouse command with a delay and depressing any of the four modifier keys Control, Option, Shift, and Command either individually or in combination:


sleep 1; modKeys


Output returned is one or more of “Control”, “Option”, “Shift”, and “Command”. An example of the utility and the Shift key used in an Applescript:


tell application "System Events"

repeat 10 times

set keyDown to (do shell script "/usr/local/bin/modKeys")

if keyDown = “Shift" then

key code 87

display dialog "Do you want to exit this script?" buttons {"Exit", "Continue"} default button 1

if result = {button returned:"Exit"} then

return

end if

else

delay 0.5

key code 87

delay 0.5

end if

end repeat

end tell


The most appropriate modifier keys to use are Shift and Option.

User uploaded file

Tested with OS X Yosemite 10.10.5, Script Editor 2,7, AppleScript 2.4, Xcode 7.2.1


AppKit Framework Reference > NSEvent Class Reference

The Swift Programming Language

Introduction to AppleScript Language Guide

3 replies
Question marked as Best reply

May 20, 2016 12:20 AM in response to Cloud4846

Hi Cloud4846. One option is to create a small utility to detect a depressed mouse button for use in an AppleScript. Copy and paste the following Swift code into a text editor such as Sublime Text and save it your Desktop with a .swift file extension using a name such as mouse.swift:


import Foundation

import AppKit


func TranslateButtonState(buttonState: Int) -> String {

if (buttonState == 1 << 0) { return "left"; }

if (buttonState == 1 << 1) { return "right"; }


return "none";

}


var buttonState = NSEvent.pressedMouseButtons()

print(TranslateButtonState(buttonState))


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


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


You can test the utility in Terminal by preceding the mouse command with a delay and depressing the left or right mouse button:


sleep 1; mouse


Output returned is one of “left”, “right”, or “none”. The utility also works with a one finger or two finger mechanical depression of a trackpad for the primary and secondary click respectively.


You can use the utility in an AppleScript to display a dialog message when a mouse button is depressed, in this example the right mouse button. Keep in mind that because of AppleScript’s single-threaded nature and that the "do shell script" is in the loop with a delay, this is really only practical with short delays as longer delays necessitate holding down the mouse button longer:


tell application "System Events"

repeat 10 times

set mouseDown to (do shell script "/usr/local/bin/mouse")

if mouseDown = "right" then

key code 87

display dialog "Do you want to exit this script?" buttons {"Exit", "Continue"} default button 1

if result = {button returned:"Exit"} then

return

end if

else

delay 0.5

key code 87

delay 0.5

end if

end repeat

end tell


Rather than use a mouse button down event to trigger a dialog, you can use a key press such as a modifier key. Copy and paste the following Swift code into a text editor and save it to your Desktop with a .swift file extension using a name such as modKeys.swift:


import Foundation

import AppKit


func TranslateModifierFlags(modifierFlags: NSEventModifierFlags) -> String {

let rawModifierFlags = modifierFlags.rawValue

var pressedButtons = [String]()


if ((rawModifierFlags & NSEventModifierFlags.ControlKeyMask.rawValue) != 0) { pressedButtons.append("Control") }


if ((rawModifierFlags & NSEventModifierFlags.AlternateKeyMask.rawValue) != 0) { pressedButtons.append("Option") }


if ((rawModifierFlags & NSEventModifierFlags.ShiftKeyMask.rawValue) != 0) { pressedButtons.append("Shift") }


if ((rawModifierFlags & NSEventModifierFlags.CommandKeyMask.rawValue) != 0) { pressedButtons.append("Command") }


if (pressedButtons.count > 0) {

return pressedButtons.joinWithSeparator(" ")

}


return "none"

}


var modifierFlags = NSEvent.modifierFlags()


print(TranslateModifierFlags(modifierFlags))


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


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


You can test the utility in Terminal by preceding the mouse command with a delay and depressing any of the four modifier keys Control, Option, Shift, and Command either individually or in combination:


sleep 1; modKeys


Output returned is one or more of “Control”, “Option”, “Shift”, and “Command”. An example of the utility and the Shift key used in an Applescript:


tell application "System Events"

repeat 10 times

set keyDown to (do shell script "/usr/local/bin/modKeys")

if keyDown = “Shift" then

key code 87

display dialog "Do you want to exit this script?" buttons {"Exit", "Continue"} default button 1

if result = {button returned:"Exit"} then

return

end if

else

delay 0.5

key code 87

delay 0.5

end if

end repeat

end tell


The most appropriate modifier keys to use are Shift and Option.

User uploaded file

Tested with OS X Yosemite 10.10.5, Script Editor 2,7, AppleScript 2.4, Xcode 7.2.1


AppKit Framework Reference > NSEvent Class Reference

The Swift Programming Language

Introduction to AppleScript Language Guide

May 20, 2016 12:49 AM in response to Roote

Erratum:


"You can test the utility in Terminal by preceding the mouse command with a delay and depressing any of the four modifier keys Control, Option, Shift, and Command either individually or in combination:"


Should be:


You can test the utility in Terminal by preceding the modKeys command with a delay and depressing any of the four modifier keys Control, Option, Shift, and Command either individually or in combination:

I need help creating an applescript for autoclicking.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.