How do I open things inside an app (Anki)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
boop47
Posts: 1
Joined: 30 Jul 2021, 03:48

How do I open things inside an app (Anki)

30 Jul 2021, 04:17

I do not have a lot of knowledge about how to code with Autohotkey and I have thus far only managed to figure out how to open and activate an app on windows. But I want to know also how to open/activate things inside the app (if it is possible).
The specific objective I struggle with is to make a code that opens the 'Add' in Anki (for adding new cards). I want to make the experience of making new cards more easy and convenient whilst I read materials on my computer.

My current code right now is a code that opens Anki:

Code: Select all

!^v::
	Run "C:\Program Files\Anki\anki.exe"
And a code that activate the window so I can add a new card

Code: Select all

^!a::
if WinExist("ahk_exe anki.exe")
	WinActivate
I would appreciate it if someone can give or explain how to make a code that can open the 'Add' in Anki with a shortcut, and if it is possible, also a shortcut to open the 'Choose deck' inside the 'Add' box when adding new cards.
User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: How do I open things inside an app (Anki)

30 Jul 2021, 07:44

Code: Select all

#IfWinActive Add ahk_class Qt5QWindowIcon ahk_exe anki.exe
F4::Send ^d ; Choose deck
#IfWinActive ahk_exe anki.exe
F3::Send a  ; Add a new card
#IfWinActive
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: How do I open things inside an app (Anki)

30 Jul 2021, 08:55

Hey @boop47, I too use Anki and I have this code (and a few more bits and pieces). Let me get onto my Surface and I'll post them here. Give me a few minutes.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: How do I open things inside an app (Anki)

30 Jul 2021, 09:35

Add new cards

Text card

Code: Select all


;-------------------------------------------------------------------------
;On RM
AnkiActions_AddSelected()
{
	;Add selected text as a new text card
	SetTitleMatchMode, 2 ; match anywhere in the title
	
	Send, ^c
	
;Activate anki
	ifWinExist, Add ahk_class Qt5QWindowIcon
	{
		WinActivate
		Send, {esc}
	}
	ifWinExist, - Anki ahk_class Qt5QWindowIcon
	{
		WinActivate
		Sleep, 300
		Send, a
  ;~ Sleep, 500
	}
	
;Paste
	Sleep, 500
	If WinActive("Add") Then
	{
		
		Send, ^v
		
	}
	
	else
	{
		
	
		ToolTip, The add window is not active
		Sleep 1500
		ToolTip
		
	}
	
	
	Return 	
	
}

;-------------------------------------------------------------------------


Image Occlusion Enhanced card

Note: This requires the Image Occlussion Enhanced for Anki 2.1 Alpha addon. It also requires Snipping Tool to be running so you can select the image to create a card from.

Code: Select all


;-------------------------------------------------------------------------
AnkiActions_AddScreenshot()
{
	
	Msgbox Make sure snipping tool is running...	
	;-------------------------------------------------------------------------

	if (strComputer = "Citrix")
	{
	
		ToolTip, This isn't set up to work in Citrix
		Sleep 1500
		ToolTip
		Exit
	}
	
	clipboard := "" ; Empty the clipboard
	
	;-------------------------------------------------------------------------
	;~ Get the active window
	WinGetTitle, strActiveWindow, A
	
	;-------------------------------------------------------------------------
	;~ Grab the mouse co-ordinates to be able to put the cursor back at the top of the window in questions
	
	;~ Move cursor to top left of active window
	;~ https://autohotkey.com/docs/commands/CoordMode.htm
	CoordMode, mouse, Window
	MouseMove, 0, 42, 0
	CoordMode, mouse, Screen	;Screen: Coordinates are relative to the desktop (entire screen).
	
	;17/02/2021 - Currently not functional.
	;CursorCoordinates_Get(xpos, ypos)
	
	;~ MsgBox, The cursor is at X%xpos% Y%ypos% ;and this information has been sent to the clipboard.
	
	;~ Send, #+s
	
	;-------------------------------------------------------------------------
	;~ Run snipping tool	;~ 2021-01-29
	
	WinActivate, Snipping Tool
	WinWaitActive, Snipping Tool
	WinWait, Snipping Tool, , 9
	if ErrorLevel
	{
		MsgBox, WinWait timed out.
		Exit
		return
	}
	else
	{
		Send, ^n
	}
	
	
	;-------------------------------------------------------------------------
	CoordMode, mouse, Screen
	MouseMove, %xpos%, %ypos%, 0
	
	Sleep, 2000
	
	KeyWait, LButton, D
	KeyWait, LButton, U
	WinWaitActive, Snipping Tool
	
	SetTitleMatchMode, 2 ; match anywhere in the title
	;~ https://autohotkey.com/board/topic/62399-window-title-matching-based-on-both-wintitle-window-cl/
	ifWinExist, Add ahk_class Qt5QWindowIcon 
	{
		WinActivate	;~ open anki
	
		ToolTip, Anki activated
		Sleep 1500
		ToolTip
		
	}
	else ifWinExist, - Anki ahk_class Qt5QWindowIcon 
	{
		WinActivate	
		Sleep 100
		Send, a		;~ open new note dialog
	}
	
	;~ open image occulation dialog
	Sleep 100
	Send, ^+o
	
	WinWaitActive, Image Occlusion Enhanced - Add Mode
	
	ifWinExist, Image Occlusion Enhanced - Add Mode ahk_class Qt5QWindowIcon 
	{
		WinActivate
		
	;~ Send, f - centres window and zooms to fit
	}
	
	
	;~ at this point you've drawn your rectangles and added any tags and words you want
	
	;-------------------------------------------------------------------------
	;~ Close the 'add' window after closing the 'add image occlude window'
	
	WinWaitClose, Image Occlusion Enhanced - Add Mode
	ifWinExist, Add ahk_class Qt5QWindowIcon 
	{
		WinActivate
		Send, {esc}
	}
	
	ifWinExist, - Anki ahk_class Qt5QWindowIcon 
	{
		WinMinimize
	}
	
	IfWinExist, Snipping Tool
	{
		WinMinimize
	}
	
	Return
	
}

