Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppleScript not works in macOS 12 Monterey #15

Open
Yang-Xijie opened this issue Oct 30, 2021 · 8 comments
Open

AppleScript not works in macOS 12 Monterey #15

Yang-Xijie opened this issue Oct 30, 2021 · 8 comments

Comments

@Yang-Xijie
Copy link
Owner

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 #14 (comment)

@Yang-Xijie
Copy link
Owner Author

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
Copy link
Owner Author

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
Copy link
Owner Author

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
Copy link
Owner Author

Solved in 5f2b73d

@Yang-Xijie
Copy link
Owner Author

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

@Yang-Xijie Yang-Xijie reopened this Oct 30, 2021
@martinmts
Copy link

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
Copy link

martinmts commented Oct 30, 2021

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.

@zcbenz
Copy link

zcbenz commented Apr 21, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants