使用mac也有些年头了,关于工具,可能常见的基本上都尝试过,比如使用mac air传递文件,用brew安装软件,iterm上搞很炫酷的主题,给 VS Code搞爆炸或者毛玻璃的效果,这些可能不是每天都用,但有一个软件是我每天必用,那就是Alfred,这个mac上的神器,我也是看了池建强老师的mac talk才开始使用,不过真的我就把原生的Spotlight直接给废弃到一边了,若是没有体会过的,或许你不知道Alfred是个啥玩意。

说不明白的,就直接给个gif吧。

上图中用了两个Alfred插件:

  1. 一个插件是将图片做了宽高的压缩的处理
  2. 另一个图片主动推到了github上并将生成的链接复制到粘贴板。
    有了这两个插件,图片的缩放和图片的上传问题都可以很好的解决,快捷高效,最主要都是免费的。想想是不是都觉得很美啊。

由上面我们可以知道:Alfred不简单搜索,他更是我们很多工具的入口。

闲话不多说,我直接说下第二个刚才实例中的第二个插件,这个插件是我自己开发,目前相对来说只能满足我们将github作为图床的功能,还有很多bug,不过在写这篇博客时,我已经通过这个上传了多个图片了。
先说下插件的我们的我们需要的几个步骤:

  1. 下载新版本的Alfred
  2. 新建一个插件的入口

其中图中的第二步需要选一个blank workFlower

  1. 新建一个hotKey,并在后面插入一个脚本。

    这个hotKey可以触发Alfred的Workflow,Workflow是一个任务(上图中的是个action脚本,主要输入的快捷键是hotKey,就会自动触发这个脚本),我们可以通过python脚本,shell等其他脚本来开发。

  2. 下面需要我们去编写自己的image_bed.py

进入我们的脚本编辑文件下,目前只有一个info.plist

需要先将Alfred-Workflow对python支持的库放到跟路径下。

在咱们自己创建的alfred的workFlower,最后创建一个空的文件,命名为image_bed.py,这样文件结构就就是:

1
2
3
4
5
6
7
8
9
10
|      
|___workflow
| |______init__.py
| |______background.py
|
|__image_bed.py
|
|___info。plist

`

我就简单把代码贴出来吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding:utf-8 -*-
import json,sys
import img_config as config

import time
import subprocess
import os
from workflow import Workflow

import clipboard

img_dir = config.img_cfg['img_dir']

CLIPBOARD_EXCEPTIONS = (
clipboard.WriteFileError,
clipboard.FileTypeUnsupportedError,
clipboard.NotImageError
)


imgUrl = ""

def timestamp_to_date(time_stamp, format_string="%Y-%m-%d-%H-%M-%S"):
time_array = time.localtime(time_stamp)
str_date = time.strftime(format_string, time_array)
return str_date

def _convert_to_png(src_path, dest_path):
"""转换图片格式为png

:param src_path: 源文件
:param dest_path: 目标文件
"""
os.system('sips -s format png {} --out {}'.format(src_path, dest_path))
os.remove(src_path)

def saveClipboardImg():
global imgUrl

try:
img_path = clipboard.get_pasteboard_img_path()
except CLIPBOARD_EXCEPTIONS as error:
# notice(str(error))
pass
return
notice(str(img_path))
file_name = os.path.split(img_path)[-1]
file_type = file_name.split('.')[-1]
if file_type == 'tiff':
date = int(time.time())
now = timestamp_to_date(date)
filename = str(now)
new_img_path = '{}{}.png'.format(img_dir, filename)
imgUrl = '{}{}.png'.format('https://raw.githubusercontent.com/ownwell/image-bed/master/img/', filename)
notice(imgUrl)

# tiff --> png
_convert_to_png(img_path, new_img_path)
img_path = new_img_path
else:
name = img_path.split("/")[-1]

imgUrl = '{}{}'.format('https://raw.githubusercontent.com/ownwell/image-bed/master/img/', name)



# from PIL import Image
# if isinstance(im, Image.Image):
# global imgUrl

# print(im.format, im.size, im.mode)
# date = int(time.time())
# now = timestamp_to_date(date)
# filename = str(now) + ".jpg"
# imgUrl = "https://github.com/ownwell/image-bed/raw/master/"+filename
# notice(imgUrl)
# im.save(path + filename, im.format)
# width, height = im.size
# pix = im.load()

# else:
# notice("not image")
# pass
def pullToGithub():
cmd = '''cd ''' + img_dir + ''' ;git add .;git commit -m "blog_img";git push origin master
'''
subprocess.call(cmd, shell=True)

def write_to_pasteboard(text):
"""内容写入剪贴板

:param text: 写入内容
"""
os.system('echo \'{}\' | pbcopy'.format(text))


def print_pasteboard_content():
"""从剪贴板打印出内容"""
write_command = (
'osascript -e \'tell application '
'"System Events" to keystroke "v" using command down\''
)
os.system(write_command)


def notice(msg, title=''):
"""通知
:param msg: 通知消息
:param title: 通知标题
"""
pass
os.system('osascript -e \'display notification "{}" with title "{}"\''.format(msg, title))


def main(wf):

saveClipboardImg()
pullToGithub()
notice(imgUrl)
md_img = '![]({})'.format(imgUrl)
write_to_pasteboard(md_img)
print_pasteboard_content();

# notice("1")
# saveClipboardImg()
# write_to_pasteboard("nihao")
# print_pasteboard_content()

if __name__ == '__main__':
wf = Workflow(libraries=['./lib'])
sys.exit(wf.run(main))

解释下,
首先 python脚本获取粘贴板里的内容
而后,判断粘贴板里的内容是否为图片或者图片的路径,并将图片资源或给定路径图片放到我们的本地的git仓库
最后,通过触发git的add和push(pullToGithub方法里)将图片推到git的远程仓库上

总结

  1. hotkey触发运行pytho脚本
  2. python脚本的作用就是赋值粘贴板资源到本地的git仓库,而后上传到git的远程端,并将图片列检拼接出

具体代码已经上传到github上,
https://raw.githubusercontent.com/ownwell/image-bed/master/img/user.workflow.6C32BCCF-38F2-4E9F-8839-B7968C0C0285