;-------------------------------------------------------------

The next code will allow you to fill in the header and frequency fields on an image occlusion card, before navigating to the Tags field.

Code: Select all


;-------------------------------------------------------------
;Anki code to fill fields.

AnkiActions_FillFields()
{
	SetTitleMatchMode, 2
	
	If WinActive("ahk_class Qt5QWindowIcon") And WinActive("Image Occlusion Enhanced - Add Mode")
	{		
		WinActivate    ;~ open anki
		
		;~ Create gui to input header text
		
		;submeg edit 22/07/2021
		global Header := ""
		global Frequency := 0
		
		Gui, AnkiGUI:New, +AlwaysOnTop +ToolWindow +Resize, Complete fields:	;+Resize to pull the GUI edges
		Gui, Add, Text, x10, Header text:
		Gui, Add, Edit, x+20 vHeader ym
		Gui, Add, Text, x10, Frequency text:
		Gui, Add, Edit, x+5 vFrequency
		
		Gui, Add, Button, w200 x10 y+30 gSendFields, Add  ; The label Add2 (if it exists) will be run when the button is pressed.
		Gui, AnkiGUI:Show,, Add field values:
		
		return  ; End of auto-execute section. The script is idle until the user does something.
		
		SendFields:
		
		Gui,AnkiGUI:Submit,NoHide
		Gui AnkiGUI:Destroy	;depending on wanting to keep it open or not.
		
		msgbox % "Header entered: " Header
		msgbox % "Frequency entered: " Frequency
		
		Return
		
		;-------------------------------------------------------------------------
		;~ Wait for the add window to become active again
		WinWaitActive, Image Occlusion Enhanced - Add Mode
		;~ Sleep, 700
		
		;-------------------------------------------------------------------------
		;~ Go to the next tab of the add window
		IfWinExist, Image Occlusion Enhanced - Add Mode
		{
			
			WinActivate
			Send, {LControl down}{TAB down}
			Sleep, 5
			Send, {TAB up}{LControl up}
			Sleep, 5 
			
		}
			
		;-------------------------------------------------------------------------
		;~ Get to the header field of the card
		Loop 6
		{
			Send +{tab}
			Sleep, 50
		}
		
	        ;-------------------------------------------------------------------------
		;~ Add the header text to the card
		Send, % Frequency . "-" . Header
		
		;-------------------------------------------------------------------------
        
		;~ Go to the tags section
		Send, ^+t
		
		return
		
	}
	
}
return

;-------------------------------------------------------------

____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: How do I open things inside an app (Anki)

30 Jul 2021, 09:47

The following code two GUIs that appear in the bottom right and left of your screen. This is perfect for if you have a tablet device and want to study your cards while away from your desk.

The GUIs look like this:

Image

The buttons with the Q3 / Q4 are "Quick" keys; essentially pressing space and then the corresponding number.

Code: Select all


;Anki GUI for touch screens
;-------------------------------------------------------------------------
;2021-06-12, Simon's code
;https://autohotkey.com/board/topic/65319-using-the-same-ifwinactive-for-multiple-windows/

;================================================
;at the start of the script (or maybe here), define the variable below
;~ global intAnkiButtonPressCount := 0
;================================================
#SingleInstance, Force
GuiShowFlag := False

EnabledIcon = %A_ScriptDir%\AnkiIcon.png

;Set the icon image
Menu,Tray,Icon, %EnabledIcon%

