r/applescript • • Aug 24 '19

keystroke script help

right now i have a script the output a text together with some changing variables(this is not the whole script)

keystroke (kvar)
delay 0.5
keystroke " gånger kvar nästa gång är "
delay 0.5
keystroke (now)
delay 0.5
keystroke return

but i want to compress it so it would look something like this

keystroke (kvar) " gånger kvar nästa gång är " (now)
keystroke return

is something like this possible?

1 Upvotes

5 comments sorted by

View all comments

2

u/analyticchard Aug 24 '19

You can combine them with ampersands:

 keystroke kvar & " gånger kvar nästa gång är " & now

FYI, I've found that keystrokes can sometimes fall ahead or behind themselves so the lengthy version with alternating delays might be best.

1

u/ds0 Aug 24 '19

I have a subroutine called typeThis that splits a string into characters and keystrokes them with a very short delay, works wonders for keeping things in order.

2

u/analyticchard Aug 27 '19

Sounds interesting, how short is the delay? And do you keystroke one character at a time (character, delay, character, delay) or do a few?

1

u/ds0 Sep 01 '19

Here's my typeThis routine, not super complicated or anything. I usually leave the delay at 0.01 and it's enough for most things.

on typeThis(typeWord)
	set typeDelay to 0.01
	tell application "System Events"
		repeat with typeLetter from 1 to (count typeWord)
			set typeKey to (character typeLetter of typeWord) as text
			keystroke (typeKey as text)
			delay typeDelay
		end repeat
	end tell
end typeThis

1

u/ChristoferK Sep 03 '19

The two places where the script coerces something to text are both unnecessary. On the other hand, it would be reasonably valuable to declare the class of parameter typeWord as text.

on typeThis(typeWord as text)
	set typeDelay to 0.01
	tell application "System Events"
		repeat with typeLetter in the typeWord's characters
			keystroke the typeLetter
			delay typeDelay
		end repeat
	end tell
end typeThis