On macOS,Install the input method has always been a nightmare,To open the system input method directory,We should drag input into this directory,The process also enter a password,Full manual bad enough,After we had to reboot the system to recognize,Not really a headache。
Later, we gradually realized the problem,Pocketed input method is also provided macOS standard installation package pkg。
Having said that,But in fact it seems that there is a way to automatically load input method,such asSogouHe did restart free,After installing only be able to identify without rebooting,Even the automatic installation package can also help you choose the right Sogou input method!
They do not know exactly how,I also have conducted my own research。
Profiles
In short,Everything is starting from the configuration file,First, I found the system configuration file to be read,This file determines which input method is currently active users,The selected input method which is:
1 |
~/Library/Preferences/com.apple.HIToolbox.plist |
In this plist.,have Appleanbledinputsourches Field,This side contains the current input method which is enabled,For example, Sogou input method、R0uter Input Tool、Pinyin system, etc.; AppleInputSourceHistory Field contains the user's history switch,It seems to be related and automatically switch; AppleSelectedInputSources Field indicates the current input method selected by the user,For example, you currently useR0uter Input ToolTyping,This is off the grid input method,If you switch to the English keyboard,This will become an English keyboard。
In theory,We edit this file,You can change the configuration of the system,But apparently,This profile is not real-time read,We edit the file,Restart preference management system and the terminal command Finder: killall cfprefsd && killall Finder
Good luck,He has the ok。
In fact ---,Generally you will not be lucky。
Obviously,Want insurance, then,Or restart the job。In short,This is already a progress,After all, does not require the user to manually add the complete restart input method, right?
System-level API
Then,Can there other more elegant options? I ended up in Carbon Framework found the answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
/* *=============================================================================== * Install/register an input source *=============================================================================== */ /* * TISRegisterInputSource() * * Summary: * Registers the new input source(s) in a file or bundle so that a * TISInputSourceRef can immediately be obtained for each of the new * input source(s). * * Discussion: * This allows an installer for an input method bundle or a keyboard * layout file or bundle to notify the system that these new input * sources should be registered. The system can then locate the * specified file or bundle and perform any necessary cache rebuilds * so that the installer can immediately call * TISCreateInputSourceList with appropriate properties (e.g. * BundleID or InputSourceID) in order to get TISInputSourceRefs for * one or more of the newly registered input sources. * * This can only be used to register the following: * * - Keyboard layout files or bundles in "/Library/Keyboard * Layouts/" or "~/Library/Keyboard Layouts/" (available to all * users or current user, respectively). Such keyboard layouts, once * enabled, are selectable. * * - Input method bundles in the new "/Library/Input Methods/" or * "~/Library/Input Methods/" directories (available to all users or * current user, respectively). * * Note: Input method bundles can include private non-selectable * keyboard layouts for use with * TISSetInputMethodKeyboardLayoutOverride. These are registered * automatically when the input method is registered, and do not * need to be separately registered. * * Security: Any code that calls TISRegisterInputSource is part of * an application or service that has already been validated in some * way (e.g. by the user). * * Parameters: * * location: * CFURLRef for the location of the input source(s), a file or * bundle. * * Result: * Error code: paramErr if location is invalid or the input * source(s) in the specified location cannot be registered; * otherwise noErr. * * Availability: * Mac OS X: in version 10.5 and later in Carbon.framework * CarbonLib: not available * Non-Carbon CFM: not available */ extern OSStatus TISRegisterInputSource(CFURLRef location) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER; |
For Swift is,Also calls painless,After we installed input method,Only need to input method app file passed to the function address,It can be registered in the system input method,This time it can make without having to reboot the system to recognize input method:
1 2 3 |
let url = URL(fileURLWithPath: "/Library/Input Methods/LogInputMac2.app") let err = TISRegisterInputSource(url as CFURL) guard err != paramErr else {return} |
Remember import Carbon
Then,I also want the system automatically shuts down the list to add my input method (ie activate input method):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* * TISEnableInputSource() * * Summary: * Enables the specified input source. * * Discussion: * TISEnableInputSource is mainly intended for input methods, or for * applications that supply their own input sources (e.g. * applications that provide keyboard layouts or palette input * methods, and keyboard input methods that provide their own * keyboard layouts and/or input modes). It makes the specified * input source available in UI for selection. * * For TISEnableInputSource to succeed, the input source must be * capable of being enabled (kTISPropertyInputSourceIsEnableCapable * must be true). Furthermore, if the input source is an input mode, * its parent must already be enabled for the mode to become enabled. * * Result: * Returns an error code: paramErr if the input source cannot be * enabled, else noErr. * * Availability: * Mac OS X: in version 10.5 and later in Carbon.framework * CarbonLib: not available * Non-Carbon CFM: not available */ extern OSStatus TISEnableInputSource(TISInputSourceRef inputSource) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER; |
This function is called it will not be so easy,The main difficulty is this TISInputSource No way to directly create,So to use another function to search,We have registered a fall in input grid system,The next step is to find pocketed input This input source in all of the available input methods,If the direct exhaust all available input sources,It will be very slow (API exact words),We need to add search conditions:
1 2 3 4 5 6 |
let conditions = NSMutableDictionary() conditions.setValue(“落格输入法id”, forKey: kTISPropertyBundleID as String) guard let array = TISCreateInputSourceList (conditions, true)?.takeRetainedValue() as? [TISInputSource] else { return } |
If there is no accident,We can get off the grid input method of the input source,There are two,One is the input method,A real model is used to input the user can enter (at both the user's perspective is the same,Off the grid because there is only one input method input mode - two in English mode is the only mode of different states)。
Then there is the added:
1 2 |
TISEnableInputSource(source) TISSelectInputSource(source) |
This time,Users can immediately switch in the upper right corner after installation to click off the grid input - good luck,You do not even need to switch。
discuss
This method looks very good,It seems from macOS 10.5 You can start with,But the real test to see only the latest macOS Mojave 10.14 It's effective,I'm here 10.13 The test is invalid,Whether to change the configuration or call the system API,All indifferent,I prefer my version of this particular system bug,Since when has the top right corner off the input method of the API call icon,However, the selection list is not pocketed input method,Even registration failed。
In short,for 10.14 New users,Installation pocketed experience input becomes excellent。
Original article written by LogStudio:R0uter's Blog » Pocketed input macOS 2 It is how to restart the free activation input method
Reproduced Please keep the source and description link:https://www.logcg.com/archives/3263.html