Created
December 15, 2023 09:56
-
-
Save oO0oO0oO0o0o00/466703aba3cfcb13614d1ef848ebcea4 to your computer and use it in GitHub Desktop.
Kittenal script of Hammerspoon on MacOS for IME fixes and screen sleeping optimization. Uploaded as backup. Not general purpose.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "hs.caffeinate" | |
require "hs.hotkey" | |
-- Exchange windows on these 2 out of 3 monitors if they are moved to the other. | |
internal_screen_id = "37D8832A-2D66-02CA-B9F7-8F30A301B230" | |
right_screen_id = "BFF6E135-AF30-4853-A5EF-EE64D279FA06" | |
apps_grouping = { | |
[internal_screen_id] = {right_screen_id, { | |
"飞书" | |
}}, | |
[right_screen_id] = {internal_screen_id, { | |
"Xcode", "Google Chrome", "Warp" | |
}} | |
} | |
-- For IME symbol fixing | |
single_quote_flag = false | |
double_quote_flag = false | |
cjkv_ime_name = "搜狗拼音" | |
function main() | |
-- Allow screens to turn off if explicitly locked with Cmd + L. | |
hs.caffeinate.set("displayIdle", true, false) | |
hs.hotkey.bind({"cmd"}, "l", function() | |
print("allow screen off & lock screen") | |
hs.caffeinate.set("displayIdle", false, false) | |
hs.caffeinate.lockScreen() | |
end) | |
-- Keep screens on when locked, | |
-- if the machine has any external power source. | |
caffeinateWatcher = hs.caffeinate.watcher.new(function(eventType) | |
if eventType == hs.caffeinate.watcher.screensDidUnlock | |
or eventType == hs.caffeinate.watcher.screensDidWake then | |
hs.caffeinate.set("displayIdle", true, false) | |
print("disallow screen off on screen awake") | |
correct_windows_locations() | |
end | |
end) | |
caffeinateWatcher:start() | |
-- Workaround IME failing to enter Chinese symbols | |
-- if switched programmatically. | |
fix_ime_symbols() | |
end | |
-------- Methods -------- | |
function correct_windows_locations() | |
for i, app in ipairs(hs.application.runningApplications()) do | |
local mainWindow = app:mainWindow() | |
if mainWindow ~= nil and mainWindow:isFullScreen() then | |
local screen = mainWindow:screen() | |
local screen_id = screen:getUUID() | |
for target_screen_id, package in pairs(apps_grouping) do | |
local wrong_screen_id, apps = table.unpack(package) | |
if wrong_screen_id == screen_id and table.contains(apps, app:name()) then | |
target_screen = hs.screen.find(target_screen_id) | |
if target_screen ~= nil then | |
print(app:name(), "is on the wrong screen. Moving to", target_screen:name()) | |
mainWindow:toggleFullScreen() | |
mainWindow:moveToScreen(target_screen) | |
mainWindow:toggleFullScreen() | |
end | |
end | |
end | |
end | |
end | |
end | |
-- Workaround IME failing to enter Chinese symbols | |
-- if switched programmatically. | |
function fix_ime_symbols() | |
for key, value in pairs({ | |
['.'] = '。', | |
[','] = ',' , | |
['\\'] = '、', | |
['['] = '【', | |
[']'] = '】', | |
[';'] = ';', | |
['`'] = '·', | |
}) do | |
hs.hotkey.bind({}, key, function() | |
if hs.keycodes.currentMethod() == cjkv_ime_name then | |
hs.eventtap.keyStrokes(value) | |
else | |
hs.eventtap.keyStrokes(key) | |
end | |
end) | |
end | |
-- Credits: ChatGPT | |
for key, value in pairs({ | |
["1"] = {"!", "!"}, | |
["2"] = {"@", "@"}, | |
["3"] = {"#", "#"}, | |
["4"] = {"¥", "$"}, | |
["5"] = {"%", "%"}, | |
["6"] = {"……", "^"}, | |
["7"] = {"&", "&"}, | |
["8"] = {"*", "*"}, | |
["9"] = {"(", "("}, | |
["0"] = {")", ")"}, | |
["-"] = {"_", "_"}, | |
["="] = {"+", "+"}, | |
["["] = {"「", "{"}, | |
["]"] = {"」", "}"}, | |
["\\"] = {"|", "|"}, | |
[";"] = {":", ":"}, | |
[","] = {"《", "<"}, | |
["."] = {"》", ">"}, | |
["/"] = {"?", "?"}, | |
["`"] = {"~", "~"}, | |
}) do | |
hs.hotkey.bind({'shift'}, key, function() | |
local index = hs.keycodes.currentMethod() == cjkv_ime_name and 1 or 2 | |
hs.eventtap.keyStrokes(value[index]) | |
end) | |
end | |
hs.hotkey.bind({}, "'", function() | |
if hs.keycodes.currentMethod() == cjkv_ime_name then | |
hs.eventtap.keyStrokes(double_quote_flag and '’' or '‘') | |
double_quote_flag = not double_quote_flag | |
else | |
hs.eventtap.keyStrokes("'") | |
end | |
end) | |
hs.hotkey.bind({'shift'}, "'", function() | |
if hs.keycodes.currentMethod() == cjkv_ime_name then | |
hs.eventtap.keyStrokes(double_quote_flag and '”' or '“') | |
double_quote_flag = not double_quote_flag | |
else | |
hs.eventtap.keyStrokes('"') | |
end | |
end) | |
end | |
-------- Utils -------- | |
function printList(list) | |
for i, value in ipairs(list) do | |
print("🐟", value) | |
end | |
end | |
function table.contains(tbl, value) | |
for _, v in ipairs(tbl) do | |
if v == value then | |
return true | |
end | |
end | |
return false | |
end | |
-------- Entrance -------- | |
hs.console.clearConsole() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment