Trying to make Google Chrome open a new tab.

Asked by Carl Johan Grevby

Hi!

I've been trying to make Google Chrome open a new tab by using CMD+T

switchApp("Google Chrome.app")
sleep(1)
type("t",Key.CMD)

But all it does is click one of the folders in my "Dock".

What am I doing wrong?

(I have been trying to google the problem but I can't find anything)

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Silverallis (silverallis) said :
#1

Try this

switchApp("Google Chrome.app")
sleep(3)
type("t", KEY_CTRL)

Revision history for this message
RaiMan (raimund-hocke) said :
#2

as mentioned by Silverallis:

your problem is the type (read: http://sikuli.org/docx/keys.html#key-modifiers about the 2nd parameter of type):

type("t", KEY_CMD)

one more thing:

this snippet gives more flexibility then swichApp() (read: http://sikuli.org/docx/globals.html#the-application-class)

ac = App("Google Chrome")
if ac.window(): # already open?
  ac.focus() # then just switch to it
else: # not yet open
 ac.open()
 while not ac.window(): wait(1)
# now the chrome window is frontmost

with this approach you can do different things, based on chrome already open or not.

Revision history for this message
Carl Johan Grevby (johangrevby) said :
#3

Thanks for the help, it works great.

Now however, I am trying to make it press enter. And I read in http://sikuli.org/docx/keys.html#special-keys that I'm supposed to write "KEY_ENTER" without the quotation marks.

But it returns the error message: NameError: name 'KEY_ENTER' is not defined

Am I even on the right track or should I study up on some code somewhere?

Revision history for this message
Carl Johan Grevby (johangrevby) said :
#4

What is the difference between "KEY.ESC" and "KEY_ESC"?

Revision history for this message
Carl Johan Grevby (johangrevby) said :
#5

Thanks Silverallis, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#6

You read the right doc, but you missed this at the beginning of
http://sikuli.org/docx/keys.html#special-keys

Usage: Key.CONSTANT (where CONSTANT is one of the following key names).

the constants like ENTER, ESC, ... are defined in a class called Key and the notation for a key to press is
Key.ENTER, Key.ESC, ...
when used in the first parameter of type() (meaning what to type)
Each key is a one character string and can be used this way (concatenation, mix in, ...)

The second parameter is reserved for some so called modifier keys, that can be pressed and hold during typing the content of the first parameter (ctrl, alt, shift, win, cmd (Mac)) and these keys have to be stated as
KEY_ALT, KEY_WIN, ...

I admit, that this is not very elegant and consistent from a users point of view, but currently it is as it is ;-)

Can you help with this problem?

Provide an answer of your own, or ask Carl Johan Grevby for more information if necessary.

To post a message you must log in.