0

I am looking for a script that would press "Q" every second when idle for a certain amount of time in an application. I have never coded before and have several issues with code (below) that'd I have no idea how to fix

Here is the code I've tried:

repeat 52 times
    repeat 52 times
        delay 0.5
        tell application "Roblox"
            set currentTab to do
            script ("q") in front window 
        end tell 
        delay 0.5 
    end repeat 
end repeat

It errors with:

error "The variable do is not defined." number -2753 from "do"

user3439894
  • 58,676
  • There are lots of existing questions/answers that address this very topic about key presses in various apps via AppleScript. What research have you done, what have you tried and what were the results? What exactly are you attempting to accomplish with this? – Allan Nov 04 '20 at 23:00
  • I have done research on the topic but I can't code and several errors occur when I try to run the script I find. I don't want to keep constantly pressing q while watching Netflix, to level up in a game, so I want to find a way to auto press the key. – Anonymous123 Nov 04 '20 at 23:05
  • I have done research on the topic but I can't code and several errors occur. You are aware that we can't see over your shoulder, right? I'm not trying to be flippant, but please understand...you need to help us help you. – Allan Nov 04 '20 at 23:08
  • Ok. One of the scripts I'm trying to use is repeat 52 times repeat 52 times delay 0.5 tell application "Roblox" set currentTab to do script ("q") in front window end tell delay 0.5 end repeat The error that comes up on this is "the variable do is not defined" It's simple stuff like this that I don't know how to fix. All I need is a simple script that has the letter "q" pressed when I'm idle on the application. – Anonymous123 Nov 04 '20 at 23:22
  • 2
    Please [edit] the question with the new details and not post them in comments. It's impossible to see the structure/syntax of your code. Comments are for asking clarifying questions/feedback and there's no way to provide enough context for people to understand what you're posting. – Allan Nov 05 '20 at 01:38
  • Please add your current script to the question, how you run it with Roblox and also some details about the error you get. – nohillside Nov 05 '20 at 06:32
  • The picture of the script in the description is what I am trying to use. The error I get is that "do" is not defined. When I try to define do (variable "do" = execute) it tells me that the variable "variable" is not defined. – Anonymous123 Nov 05 '20 at 18:53
  • Please do not post unnecessary screen shots when simply copying and pasting is all that necessary. – user3439894 Nov 05 '20 at 19:09
  • The two lines of code set currentTab to do and script ("q") in front window should be on one line, e.g. set currentTab to do script ("q") in front window, however, that is an improper use of the do script command! That said, there are hundreds of examples of how to use System Events and the keystroke/key code command on the Internet, I know because I've writen more than a few of them. – user3439894 Nov 05 '20 at 19:16
  • Also, Ask Different is not a code writing service! Please take the time to search the Internet for applescript sending keystrokes and do some reading. There are plenty of working examples out there! The top hit for that search string is: How do I automate a key press in AppleScript? – user3439894 Nov 05 '20 at 19:23

1 Answers1

2

I do not have Roblox, so I can not help you with anything Roblox application specific. However, the following example AppleScript code does in one repeat loop, as I fail to see the need for two repeat loops, it sends a keystroke to the frontmost application, in this case Roblox.

repeat 2704 times
    tell application "Roblox" to activate
    delay 0.5
    tell application "System Events" to keystroke "q"
    delay 0.5
end repeat
user3439894
  • 58,676
  • Thank you, it worked. When I first tried it, though I got an error saying System Events doesn't allow Script Editor to send keystrokes. I was able to easily fix it after allowing accessibility in security and privacy settings. – Anonymous123 Nov 05 '20 at 23:57