What is wrong with my AppleScript code?

What is wrong with my code, in applescript it does not type like it is supposed to sometimes. Can someone help fix my code?


tell application "System Events"

set textToType to "pls"

delay 1

set textToType2 to "i"

delay 1

set textToType3 to "pls hunt"

delay 1

set textToType4 to "pls dig"

delay 1

set textTotype5 to "pls use horse"

delay 1

set textToType6 to "pls beg"

delay 1

set textToType7 to "pls daily"

delay 1

set textToType8 to "pls use daily"

delay 1

repeat

set x1 to random number from 6 to 9

set x2 to random number from 6 to 9

set x3 to random number from 6 to 9

set x4 to random number from 6 to 9

set x5 to random number from 6 to 9

set x6 to random number from 6 to 9

set x7 to random number from 6 to 9

set x8 to random number from 6 to 9

set commandChoice to " crime"

set commandChoice5 to " use horse"

set commandList to {" beg", " fish"}

set commandList2 to {"", ""}

set commandList3 to {" hunt", " dig"}

set commandList4 to {"", ""}

set commandList5 to {" scout", " hl"}

set commandList6 to {"", ""}

set commandChoice8 to " crime"

set item 1 of commandList2 to some item of commandList

set item 2 of commandList2 to some item of commandList

repeat until item 1 of commandList2 is not item 2 of commandList2

set item 2 of commandList2 to some item of commandList

end repeat

set {commandChoice1, commandChoice2} to commandList2

set item 1 of commandList4 to some item of commandList3

set item 2 of commandList4 to some item of commandList3

repeat until item 1 of commandList4 is not item 2 of commandList4

set item 2 of commandList4 to some item of commandList3

end repeat

set {commandChoice3, commandChoice4} to commandList4

set item 1 of commandList6 to some item of commandList5

set item 2 of commandList6 to some item of commandList5

repeat until item 1 of commandList6 is not item 2 of commandList6

set item 2 of commandList4 to some item of commandList5

end repeat

set {commandChoice6, commandChoice7} to commandList6

delay x5

keystroke textToType & commandChoice1

keystroke return

delay x1

keystroke textToType & commandChoice2

keystroke return

delay x2

keystroke textToType & commandChoice3

keystroke return

delay x3

keystroke textToType & commandChoice4

keystroke return

delay x4

keystroke textToType & commandChoice5

keystroke return

delay x6

keystroke textToType & commandChoice6

keystroke return

delay x7

keystroke textToType & commandChoice7

keystroke return

delay x8

keystroke textToType & commandChoice8

keystroke return

end repeat

end tell


[Re-Titled by Moderator]


Posted on Dec 13, 2021 2:20 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 14, 2021 1:35 PM

> I am trying to create an autotyper, but when I run this script there are no bugs, but sometimes the autotyper works and sometimes it does not.


I suspect your autotyper never works - at least not in the way you expect it to.


For example, your script starts off defining a whole series of textToTypeN variables:


set textToType to "pls"
delay 1
set textToType2 to "i"
delay 1
set textToType3 to "pls hunt"
delay 1
set textToType4 to "pls dig"
delay 1
set textTotype5 to "pls use horse"
delay 1
set textToType6 to "pls beg"
delay 1
set textToType7 to "pls daily"
delay 1
set textToType8 to "pls use daily"
delay 1


First it contains a number of superfluous delay statements that do nothing but slow down your script, and could be just as effective with a simple delay 8 statement at the end.

More importantly, though, 7 of these 8 variables are never referenced again in the script, so either you're not showing the whole script, or you're expecting these variables to do something they're never going to do.


Secondly, you are using keystroke, but there is no indication as to where the script should type - it's entirely up to you to make sure the expected application is frontmost when the script fires, and that the script is ready to accept input.


Ultimately, you use case appears to be highly application-specific, and you haven't actually said what doesn't work.


When I run it here, I get what looks to be valid results - pseudo-randomized commands, with varying delays. However, when applied in-game (I assume this is game-related), you need to explain what doesn't work - for example, do the commands not appear at all (the app is suppressing automated keystokes?), or maybe some commands do, but others don't, or maybe half the command (e.g. "pls ") goes through but the rest doesn't.

The specific failure case here is likely to be relevant in troubleshooting the problem. Many games, for example, implement key logging technology to prevent users from auto typing (AppleScript will 'type' the text far faster than any human), so it may be that the application thinks you're typing too fast and is blocking your commands. No way of knowing with such limited data.

6 replies
Question marked as Top-ranking reply

Dec 14, 2021 1:35 PM in response to deadmeat1

> I am trying to create an autotyper, but when I run this script there are no bugs, but sometimes the autotyper works and sometimes it does not.


I suspect your autotyper never works - at least not in the way you expect it to.


For example, your script starts off defining a whole series of textToTypeN variables:


set textToType to "pls"
delay 1
set textToType2 to "i"
delay 1
set textToType3 to "pls hunt"
delay 1
set textToType4 to "pls dig"
delay 1
set textTotype5 to "pls use horse"
delay 1
set textToType6 to "pls beg"
delay 1
set textToType7 to "pls daily"
delay 1
set textToType8 to "pls use daily"
delay 1


First it contains a number of superfluous delay statements that do nothing but slow down your script, and could be just as effective with a simple delay 8 statement at the end.

More importantly, though, 7 of these 8 variables are never referenced again in the script, so either you're not showing the whole script, or you're expecting these variables to do something they're never going to do.


Secondly, you are using keystroke, but there is no indication as to where the script should type - it's entirely up to you to make sure the expected application is frontmost when the script fires, and that the script is ready to accept input.