F1 & F12::

If WinActive ("ahk_class Qt5QWindowIcon")
{
	
	SetTitleMatchMode, 2
	if winactive("- Anki") Then
	{
		If GuiShowFlag = True 
		{
			
			gui, GuiLeft:hide
			gui, GuiRight:hide
			
			GuiShowFlag := False
			
		}
		else
		{
			
			GuiShowFlag := True
			
			;-------------------------------------------------------------------------
			
			;Find the co-ordinates of the active window
			WinGetPos, X, Y, Width, Height, A  ; "A" to get the active window's pos.
			
			;-------------------------------------------------------------------------
			
			;Define gui specs - Change these as required
			intGuiWidth := 75
			intButtonHeight := intGuiWidth - 10
			intNumberOfButtons := 8
			intGuiHeight := (intButtonHeight + 5)*intNumberOfButtons +5
			intOffsetFromBottom := 150
			
			
			;Set the location of Gui 1
			intGui1Height := intGuiHeight + 5
			intGui1y := Y + Height - intGui1Height - intOffsetFromBottom - 150
			intGui1Width := intGuiWidth
			intGui1x := X + 10   ;+ Width - intGui1Width
			
			;Set the location of Gui 2
			intGui2Height := intGuiHeight + 5
			intGui2y := Y + Height - intGui2Height - intOffsetFromBottom - 150
			intGui2Width := intGuiWidth
			intGui2x := X + Width - intGui2Width - 55
			
			
			;Define button dimensions
			intButtonWidth := intGuiWidth - 10
			intButtonHeight := intGuiWidth - 10
			
			;-------------------------------------------------------------------------
			
			;Define transparency level
			intTransparency := 150
			
			;-------------------------------------------------------------------------
			
			;Gui 1 setup
			
			;https://autohotkey.com/board/topic/57707-noactivate-but-clickable/
			Gui, 1: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x08000000	
			Gui, 1:Color, FFFFFF	;white
			
			;Gui, 1:+AlwaysOnTop
			;Gui, 1:+ToolWindow -Caption 
			
			Gui, 1:Add, Button, x5 y5 w%intButtonWidth% h%intButtonHeight% gFour_AnswerButton_Anki,4
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gThree_AnswerButton_Anki,3
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gTwo_AnswerButton_Anki,2
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gOne_AnswerButton_Anki, 1
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gUndo_Anki,Undo
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gSuspendCard_Anki, Suspend Card
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gBury_Anki, Bury
			Gui, 1:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gResetProgress_Anki, Reset Progress
			
			Gui, 1:Show,x%intGui1x% y%intGui1y% w%intGui1Width% h%intGui1Height% NoActivate, GuiLeft
			WinSet, Transparent, %intTransparency%, GuiLeft
			
			;WinSet, AlwaysOnTop, On, Mini Tut 19
			;WinSet, Transcolor, 000111, Mini Tut 19
			
			
			;-------------------------------------------------------------------------
			
			;Gui 2 setup
			;https://autohotkey.com/board/topic/57707-noactivate-but-clickable/
			Gui, 2: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x08000000	
			Gui, 2:Color, FFFFFF	;white
			
			;Gui, 2:+AlwaysOnTop
			;Gui, 2:+ToolWindow -Caption 
			
			Gui, 2:Add, Button, x5 y5 w%intButtonWidth% h%intButtonHeight% gQuickFour_AnswerButton_Anki,Q4
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gQuickThree_AnswerButton_Anki,Q3
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gQuickTwo_AnswerButton_Anki,Q2
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gBlueFlag_Toggle_Anki, Blue
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gChangeDeck_Anki, Change Deck
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gMegaLibraryAHK, MegaLibary GUI
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gReload_Anki, Reload
			Gui, 2:Add, Button, y+5+%intButtonHeight% w%intButtonWidth% h%intButtonHeight% gTaskbar_Toggle_Anki, Show Taskbar
			
			
			;Gui, Show, NoActivate   ; Don't deactivate current window
			;If "NoActivate" is to work properly, XMouse needs to be off. 
			
			Gui, 2:Show,x%intGui2x% y%intGui2y% w%intGui2Width% h%intGui2Height% NoActivate, GuiRight
			WinSet, Transparent, %intTransparency%, GuiRight
			
			;-------------------------------------------------------------------------
			return
			
		}
	}	
	
	else	;If the active window is not Anki
	{
	
		ToolTip, %A_ThisHotkey% not relevant for the current window
		Sleep 1500
		ToolTip
		
	}
	
	
}
return
	
#IfWinActive
^#a::
{
	WinGetClass, class, A
	MsgBox '%A_ThisHotkey%' is not active for this program (%class%)
	return

}
return

;-------------------------------------------------------------

