How do I programmatically change the note type of a note? - anki

I'm currently trying to develop an Anki addon that changes the note type of a card when in the card browser.
To accomplish this, I'm initially hooking into the editor shortcuts via aqt.gui_hooks.editor_did_init_shortcuts.append(), then adding a shortcut that sends a callback to another function, passing in aqt.editor.Editor into a function
the problem is I'm now having a hard time changing the note type of the card.
I've tried editor.note.note_type = mw.col.models.by_name(note_type), but it doesn't seem to change anything.
I've looked at other anki extensions, but it seems they change the note_type by modifying the notetype_chooser.selected_note_type_id field of aqt.addcards, which is not found in aqt.editor.Editor
here's the code I have so far:
def editor_switch_note_card_type(editor: aqt.editor.Editor, note_type: str):
# doesn't do anything
editor.note.note_type = mw.col.models.by_name(note_type)
# does something
editor.note['Front'] += 'hello world'
# not sure if this does anything
mw.col.update_note(editor.note)
# editor_init_shortcuts binds editor_switch_note_card_type to a shortcut key when editor is focused
aqt.gui_hooks.editor_did_init_shortcuts.append(editor_init_shortcuts)

Related

How would I get the command that a member inputed in discord.py

I am currently busy building a very modular discord bot and so I am attempting to create a command call that is built from a config file.
I want to be able to have a small amount of code and have the functions be dynamically called depending on what I put into the config file but now I am stuck with not being able to find what the actual command is that the member inputted.
STATIC_NAMES = ['rules', 'server']
#commands.command(aliases=STATIC_NAMES)
async def caller_(self, ctx):
await ctx.send(embed=self.embed_dict["the actual command that the person inputted"])
Essentially what I am asking is whether it is possible to get what the actual command was that called the function if you are using aliases for your calls.
Edit:
I know that I am editing this rather quickly but I seem to have found an alright workaround until I can find a better solution.
I am just taking the entire message, cutting out the prefix, splitting it in case there are other arguments passed and then capitalizing that.
await ctx.send(embed=self.embed_dict[ctx.message.content[len(PREFIX):].split(' ')[0].capitalize()])
Which is working for now.
Just use the Context.command.name attributes:
command_name = ctx.command.name

Keyboard shortcuts giving me errors in tkinter

