Applescript - creating a simple script

Hi all,


I am new to using Applescript, and cannot figure out how to create what I think must be a super simple script.


What I want to happen is:

An online tool is already open in Google Chrome, I am in the first text field. I need the following shortcuts to be repeated. Command - Shift - "S", and then with a little delay Command - Enter. And then start again. Would be great to have a 'start' and 'end' command added too.


That's what I have so far:


activate application "Google Chrome"


tell application "System Events" to keystroke "s" using {command down, shift down}


delay (random number from 0.5 to 5)


tell application "System Events" to keystroke "enter" using {command down}


delay (random number from 0.5 to 5)


end


I only managed to get the first set of shortcuts right. After that a new window of Chrome opens.


I tried a bunch of forums, but could not find any solution. Can anyone help? Thanks!!




MacBook

Posted on Sep 10, 2019 1:10 AM

Reply

Similar questions

3 replies

Sep 10, 2019 6:02 AM in response to Ktoff

Rather than send "enter" send the equivalent key code as shown below. I don't have Chrome installed, so used TextEdit here. The following is the normal System Events template for controlling an application. Modify per your content.


tell application "TextEdit" to activate
tell application "System Events"
	tell application process "TextEdit"
		set frontmost to true
		key code 36 -- send a return key code
	end tell
end tell



Because you are attempting to manipulate an application via its graphical interface, you may need to use additional System Events : Dictionary : Processes Suite commands to abide by the current interface element hierarchies within the Chrome application.

Sep 10, 2019 10:48 AM in response to Ktoff

tell application "System Events" to keystroke "enter" using {command down}


Is incorrect.


See prior post for the correct way.


delay (random number from 0.5 to 5)


Why the randomness? Would only be useful in a game to defeat automation detection software in the game. The value should be the maximum time ever expected vs. the time you will get board of waiting.


R

Sep 10, 2019 11:26 AM in response to VikingOSX

As has been mentioned before, 'keystroke "enter"' will literally type the word 'enter' (albeit with the Command key down in your example).


As for the other points, to get the action to repeat, just wrap it in a repeat/end repeat block - you'll just need to define how the repeat should end (after x iterations; indefinitely; until some condition is met; etc.).


As for the 'start' and 'end' requests, it's not clear quite what you want there, but the basic premise is to just include commands outside of the repeat loop. You can even put them in a separate handler to make the code easier to follow:



global w


doStartStuff()


repeat 20 times

tell application "System Events"

keystroke "s" using {command down}

delay 1

key code 36

delay 1

end tell

end repeat

doEndStuff()


on doStartStuff()

-- any startup actions go here

tell application "Google Chrome"

set w to make new window

set URL of tab 1 of w to "http://www.apple.com/"

delay 2 -- give the window time to update

end tell

end doStartStuff


on doEndStuff()

-- cleanup actions go here

close w

end doEndStuff

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.

Applescript - creating a simple script

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