Comments (10)
Python Debug: Log to My clipboard
- debug 工具太重,print 函数太轻。所以利用 Alfred snippets,Python 分号,以及 Python 小工具实现快速舒适优雅的人工 debug,每一处都是细节,细节的串联带来心情的愉悦
_log = {{cursor}}; import json; import pyperclip; pyperclip.copy(json.dumps(str(_log), sort_keys=True, indent=4))
from learning_list.
vscode snippets function
比较了一下,发现 vscode 的 snipptes 功能比 Alfred 的更强大一下,可以针对不同语言进行设置,而且可以用同步设置的插件进行同步。很棒,接下来要把这个功能用起来,正好要进行重构代码工作,一个可以把 snipptes 培养成习惯的好时机。
from learning_list.
输入法自动切换
⚠️ 已经找到更好的方法,在 hammerspoon 生态中
#! /usr/bin/env python
# coding: utf-8
# ! 自动切换输入法的最终解决方案:
# ! 利用 issw 和 MacOS 自带的切换前台应用时发出的通知
'''
auto switch input method between different applications
if you want to change the app list, modify the var 'en_list' and 'zh_list',
or add your own custom_list.
'''
from __future__ import unicode_literals
from __future__ import division
from __future__ import print_function
from subprocess import call, check_output
from AppKit import NSWorkspace, NSWorkspaceDidActivateApplicationNotification, NSWorkspaceApplicationKey
from Foundation import NSObject
from PyObjCTools import AppHelper
# !add your custom apps here, check the bundle id in /Application/xx.app/Contents/info.plist
# !or use command: osascript -e 'id of app "Safari"'
en_list = [
"com.googlecode.iterm2",
"com.google.Chrome",
"com.apple.Terminal",
"com.sublimetext.3",
"com.apple.Safari",
"com.sequelpro.SequelPro",
"com.microsoft.VSCode",
]
zh_list = [
"com.tencent.xinWeChat",
"com.tencent.WeWorkMac",
"com.workflowy.desktop",
"com.apple.Notes",
"com.soulmen.ulysses3",
]
def get_avaliable_input_methods():
return check_output(["issw", '-l']).decode().strip().split('\n')
class Observer(NSObject):
def handle_(self, noti):
info = noti.userInfo().objectForKey_(NSWorkspaceApplicationKey)
bundleIdentifier = info.bundleIdentifier()
if bundleIdentifier in en_list:
print("found: [%s] active, switch to US" % bundleIdentifier)
call(["issw", "com.apple.keylayout.ABC"])
elif bundleIdentifier in zh_list:
print("found: [%s] active, switch to Shuangpin" % bundleIdentifier)
call(["issw", "com.apple.inputmethod.SCIM.Shuangpin"])
def main():
nc = NSWorkspace.sharedWorkspace().notificationCenter()
observer = Observer.new()
nc.addObserver_selector_name_object_(
observer,
"handle:",
NSWorkspaceDidActivateApplicationNotification,
None
)
AppHelper.runConsoleEventLoop(installInterrupt=True)
if __name__ == '__main__':
main()
from learning_list.
Loose JSON string Parse
function run(argv) {
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
function looseJsonParse(obj){
return Function('"use strict";return (' + obj + ')')();
}
var query = argv[0];
var format_str = (source={}) => {
let str = (obj) => JSON.stringify(obj, null, 4);
return str(source);
}
query = query.replaceAll('\n', '')
return format_str(looseJsonParse(query))
}
from learning_list.
How to Paste Everything as Plain Text
Try to copy something from a web page into your email, and you’ll find that the result is distorted by HTML and other unnecessary bits. Here’s how you can prevent that using a simple workaround that does not rely on the use of a third-party app.
- Open System Preferences from the Apple menu and select Keyboard.
- On the Keyboard panel, select the tab for Shortcuts.
- Scroll down the list on the left and select App Shortcuts.
- Click the “+” below the boxes to add a shortcut.
- Select All Applications for Application input box. This (theoretically) will apply the shortcut across the board.
- Type Paste and Match Style(系统语言为中文时名称为「粘贴并匹配样式」) into the Menu Title input box. This takes the copied text and pastes it in using the current document’s formatting.
- set shift+command+v
from learning_list.
少用 rm,优先考虑 trash 命令
brew install trash
from learning_list.
autoenv 自动执行shell脚本
自动激活 Python 虚拟环境
#source "${AUTOENV_CUR_DIR}/.venv/bin/activate\""
if [[ "${AUTOENV_CUR_DIR}" == "\"$PWD" ]] then
source .venv/bin/activate
fi
from learning_list.
alfred top precess workflow
安装新系统之后,往往不能工作,此时应该是 ruby 的依赖被破坏。重新安装,可能需要 sudo 权限。
sudo gem install alfred-workflow
sudo gem install mixlib-shellout -v 2.2.6
from learning_list.
autoexpect: shell auto workflow
autoexpect command => script.exp => 加工修改 => real.exp
#!/usr/bin/expect
# 参数1: jumpserver 参数2: server_ip
if { $argc != 2 } {
send_user "Usage: jumpserver, server_ip \n"
exit
}
set force_conservative 0
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
set timeout -1
# 接收入参
set jumpserver [lindex $argv 0]
set server_ip [lindex $argv 1]
#开始执行自动 shell 交互
#登录跳板机
catch {spawn ssh $jumpserver}
#登录目标机
expect "*IP*"
send "/"
expect "*K/"
send "$server_ip"
send "\r"
expect "*36m"
send "\r"
expect "USERNAME"
send "username\r"
expect {
"*yes/no*" {send "yes\r";exp_continue}
"*assword*" {send "password\r";exp_continue}
"*~*" {send "sudo su -\r"}
}
expect "*username*"
send "password\r"
expect "*~*"
send "screen -D -r supervisor\r"
# 将控制权交给用户
interact
from learning_list.
相见恨晚的 Hammerspoon
- Hammerspoon 是更高阶的 Alfred,而且两者完全可以配合使用。
- Hammerspoon 更靠近底层,在 Mac OS 基础上对外提供更好用的 API
- Hammerspoon 可编程性更强,所有的功能都是代码实现,不需要界面,却可以产生界面,魔改空间更大
- Hammerspoon 统一使用 lua 脚本,同时支持 Mac OS 上的自动化脚本:Applescript 和 JXA (JXA stands for JavaScript for Automation.)
- Hammerspoon 学习曲线更为陡峭,但学到手的是实实在在的硬技能
Hammerspoon 可以完美实现的功能:
- 给任意程序设置启动或进入前台时的默认输入法
- 全局搜索多个浏览器的 tab 页面,并直接进入
- 完备的窗口管理能力,可实现任何想要的功能
- 弹窗式全功能谷歌翻译
- 精美的时钟插件
- 小功能直接放在菜单栏,仿佛一个小 app
- 基于上下文的快捷键
- ...
Alfred 生态中已经有全局搜索浏览器 tab 的插件,但毕竟是收费的,最近几天业余时间主要研究如何在 Hammerspoon 中完美实现,最终思路如下:
- 利用 AppleScript 拿到所有在运行浏览器 tab 列表:标题,URL,tabindex,windowindex
- 用户搜索或选中某一个 tab
- 使用 tab 的 tabindex 和 windowindex,将页面放置到前台
Hammerspoon 中已经有一个实现,但还不完美,存在某些问题,但是流程已经有了。
我做的改动是,提取 tabindex,windowindex,避免激活时再次查询一遍,同时各个浏览之间的激活方式也能使用更一致的方案。
提取 tablist 的脚本:
tell application "Google Chrome"
set winlist to tabs of windows
set tablist to {}
set winidx to 0
repeat with i in winlist
set winidx to winidx + 1
if (count of i) > 0 then
set tabidx to 0
repeat with currenttab in i
set tabidx to tabidx + 1
set tabinfo to {title of currenttab as Unicode text, URL of currenttab, tabidx, winidx}
copy tabinfo to the end of tablist
end repeat
end if
end repeat
return tablist
end tell
safari 和 chrome 有一点不一样,不能用 title, 需要用 name
tell application "Safari"
set winlist to tabs of windows
set tablist to {}
set winidx to 0
repeat with i in winlist
set winidx to winidx + 1
if (count of i) > 0 then
set tabidx to 0
repeat with currenttab in i
set tabidx to tabidx + 1
set tabinfo to {name of currenttab as Unicode text, URL of currenttab, tabidx, winidx}
copy tabinfo to the end of tablist
end repeat
end if
end repeat
return tablist
end tell
将特定 tab 带到前台的脚本(不得不说,方法很是诡异):
tell application "Safari" to set index of window 2 to 1
tell window 2 of application "Safari" to set current tab to tab 1
同时也可以考虑 JXA,而且搜索的时候可以同时搜索 AppleScript 和 JXA,有些大神喜欢用 JXA,有些喜欢使用 AppleScript。
但要知道Safari 对 JXA 的支持有限。
另外还需要提到一点,很多时候,我们的 AppleScript 或 JXA 脚本需要从 lua 脚本中接受变量,这个时候该如何操作呢?
- 总的方案就是字符串拼接
.. a ..
- 比较坑的是,lua 中多行字符串使用
[[ a, bc ]]
实现,所以出现了比较诡异的多行字符串拼接方案 local setWindow = [[tell application "Safari" to set index of window ]] .. input.wid .. [[ to 1]]
一开始我还以为 ]] .. someVar .. [[
是某种特殊语法呢,坑了我一把。
建议使用 Mac 自带的脚本编辑器调试 AppleScript 和 JXA。
入门 JXA 的好文
- https://medium.com/@bit2pixel/how-i-navigate-hundreds-of-tabs-on-chrome-with-jxa-and-alfred-9bbf971af02b
- https://wiki.nikitavoloboev.xyz/macos/jxa
学了一波之后,发现 JAX 也可以搞定 Safari,但是方法不一样:
#!/usr/bin/env osascript -l JavaScript
const safari = Application('Safari')
// need activate firstly
safari.activate()
safari.windows[winIdx].visible = true
// safari need take tab to frontmost by this way
safari.windows[winIdx].currentTab = safari.windows[winIdx].tabs[tabIdx]
safari.windows[winIdx].index = 1
反观 Chrome,是这样:
chrome.activate()
chrome.windows[winIdx].visible = true
chrome.windows[winIdx].activeTabIndex = tabIdx + 1
chrome.windows[winIdx].index = 1
from learning_list.
Related Issues (20)
- 给函数式程序员的 Hack Pipe:我如何学着停止担忧并爱上那顶小帽子 HOT 1
- 一文说透 TypeScript 的增删查改
- 重温 React hook 基本原理
- rust 中消失的三元去了哪里?
- 如何判断类型中是否有必填字段
- automate chrome by script kit
- applescript 踩坑经验
- hammerspoon 添加自定义菜单,执行 shell 脚本
- karabiner multitouch_extension
- 我的 Mac 使用实践总结
- 活久见之 JS 新见闻:return 写不写还是有关键区别的。
- Rust trait vs TypeScript interface
- React Final Form useFormValuesWatch
- data fetch in react
- type safe useParams
- finally, learn how generate struct with builtin function in Zig
- zig code: auto git push, find suitable target branch and open merge url in browser
- bun preloader for test
- depth protection and loose union
- 初步建立起 RSC 的直观体验 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning_list.