[MacOS] “かなキー2回押しで日本語変換”同等機能をAppleScriptで実装 Montereyでも動作

[M1 Mac, Monterey 12.6.3]

Monterey以降ではアルファベットからのかなキー2回押し日本語変換が使えないため、自製のAppleScriptをcmd + opt + zに割り当てています。

Vanturaではうまく動作しなかったのですが、Montereyでは一応動くのでホッとしています。

use AppleScript version "2.5" --  (10.12) or later
use framework "Foundation"
use scripting additions

property NSString : a reference to current application's NSString
property NSStringTransformLatinToHiragana : a reference to current application's NSStringTransformLatinToHiragana

selectString()

set latin to the clipboard

set hiragana to alphabetToHiragana(latin)

set the clipboard to hiragana

pasteString()

henkan()

on selectString()
    tell current application
        tell application "System Events"
            keystroke "ac" using {command down}
        end tell
    end tell
end selectString

on pasteString()
    tell current application
        tell application "System Events"
            keystroke "v" using {command down}
        end tell
    end tell
end pasteString

on alphabetToHiragana(aStr)
	set aString to NSString's stringWithString:aStr
	return (aString's stringByApplyingTransform:(NSStringTransformLatinToHiragana) |reverse|:false) as string
end alphabetToHiragana

on henkan()
    tell current application
        tell application "System Events"
            keystroke "a" using {command down}
            key code 104
            key code 104
        end tell
    end tell
end henkan