Module: AnkiConnect::Client::Graphical

Included in:
AnkiConnect::Client
Defined in:
lib/anki_connect/graphical.rb

Overview

Methods to interact with Anki's GUI windows and dialogs (card browser, review screen, editing interfaces).

Instance Method Summary collapse

Instance Method Details

#gui_add_cards(note = nil) ⇒ Integer

Opens Add Cards dialog with preset values. Multiple invocations close old window and reopen with new values.

Parameters:

  • note (Hash, nil) (defaults to: nil) —

    Optional note preset

Returns:

  • (Integer) —

    Note ID that would be created if user confirms



52
53
54
55
56
# File 'lib/anki_connect/graphical.rb', line 52

def gui_add_cards(note = nil)
  params = {}
  params[:note] = normalize_gui_note(note) unless note.nil?
  request(:guiAddCards, **params)
end

#gui_add_note_set_data(note, append: false) ⇒ Boolean, Hash

Sets fields/tags/deck/model in open Add Note dialog. Returns error if Add Note dialog not open. Deck/model always replace; fields/tags respect append flag.

Parameters:

  • note (Hash) —

    Note with optional deck_name, note_type_name, fields, tags, and media

  • append (Boolean) (defaults to: false) —

    If true, appends to fields/tags; otherwise replaces (default: false)

Returns:

  • (Boolean, Hash) —

    true on success, or an error hash when the dialog is closed



73
74
75
# File 'lib/anki_connect/graphical.rb', line 73

def gui_add_note_set_data(note, append: false)
  request(:guiAddNoteSetData, note: normalize_gui_note(note), append: append)
end

#gui_answer_card(ease) ⇒ Boolean

Answers the current card. Answer must be displayed before answering.

Parameters:

  • ease (Integer) —

    Answer button (1-4)

Returns:

  • (Boolean) —

    true on success, false otherwise



119
120
121
# File 'lib/anki_connect/graphical.rb', line 119

def gui_answer_card(ease)
  request(:guiAnswerCard, ease: ease)
end

#gui_browse(query = nil, reorder_cards: nil) ⇒ Array<Integer>

Opens Card Browser dialog and searches for query.

Parameters:

  • query (String, nil) (defaults to: nil) —

    Search query string; nil preserves the current browser search

  • reorder_cards (Hash, nil) (defaults to: nil) —

    Object with order and column_id

Returns:

  • (Array<Integer>) —

    Array of card IDs found



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/anki_connect/graphical.rb', line 19

def gui_browse(query = nil, reorder_cards: nil)
  params = {}
  params[:query] = query unless query.nil?
  if reorder_cards
    normalized = normalize_keys(reorder_cards, REORDER_CARD_KEYS, name: 'reorder_cards')
    missing_keys = REORDER_CARD_KEYS.values.reject { |key| normalized.key?(key) }
    raise ArgumentError, "missing reorder_cards keys: #{missing_keys.join(', ')}" unless missing_keys.empty?

    params[:reorderCards] = normalized
  end
  request(:guiBrowse, **params)
end

#gui_check_database ⇒ Boolean

Requests database check. Returns immediately without waiting for check to complete.

Returns:

  • (Boolean) —

    true (always)



176
177
178
# File 'lib/anki_connect/graphical.rb', line 176

def gui_check_database
  request(:guiCheckDatabase)
end

#gui_current_card ⇒ Hash

Gets information about current card in review.

Returns:

  • (Hash) —

    Current card information

Raises:

  • (APIError) —

    If review is not active



88
89
90
# File 'lib/anki_connect/graphical.rb', line 88

def gui_current_card
  request(:guiCurrentCard)
end

#gui_deck_browser ⇒ nil

Opens Deck Browser dialog.

Returns:

  • (nil)


141
142
143
# File 'lib/anki_connect/graphical.rb', line 141

def gui_deck_browser
  request(:guiDeckBrowser)
end

#gui_deck_overview(name) ⇒ Boolean

Opens Deck Overview dialog for a deck.

Parameters:

  • name (String) —

    Deck name

Returns:

  • (Boolean) —

    true on success, false otherwise



