Skip to content

Instantly share code, notes, and snippets.

@noborus
Last active November 6, 2021 11:48
Show Gist options
  • Save noborus/7541419 to your computer and use it in GitHub Desktop.
Save noborus/7541419 to your computer and use it in GitHub Desktop.
ibus_toggle のRuby版(iBus 1.4以下対応) ruby-dbusが必要($ gem install ruby-dbus)
#! /usr/bin/ruby
# -*- coding: utf-8 -*-
require 'dbus'
IBUS_SERVICE="org.freedesktop.IBus"
IBUS_PATH_IBUS="/org/freedesktop/IBus"
IBUS_INTERFACE_IBUS="org.freedesktop.IBus"
IBUS_INTERFACE_INPUTCONTEXT="org.freedesktop.IBus.InputContext"
def get_address
Dir.glob(ENV['HOME'] + "/.config/ibus/bus/*").each {|file|
File.open(file).each_line {|line|
/IBUS_ADDRESS=/ =~ line
next if !$'
return $'.chop
}
}
end
ibus_bus = DBus::RemoteBus.new(get_address())
ibus_service = ibus_bus.service(IBUS_SERVICE)
ibus = ibus_service.object(IBUS_PATH_IBUS)
ibus.introspect
ibus.default_iface = ibus[IBUS_INTERFACE_IBUS]
# /org/freedesktop/IBus/InputContext_*
ic_name = ibus.CurrentInputContext[0]
input_context = ibus_service.object(ic_name)
input_context.introspect
input_context.default_iface = IBUS_INTERFACE_INPUTCONTEXT
if input_context.IsEnabled[0]
input_context.Disable
else
input_context.Enable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment