InputSourceSwitcher icon indicating copy to clipboard operation
InputSourceSwitcher copied to clipboard

AppleScript not works in macOS 12 Monterey

Open Yang-Xijie opened this issue 3 years ago • 8 comments

The AppleScript below does not work for Monterey. Is it a related issue?

	tell application "System Events"
		tell process "TextInputMenuAgent"
			click menu item layoutName 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

Originally posted by @martinmts in https://github.com/Yang-Xijie/InputSourceSwitcher/issues/14#issuecomment-955085182

Yang-Xijie avatar Oct 30 '21 02:10 Yang-Xijie

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

and get

System Events got an error: Can’t get menu item "ABC" of menu item of menu 1 of menu bar item 1 of menu bar 2 of process "TextInputMenuAgent".

Yang-Xijie avatar Oct 30 '21 03:10 Yang-Xijie

tell application "System Events"
	tell process "TextInputMenuAgent"
		--get the name of menu item of menu 1 of menu bar 2
		click menu bar item 1 of menu bar 2
		click menu item "Japanese" of menu 1 of menu bar item 1 of menu bar 2
	end tell
end tell

It will be OK with a slow speed... and sometimes fails.

Yang-Xijie avatar Oct 30 '21 04:10 Yang-Xijie

tell application "System Events"
	tell process "TextInputMenuAgent"
		--get the name of menu item of menu 1 of menu bar 2
		click menu bar item 1 of menu bar 2
		click menu item "Japanese" of menu 1 of menu bar item 1 of menu bar 2
	end tell
end tell

It will be OK with a slow speed... and sometimes fails.

ckeck https://apple.stackexchange.com/questions/423354/applescript-cant-get-rid-of-delay-after-click

Yang-Xijie avatar Oct 30 '21 04:10 Yang-Xijie

Solved in 5f2b73d7c315f5900da1b76d04b8b3e9808d6716

Yang-Xijie avatar Oct 30 '21 06:10 Yang-Xijie

v1.3 not really works, not reliable...

Yang-Xijie avatar Oct 30 '21 11:10 Yang-Xijie

I've tried this:

my changeKeyboardLayoutTo("U.S.")

to changeKeyboardLayoutTo(layoutName)
	ignoring application responses
		tell application "System Events" to ¬
			click menu bar item 1 of menu bar 2 of ¬
				application process "TextInputMenuAgent"
	end ignoring
	
	delay 0.1
	do shell script "killall 'System Events'"
	delay 0.2
	
	tell application "System Events"
		launch
		click menu item layoutName of menu 1 of ¬
			menu bar item 1 of menu bar 2 of ¬
			application process "TextInputMenuAgent"
	end tell
end changeKeyboardLayoutTo

It is essentially the same with you had in one of the post above. It does work most of the time. But it is not preferred. It may interfere with what I might be doing. For example, if Alfred is activated, running this AppleScript will cause Alfred to be deactivated.

I've also tried below (found in macos - Change OSX keyboard layout("input source") programmatically via terminal or AppleScript? - Stack Overflow)

#include <Carbon/Carbon.h>

int main (int argc, const char * argv[]) {
    NSArray* sources = CFBridgingRelease(TISCreateInputSourceList((__bridge CFDictionaryRef)@{ (__bridge NSString*)kTISPropertyInputSourceID : @"com.apple.keylayout.French" }, FALSE));
    TISInputSourceRef source = (__bridge TISInputSourceRef)sources[0];
    OSStatus status = TISSelectInputSource(source);
    if (status != noErr)
        return -1;

    return 0;
}

The problem is that it does not work for all keyboards. For example, it does not work for rime/squirrel, even I have put im.rime.inputmethod.Squirrel there. Also, some keyboards do not have Bundle ID in the plist file.

martinmts avatar Oct 30 '21 14:10 martinmts

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

and get

System Events got an error: Can’t get menu item "ABC" of menu item of menu 1 of menu bar item 1 of menu bar 2 of process "TextInputMenuAgent".

If I run this in ScriptDebugger:

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

I can get the following Result.

menu item "U.S." of menu 1 of menu bar item 1 of menu bar 2 of application process "TextInputMenuAgent"

If my understanding is correct, it indicates that the AppleScript is able to see U.S., but the click has no effect.

martinmts avatar Oct 30 '21 14:10 martinmts

Hi guys, I just found this project which works incredibly well with CJK even on macOS 12, the trick seems to be pressing "select the previous input source shortcut" after calling TISSelectInputSource. https://github.com/nuridol/SwitchIM

zcbenz avatar Apr 21 '22 00:04 zcbenz