Ultimately, you use case appears to be highly application-specific, and you haven't actually said what doesn't work.


When I run it here, I get what looks to be valid results - pseudo-randomized commands, with varying delays. However, when applied in-game (I assume this is game-related), you need to explain what doesn't work - for example, do the commands not appear at all (the app is suppressing automated keystokes?), or maybe some commands do, but others don't, or maybe half the command (e.g. "pls ") goes through but the rest doesn't.

The specific failure case here is likely to be relevant in troubleshooting the problem. Many games, for example, implement key logging technology to prevent users from auto typing (AppleScript will 'type' the text far faster than any human), so it may be that the application thinks you're typing too fast and is blocking your commands. No way of knowing with such limited data.

Dec 14, 2021 9:08 PM in response to deadmeat1

Add a 0.3 to 2.0 second delay between "typed" characters.




use AppleScript version "2.4"
-- Apple Changed how scripting additions works to run many oof the additions under your apppication, Script Editor.
--   https://forum.latenightsw.com/t/weird-use-scripting-additions-problem/2177/5
use scripting additions
use framework "Foundation" --for ASObjC


on run --start
	set theApp to "TextEdit"
	-- what launch and activate application has changed over the years
	-- I assume launch loads the application if needed
	tell application theApp
		-- I assume launch waits until the app is load to give back control
		launch
	end tell
	
	-- start the front most application
	-- I assume activate if not loaded, loads the application. When loaded, invokes the application
	tell application theApp
		activate
	end tell
	
	-- how effective is delay scripting addition?	
	-- little to do with the probem. We are testing the accuracy of delay scripting addition
	log "start delay 2"
	log localDateTime()
	-- just a test to see how small of a fraction of a second delay supports
	repeat 2 times -- loop twice for two seconds
		repeat 100 times -- 1 second
			delay 0.01
		end repeat
	end repeat
	log localDateTime()
	log "looping delay" & return & "more delay..."
	
	log "Application to input characters should be active. No? Increase delay time"
	delay 10 -- two seconds was too little time
	
	-- modifiers are not supported by randChars. You need to wing it.
	tell application "System Events"
		-- TextEdit gives a beep to this.
		keystroke space using command down
	end tell
	fractionDelay()
	
	tell application "System Events"
		keystroke "a" using option down -- which is å
	end tell
	fractionDelay()
	
	
	randChars("Hello world!")
	
end run


-- ---------------------------------------


on fractionDelay()
	tell current application
		set fractionTime to random number from 30 to 200 -- 0.3 to 2 seconds
	end tell
	set fractionTime to fractionTime / 100
	log "fractionTime is " & fractionTime
	log localDateTime()
	delay fractionTime
	
end fractionDelay


on localDateTime()
	set now to current application's class "NSDate"'s |date|()
	set dateFormatter to current application's class "NSDateFormatter"'s new()
	tell dateFormatter to setDateFormat:("MM dd yyyy  HHmmss.SSS")
	set timeStamp to (dateFormatter's stringFromDate:(now)) as text
	return timeStamp
end localDateTime


on randChars(theCharacters)
	
	repeat with i from 1 to length of theCharacters
		
		set thisChar to text (i) thru i of theCharacters
		log "thisChar is " & thisChar
		(*
		tell current application
			set fractionTime to random number from 1 to 200
		end tell
		set fractionTime to fractionTime / 100
		log "fractionTime is " & fractionTime
		log localDateTime()
		delay fractionTime
		*)
		
		tell application "System Events"
			keystroke thisChar
		end tell
		
		fractionDelay()
		
		
		
	end repeat
	
	return
end randChars

Dec 14, 2021 1:10 PM in response to rccharles

I am trying to create an autotyper, but when I run this script there are no bugs, but sometimes the autotyper works and sometimes it does not. I was asking if there are any logic errors or such that cause that in this script. I also want the autotyper to have a randomizer. It works on a smaller scale script: tell application "System Events"

set textToType to "pls"

delay 1

set textToType2 to "i"

delay 1

set textToType3 to "pls hunt"

delay 1

set textToType4 to "pls dig"

delay 1

set textTotype5 to "pls use horse"

delay 1

set textToType6 to "pls beg"

delay 1

set textToType7 to "pls daily"

delay 1

set textToType8 to "pls use daily"

delay 1

repeat

set x1 to random number from 9 to 13

set x2 to random number from 9 to 13

set x3 to random number from 9 to 13

set x4 to random number from 9 to 13

set x5 to random number from 9 to 13

set commandChoice to " crime"

set commandChoice5 to " use horse"

set commandList to {" beg", " fish"}

set commandList2 to {"", ""}

set commandList3 to {" hunt", " dig"}

set commandList4 to {"", ""}

set item 1 of commandList2 to some item of commandList

set item 2 of commandList2 to some item of commandList

repeat until item 1 of commandList2 is not item 2 of commandList2

set item 2 of commandList2 to some item of commandList

end repeat

set {commandChoice1, commandChoice2} to commandList2

set item 1 of commandList4 to some item of commandList3

set item 2 of commandList4 to some item of commandList3

repeat until item 1 of commandList4 is not item 2 of commandList4

set item 2 of commandList4 to some item of commandList3

end repeat

set {commandChoice3, commandChoice4} to commandList4

delay x5

keystroke textToType & commandChoice1

keystroke return

delay x1

keystroke textToType & commandChoice2

keystroke return

delay x2

keystroke textToType & commandChoice3

keystroke return

delay x3

keystroke textToType & commandChoice4

keystroke return

delay x4

keystroke textToType & commandChoice5

keystroke return

end repeat

end tell



However, it does not work on that script.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

What is wrong with my AppleScript code?

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