;-------------------------------------------------------------
;=============================================================
;ANKI GUI FUNCTIONS
;=============================================================

;-------------------------------------------------------------

QuickTwo_AnswerButton_Anki()
{
	
	Send, {space}
	Sleep, 600
	Send, 2
	;~ controlsend,, 2
	intAnkiButtonPressCount:=0
	;fUpdateCSV("'" . A_ThisHotkey . " - Send Space then 2 in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

QuickThree_AnswerButton_Anki()
{
	Send, {space}
	Sleep, 600
	Send, 3
	;~ controlsend,, 3
	intAnkiButtonPressCount:=0
	;fUpdateCSV("'" . A_ThisHotkey . " - Send Space then 3 in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

QuickFour_AnswerButton_Anki()
{
	Send, {space}
	Sleep, 600
	Send, 4
	;~ controlsend,, 4
	intAnkiButtonPressCount:=0
	;fUpdateCSV("'" . A_ThisHotkey . " - Send Space then 4 in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Bury_Anki()
{
	
	Send, -
	intAnkiButtonPressCount:=0
	;fUpdateCSV("'" . A_ThisHotkey, " - Send '-' in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Undo_Anki()	;2021-06-13
{
	
	Send, ^z
	intAnkiButtonPressCount:=0
	;fUpdateCSV("'" . A_ThisHotkey, " - 'Undo' in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

One_AnswerButton_Anki()
{
	if (intAnkiButtonPressCount < 1) Then
	{
		intAnkiButtonPressCount++
		Send, {space}
	}
	else
	{
		Send, 1
		intAnkiButtonPressCount:=0
	}
	;fUpdateCSV("'" . A_ThisHotkey . " - Send 1 in Anki'")	;2021-06-11
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Two_AnswerButton_Anki()
{

	if (intAnkiButtonPressCount < 1) Then
	{
		intAnkiButtonPressCount++
		Send, {space}
	}
	else
	{
		Send, 2
		intAnkiButtonPressCount:=0
	}

}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Three_AnswerButton_Anki()
{
	if (intAnkiButtonPressCount < 1) Then
	{
		intAnkiButtonPressCount++
		Send, {space}
	}
	else
	{
		Send, 3
		intAnkiButtonPressCount:=0
	}

}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Four_AnswerButton_Anki()
{
	if (intAnkiButtonPressCount < 1) Then
	{
		intAnkiButtonPressCount++
		Send, {space}
	}
	else
	{
		Send, 3
		intAnkiButtonPressCount:=0
	}

}

;-------------------------------------------------------------

;-------------------------------------------------------------

BlueFlag_Toggle_Anki()
{
	Send, ^4
}

;-------------------------------------------------------------

;-------------------------------------------------------------

Reload_Anki()
{
	Reload
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

ResetProgress_Anki()
{
	;browse button
	Send, b
	
	;wait for browse window
	SetTitleMatchMode, 2
	WinWaitActive, Browse (
	
	;press reset ?^!r
	Send, ^!r
	
	;~ WAit for the reschedule window
	SetTitleMatchMode, 2
	WinWaitActive, Reschedule
	
	;Press ok
	SetTitleMatchMode, 2
	If WinActive("Reschedule") Then
	{
		Send, {Enter}
	}
	
	;Wait for browse window
	SetTitleMatchMode, 2
	WinWaitActive, Browse (
	
	;press escape
	Send, {esc}
	
	intAnkiButtonPressCount:=0
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

Taskbar_Toggle_Anki()
{
	Send, #t
	
}
Return 

;-------------------------------------------------------------

;-------------------------------------------------------------
MegaLibraryAHK()
{	
	
	Send, {F13}

}
Return

;-------------------------------------------------------------

;-------------------------------------------------------------

ChangeDeck_Anki()
{
	;browse button
	Send, b
	
	;wait for browse window
	SetTitleMatchMode, 2
	WinWait, Browse (, , 3
	if ErrorLevel
	{
		MsgBox, WinWait timed out.
		return
	}
	
	;Change deck keyboard shortcut
	Send, ^d
	
	;~ WAit for the deck change window
	SetTitleMatchMode, 2
	WinWait, Change Deck, , 4
	if ErrorLevel
	{
		MsgBox, WinWait timed out.
		return
	}
	
	;Wait for deck change window to close
	SetTitleMatchMode, 2
	WinWaitClose, Change Deck
	
	ifWinExist, Browse (
	{
		Send, {esc}
	}
	
	intAnkiButtonPressCount:=0
	
}

Return 

;-------------------------------------------------------------

;-------------------------------------------------------------

SuspendCard_Anki()
{
	Send, {@}
	intAnkiButtonPressCount:=0
	
}

Return 

;-------------------------------------------------------------

____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], Gowynda, jaka1 and 165 guests