Yang-Xijie / InputSourceSwitcher

A menu bar app to swiftly change input sources using shortcuts on macOS Big Sur. [Swift+SwiftUI] NO LONGER MAINTAIN
MIT License
36 stars 6 forks source link

NOTICE

I have stopped maintaining this project because Applescript not works as before in macOS 12.

macOS 11, Big Sur

On macOS 11, Applescript below worked well with little latency:

tell application "System Events"
    tell process "TextInputMenuAgent"
        click menu item "\(inputSourceName)" of menu 1 of menu bar item 1 of menu bar 2
        click menu bar item 1 of menu bar 2
    end tell
end tell

The corresponding release and commit are:

https://github.com/Yang-Xijie/InputSourceSwitcher/releases/tag/v1.2

d5f309e678f8e97d83da95e6facc5b4eada6b501

macOS 12, Monterey

When macOS 12 came, I tried to update this project. However, it seems that click in Applescript not works as expected. I made several commits on Oct 30, 2021 using a new Applescript. However, I should say it just works sometimes, not always realiable.

ignoring application responses
    tell application "System Events"
        click menu bar item 1 of menu bar 2 of application process "TextInputMenuAgent" of application "System Events"
    end tell
end ignoring

delay 0.1
do shell script "killall 'System Events'"
delay 0.1

tell application "System Events"
    launch
    click menu item "\(inputSourceName)" of menu 1 of menu bar item 1 of menu bar 2 of application process "TextInputMenuAgent" of application "System Events"
end tell

Corresponding release and commit are:

https://github.com/Yang-Xijie/InputSourceSwitcher/releases/tag/v1.3

5f2b73d7c315f5900da1b76d04b8b3e9808d6716

Source Switcher

A menu bar app to change input sources swiftly using shortcuts on macOS Big Sur and later.

View screenshots

Introduction

This app uses Applescript to manipulate UI operation which is better than using Carbon (a framework most of whose APIs are deprecated) in other tools to change input sources.

Installation / Usage

Download the release version and drop Source Switcher.app into your Applications folder. (may not be the latest version)

Or clone this repo and build it on your Mac.

Notice: You should go to System Preferences -> Security & Privacy -> Privacy -> Accessiblity to make Source Switcher able to execute Applescript on your Mac.

Then go to System Preferences -> Security & Privacy -> Privacy -> Automation to select Source Switcher.app and Tick the box of System Envent.app.

You'd better perform this operation before you open this app for the first time. If no input source appears, just lick Reset to get your current input sources.

Notice: If you get Source Switcher.app can't be opened because Apple cannot check it for malicious software, please open your Applications folder, find Source Switcher.app and right click -> Open to open the app.

Background

Search "input source mac" on GitHub and there are plenty of repositories such as hnakamur / inputsource and minoki / InputSourceSelector. They are command line softwares, and you can easily use Alfred to add shortcuts to switch input sources conveniently. However, they have a common problem: when it comes to CJK such as Japanese, they don't work. (check InputSourceSelector issues #2 (Japanese) and stackoverflow questions #22885767 (English) to get more details)

After some analysis, I find that they all use Carbon Core, most of whose APIs are deprecated after macOS 10.8, and use the input source id (eg. com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese) to identify different input sources.

laishulu / macism says it solves the problem. I'm not sure whether it works: it uses Carbon APIs and just stops for some time to deal with that problem...

Then how to solve it? The fact is, if we uses some mature exposed APIs provided by the system, issues not appear! To be concrete, we click the control center in the menu bar or switch input sources in System Preferences instead of calling macOS APIs. They are kind of UI things.

So one way is what hatashiro / kawa uses, which really solves the problem by using a method in System Preferences -> Keyboard -> Shortcuts -> Input Sources -> Select the previous input source. But issue still exists: if you change input sources quickly, you will find kawa doesn't work. This is because Select the previous input source has different functions when triggering the shortcut (⌃␣ if you haven't changed it) at a different speed. Kawa will work (as far as I consider) if change Select the previous input source to Select next source in input menu (see kawa pull #21). By the way, this project also uses Carbon APIs and this repo closes issues...

Innovatively, I use a completely UI way to solve all these problems. Referring to Geoff Taylor | Scripting the menu bar in macOS Big Sur, I use UI Browser to find how to use Applesript to express the Input Source Panel in the menu bar (Control Center). Then refer to Chris J. | How to launch an AppleScript from AppKit on Catalina with Swift to use Applescript in macOS app (which has a drawback that sandbox is disabled). After getting the prototype, I design the UI of this app, make this app a menu bar app, add shortcuts to it and better its code logic to avoid some potential bugs.

Referenced Open Source Projects

menubarpopoverswiftui2 - LICENSE

KeyboardShortcuts - LICENSE

About

Discuss about this app in the Discussion part. Check current desired features in Projects -> TODO.

Any issue or pull request are welcome!

If you find this tiny app improving your efficency, feel free to buy me a cup of coffee using WeChat Pay or Alipay.