134
135
136
# File 'lib/anki_connect/graphical.rb', line 134

def gui_deck_overview(name)
  request(:guiDeckOverview, name: name)
end

#gui_deck_review(name) ⇒ Boolean

Starts review for a deck.

Parameters:

  • name (String) —

    Deck name

Returns:

  • (Boolean) —

    true on success, false otherwise



149
150
151
# File 'lib/anki_connect/graphical.rb', line 149

def gui_deck_review(name)
  request(:guiDeckReview, name: name)
end

#gui_edit_note(note_id) ⇒ nil

Opens Edit dialog for a note. Opens edit dialog with Preview, Browse, and navigation buttons.

Parameters:

  • note_id (Integer) —

    Note ID

Returns:

  • (nil)


63
64
65
# File 'lib/anki_connect/graphical.rb', line 63

def gui_edit_note(note_id)
  request(:guiEditNote, note: note_id)
end

#gui_exit_anki ⇒ nil

Schedules graceful Anki shutdown. Asynchronous - returns immediately without waiting for termination.

Returns:

  • (nil)


168
169
170
# File 'lib/anki_connect/graphical.rb', line 168

def gui_exit_anki
  request(:guiExitAnki)
end

#gui_import_file(path: nil) ⇒ nil

Opens Import dialog with optional file path. Opens file dialog if no path provided. Forward slashes required on Windows. Anki 2.1.52+ only.

Parameters:

  • path (String, nil) (defaults to: nil) —

    File path to import (optional)

Returns:

  • (nil)


158
159
160
161
162
# File 'lib/anki_connect/graphical.rb', line 158

def gui_import_file(path: nil)
  params = {}
  params[:path] = path if path
  request(:guiImportFile, **params)
end

#gui_play_audio ⇒ Boolean

Plays audio for current card side.

Returns:

  • (Boolean) —

    true on success, false otherwise



183
184
185
# File 'lib/anki_connect/graphical.rb', line 183

def gui_play_audio
  request(:guiPlayAudio)
end

#gui_review_active? ⇒ Boolean

Checks whether the review screen has an active card.

Returns:

  • (Boolean) —

    true when review is active



80
81
82
# File 'lib/anki_connect/graphical.rb', line 80

def gui_review_active?
  request(:guiReviewActive)
end

#gui_select_card(card_id) ⇒ Boolean

Selects a card in the open Card Browser.

Parameters:

  • card_id (Integer) —

    Card ID

Returns:

  • (Boolean) —

    true if browser is open, false otherwise



36
37
38
# File 'lib/anki_connect/graphical.rb', line 36

def gui_select_card(card_id)
  request(:guiSelectCard, card: card_id)
end

#gui_selected_notes ⇒ Array<Integer>

Gets selected notes from open Card Browser.

Returns:

  • (Array<Integer>) —

    Array of note IDs (empty if browser not open)



43
44
45
# File 'lib/anki_connect/graphical.rb', line 43

def gui_selected_notes
  request(:guiSelectedNotes)
end

#gui_show_answer ⇒ Boolean

Shows answer side of current card.

Returns:

  • (Boolean) —

    true if in review mode, false otherwise



110
111
112
# File 'lib/anki_connect/graphical.rb', line 110

def gui_show_answer
  request(:guiShowAnswer)
end

#gui_show_question ⇒ Boolean

Shows question side of current card.

Returns:

  • (Boolean) —

    true if in review mode, false otherwise



103
104
105
# File 'lib/anki_connect/graphical.rb', line 103

def gui_show_question
  request(:guiShowQuestion)
end

#gui_start_card_timer ⇒ Boolean

Starts/resets timer for current card. Useful for accurate time tracking when displaying cards via API.

Returns:

  • (Boolean) —

    false when review is not active or no card is available



96
97
98
# File 'lib/anki_connect/graphical.rb', line 96

def gui_start_card_timer
  request(:guiStartCardTimer)
end

#gui_undo ⇒ Boolean

Undoes last action/card.

Returns:

  • (Boolean) —

    true on success, false otherwise



126
127
128
# File 'lib/anki_connect/graphical.rb', line 126

def gui_undo
  request(:guiUndo)
end