I am trying to create a text editor with python 3 and tkinter. The text editor works great except for when I try to use my keyboard shortcuts. Whenever I use any of the shortcuts, I get an error that says this:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: newFile() takes 0 positional arguments but 1 was given
newFile() can be replaced with copySelected(), selectAll(), or whatever command I am trying to use. This only happens when I am trying to use the key bindings. It works just fine from the menu bar. The wierd thing is that when I am cutting, copying, or pasting I get the error they actually work in the app. Here is the key binding code:
textField.bind("<Command-n>", newFile)
textField.bind("<Command-N>", newFile)
textField.bind("<Command-o>", openFile)
textField.bind("<Command-O>", openFile)
textField.bind("<Command-s>", saveFile)
textField.bind("<Command-S>", saveFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-z>", undo)
textField.bind("<Command-Z>", undo)
textField.bind("<Command-Shift-z>", redo)
textField.bind("<Command-Shift-Z>", redo)
textField.bind("<Command-x>", cutSelected)
textField.bind("<Command-X>", cutSelected)
textField.bind("<Command-c>", copySelected)
textField.bind("<Command-C>", copySelected)
textField.bind("<Command-v>", paste)
textField.bind("<Command-V>", paste)
textField.bind("<Command-a>", selectAll)
textField.bind("<Command-A>", selectAll)
I am currently testing the code on Mac OS but I have already made the code os specific so that it will work on Windows and Linux as well. The Windows and Linux code is exactly the same other than the fact that Command is replaced with Control. The error occurs on all three of the platforms.
Any help is greatly appreciated. Thanks!
When you bind a key to the function, tkinter will automatically pass an object to the callback. This object represents the event that caused the callback to be called. It has information such as the widget that received the event, the x and y coordinate of the mouse, and other details unique to the event (mouse button, keyboard character, etc).
When you bind a function to an event, your function must be able to accept this parameter. For example:
def newFile(event):
...
Note that this is different than if you call the function via the command attribute of a widget. In that case no event object is passed. If you want to be able to call the function both via a binding and via a command attribute then you can make the parameter optional (and make sure that your function doesn't actually attempt to use it, since it may not be present):
def newFile(event=None):
...
TypeError: newFile() takes 0 positional arguments but 1 was given
I assume you forgot to add the event argument.
def saveFile(self, event):
# Code here
now you can call
textField.bind("<Command-S>", saveFile)
If this don't work please provide us your saveFile function/methode.
I'm guessing you are using classes and the callbacks for keyboard shortcuts is:
def saveFile():
# Code here
but your method should be:
def saveFile(self):
# Code here
as you always have to include the self reference.
If this does not work you may have to include more details about your code for us to pinpoint your problem.

I cannot send keystrokes to a selected input box

from pywinauto.application import Application
app = Application().Start(cmd_line=u'"path to program" ')
afx = app[u'Afx:01360000:0']
afx.Wait('ready')
afxtoolbar = afx[u'1']
toolbar_button = afxtoolbar.Button(3)
toolbar_button.Click()
window = app.Dialog
window.Wait('ready')
edit = window.Edit4
edit.Click()
app.typekeys ("Success")
So at this point, I've gotten the application to open, the correct window to pop up and also a mouse click on the box that I want to populate with a short string. I cannot for the life of me, figure out how to pass keyboard input to this field. I have read all the docs for PyWinAuto, and nothing is helping...
Basically all I need to do is figure out how to send a string, and then how to send the TAB key six times. I can then finish my program to automate this application.
I am also using Swapy64bit to help. The program uses a win32 backend. I'm using Python 3.6.
Am I not prefixing typekeys correctly? The PyWinAuto documentation leaves much to be desired.
First the correct name of the method is type_keys, but assume you use it correctly.
The reason might be losing focus at the edit control because type_keys tries to set focus automatically. The solution is:
app.type_keys("Success{TAB 6}", set_foreground=True)

corona sdk crashes when attempting to open a file using io.open

I've never used corona sdk before and decided to create a key logger as a test to see if I liked corona.
I'm able to check input and specify the file path but once my code reaches io.open the simulater freezes and crashes and the input is not added to the text file.
I've looked just about every where I can think of for an answer and found none.
I'm sure this isn't the most efficient code out there. I'm a newbie to any type of programming outside of the ROBLOX engine.
function WriteToFile(String)
local path = system.pathForFile("R#T.txt", system.DocumentsDirectory)
print("Path") -- Just to try and determine the origin of the crash
local File, ErrorString = io.open(path, "W")
print("File")
if not File then
print('File error: ' .. ErrorString)
else
print("else")
File:write(String)
io.close(File)
end
File = nil
end
function ReturnInput( Key )
if Key.phase == 'down' then
print(Key.keyName)
WriteToFile(tostring(Key.keyName))
end
end
Runtime:addEventListener('key', ReturnInput)
Sorry to tell you this, but unfortunately you can not create keyloggers with Corona SDK. You can not create anything that runs on the background.
E.g. you can't make a floater widget and when you press it, you get screenshot.

How to send "Ctrl + c" in Sikuli?

This feels like it should be pretty easy but I can't find documentation on how to do this:
I just want Sikuli to type Ctrl+C to copy text to the clipboard.
type(KEY_CTRL+'c') doesn't work and neither does type(KEY_CTRL,'c').
Any suggestions?
Try using type("c",KEY_CTRL) instead.
I wrote a simple script which types a line in notepad, double clicks it to mark it and then ctrl+x ctrl+v it into the document again. Works great.
openApp("notepad.exe")
find("textfield.png" )
type("Some text")
doubleClick("theText.png")
type("x", KEY_CTRL)
click("theTextField.png" )
type("v",KEY_CTRL)
The following works in 0.9 and newer versions of sikuli
type('x', KeyModifier.CTRL)
Key objects are defined for pretty much all the modifier keys and num pad keys. Anyways, it should look something like this
keyDown(Key.CTRL)
type('c')
keyUp(Key.CTRL)
The usage of type() and the possible key names are documented here:
http://doc.sikuli.org/region.html#Region.type
http://doc.sikuli.org/keys.html#key-constants
As others have mentioned, use the following:
type('c', Key.CTRL) # Copy command
One point worth mentioning - do not use upper-case characters, i.e.:
type('C', Key.CTRL) # Does not copy, avoid this
I haven't looked into the Sikuli source code, but my best guess is that it implicitly sends this as Shift+C, which results in a different command entirely.
type('x', Key.CTRL) also works.
Also, make sure that NUM_LOCK is off. If NUM_LOCK is on, it can make anything with KeyModifier.CTRL or KeyModifier.SHIFT misbehave.
You can try next code:
keyDown(Key.CTRL)
type("c")
keyUp(Key.CTRL)
I had a requirement to automate a flash content. The following code worked for me.
These were the following steps I ahd to perform as a part of the automation:
Enter Username and Password
Click on Login Button
Click on the button which will navigate to the application
The challenge I faced was to focus on the Username and password which had no placeholders . Hence the focusing was difficult. So I used the CTRL keys to do this .
Pattern appLogo = new Pattern("C:\\images\\appLogo.png");
StringSelection userNameText = new StringSelection("username");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(userNameText, null);//Copy the text into the memory
Screen s = new Screen();
s.find(appLogo);
s.click(appLogo);
s.type(Key.TAB);//I had to enter tab twice to focus on user name textbox
s.type(Key.TAB);
s.type("V",KeyModifier.CTRL);
StringSelection password = new StringSelection("password");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(password, null);
s.type(Key.TAB);//I had to enter tab twice to focus on user name textbox
s.type("V",KeyModifier.CTRL);
Pattern loginButton = new Pattern("C:\\images\\Login.png");
s.find(loginButton);
s.doubleClick(loginButton);
The scenario is like i need to press say key E in my keyboard after finishing the test how to add this in the script in Sikuli IDE.

Resources