Marco Nie - 2022年1月 https://blog.niekun.net/2022/01/ you are the company you keep... macOS 中给 finder 添加右键打开 vs code 选项 https://blog.niekun.net/archives/2403.html 2022-01-27T22:00:00+08:00 macOS 中的右键菜单没有像 windows 那样多样的选项,我最常用的是:用 vs code 打开文件夹,新建文本文件等。目前有第三方的软件可以添加这些功能,比如 irightmouse,但基本都是收费软件,下面介绍如何通过系统自带的 automator 来添加我们需要的功能。vs code 菜单首先我们创建一个 Open in Visual Studio Code 的 action。打开 Automator,点击 file - new 创建一个 quick action:首先修改右边的参数,workflow receives current files or folders in finder,下面的 image 根据个人喜好修改:然后左侧搜索 run shell script 双击添加到右侧,修改 pass input:as arguments,然后脚本内容如下:for f in "$@" do open -a 'Visual Studio Code' "$f" done保存脚本即可,命名为 Open in Visual Studio Code,需要保存到当前用户目录下的 library/services 目录下(默认就会保存到这个目录):然后在 finder 中测试,选中一个文件夹查看右键菜单,在 quick action 中会出现 Open in Visual Studio Code:点击后即可在 vs code 中打开文件或文件夹。copy path 菜单下面我们用同样的方法做一个复制文件或文件夹路径的 action。新建一个 quick action,修改右边的参数,workflow receives current files or folders in finder。然后添加一个 copy to clipboard 的 variable 即可:命名为 Copy Path 并保存即可。此时 finder 中 quick action 菜单中就会有了对应的选项:create new txt file 菜单macOS 中没有右键创建新文件的选项,想要在当前目录中创建一个新文件挺麻烦的,下面我们通过类似的方法来创建一个快捷选项。新建一个 quick action,这里 receives 选择 no input:然后搜索并添加一个 Run AppleScript 变量,修改脚本内容如下:set file_name to "untitled" set file_ext to ".txt" set is_desktop to false -- get folder path and if we are in desktop (no folder opened) try tell application "Finder" set this_folder to (folder of the front Finder window) as alias end tell on error -- no open folder windows set this_folder to path to desktop folder as alias set is_desktop to true end try -- get the new file name (do not override an already existing file) tell application "System Events" set file_list to get the name of every disk item of this_folder end tell set new_file to file_name & file_ext set x to 1 repeat if new_file is in file_list then set new_file to file_name & " " & x & file_ext set x to x + 1 else exit repeat end if end repeat -- create and select the new file tell application "Finder" activate set the_file to make new file at folder this_folder with properties {name:new_file} if is_desktop is false then reveal the_file else select window of desktop set selection to the_file delay 0.1 end if end tell -- press enter (rename) tell application "System Events" tell process "Finder" keystroke return end tell end tell脚本中,定义了新建文件的文件名和扩展名,如果修改其他名称可以自行修改。完成后保存即可,名称为 Create New TXT File。保存后在 finder 中我们在任意位置右键会发现没有 quick action 选项,这是因为这个 action 定义中并没有选择输入源,因为新建文件和当前的文件并没有什么关系。此时需要通过左上角的 finder - services 找到我们刚创建的 action:也可以给这个 action 定义快捷键,在 keyboard - shrotcuts - services 中找到这个 action 在后边添加 shortcut 即可:注意第一次运行这个 action 会提示需要授权 finder 的几个权限,根据提示打开即可。参考链接Create a new (.txt) File in Finder - Keyboard ShortcutOSX: how to add a right click option to a folder which opens the folder with an app like VS Code? 获取 JD cookie 方法 https://blog.niekun.net/archives/2388.html 2022-01-09T18:53:00+08:00 在运行自动化脚本的时候需要获取到对应网站的登录 cookie,这里介绍通过 chrome 浏览器获取到需要的信息。这里提供的方法仅供用于前端开发测试。首先登陆移动端网站:https://m.jd.com右键点击 inspect 进入开发者界面,然后点击网页右上角的登录键根据提示需要输入账户信息登录。登录成功后在开发栏切换到 network 选项,并且在 filter 中输入 log.gif 过滤出需要的信息:选中第一个 log 文件,点击 header 栏,在其中找到 cookie 项:复制其中的内容到剪贴板。找到开发页面下方的 console 栏,我们通过简单的 js 脚本提取出上面 cookie 的有效内容:var c = '这里粘贴复制好的 cookie'; var value = c.match(/pt_pin=.+?;/) + c.match(/pt_key=.+?;/); copy(value);执行完成后此时我们的剪贴板里就已经复制好了 cookie 信息。手机端可以通过解包 app 来提取 cookie,具体可以参考我的 js 脚本:https://github.com/nie11kun/config-rules/blob/master/Shadowrocket/get_jdcookie.js