Skip to content

Commit 8fc5e67

Browse files
Update workflow version
1 parent 12f530d commit 8fc5e67

File tree

3 files changed

+109
-49
lines changed

3 files changed

+109
-49
lines changed

vscode-open-project/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ no any dependency.
2121

2222

2323

24-
[![](https://img.shields.io/badge/version-v1.2-green?style=for-the-badge)](https://img.shields.io/badge/version-v1.2-green?style=for-the-badge)
24+
[![](https://img.shields.io/badge/version-v1.3-green?style=for-the-badge)](https://img.shields.io/badge/version-v1.3-green?style=for-the-badge)
2525
[![](https://img.shields.io/badge/download-click-blue?style=for-the-badge)](https://github.com/alanhe421/alfred-workflows/raw/master/vscode-open-project/VSCode%20-%20Open%20Project.alfredworkflow)
2626

2727

vscode-open-project/src/info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<key>acceptsurls</key>
104104
<false/>
105105
<key>name</key>
106-
<string>Open With {VAR:SECOND_EDITOR}</string>
106+
<string>Open With {var:SECOND_EDITOR}</string>
107107
</dict>
108108
<key>type</key>
109109
<string>alfred.workflow.trigger.universalaction</string>
@@ -433,7 +433,7 @@ no any dependency.
433433
<key>variablesdontexport</key>
434434
<array/>
435435
<key>version</key>
436-
<string>1.2</string>
436+
<string>1.3</string>
437437
<key>webaddress</key>
438438
<string>https://github.com/alanhe421/alfred-workflows/tree/master/vscode-open-project</string>
439439
</dict>

vscode-open-project/src/main.sh

Lines changed: 106 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,72 @@
11
#!/bin/bash
22

3-
# 获取查询字符串和编辑器信息
3+
# 编辑器映射:命令名 -> 应用显示名
4+
editor_to_app() {
5+
case "$1" in
6+
code)
7+
echo "Code"
8+
;;
9+
cursor)
10+
echo "Cursor"
11+
;;
12+
code-insiders)
13+
echo "Code - Insiders"
14+
;;
15+
antigravity)
16+
echo "Antigravity"
17+
;;
18+
*)
19+
echo "Code" # fallback
20+
;;
21+
esac
22+
}
23+
24+
# 获取查询字符串
425
query="$1"
5-
editor=$(if [[ "$EDITOR" == "cursor" ]]; then echo "Cursor"; else echo "Code"; fi)
626

7-
# 定义 state.vscdb 文件路径
8-
STATE_DB="$HOME/Library/Application Support/${editor}/User/globalStorage/state.vscdb"
27+
# 1. Primary Editor Logic
28+
primary_app="$(editor_to_app "$EDITOR")"
29+
if [[ -z "$primary_app" ]]; then
30+
primary_app="Code"
31+
fi
32+
33+
DB1="$HOME/Library/Application Support/${primary_app}/User/globalStorage/state.vscdb"
34+
dbs=("$DB1")
935

10-
# 检查文件是否存在
11-
if [[ ! -f "$STATE_DB" ]]; then
36+
# 2. Second Editor Logic
37+
if [[ -n "$SECOND_EDITOR" && "$SECOND_EDITOR" != "$EDITOR" ]]; then
38+
second_app=""
39+
# Look up in map
40+
if [[ -n "$(editor_to_app "$SECOND_EDITOR")" ]]; then
41+
second_app="$(editor_to_app "$SECOND_EDITOR")"
42+
fi
43+
44+
if [[ -n "$second_app" ]]; then
45+
DB2="$HOME/Library/Application Support/${second_app}/User/globalStorage/state.vscdb"
46+
# Only add if file exists (checked later in ruby as well but good to filter here)
47+
if [[ -f "$DB2" ]]; then
48+
dbs+=("$DB2")
49+
fi
50+
fi
51+
fi
52+
53+
# 检查至少有一个文件存在 (Main DB validation is handled by ruby or implicit check)
54+
# The original script checked explicitly "if [[ ! -f "$STATE_DB" ]]"
55+
# Here we can proceed if we have at least one valid DB.
56+
# If DB1 is missing, we might still have DB2.
57+
# So we check if our array has any existing files?
58+
# Actually, let's just pass all candidates to Ruby and let it handle emptiness.
59+
# But for "fast fail" if main DB is missing AND no second DB?
60+
if [[ ! -f "$DB1" && ${#dbs[@]} -eq 1 ]]; then
61+
# Original behavior for single DB missing
1262
echo '{"items": []}'
1363
exit 1
1464
fi
1565

1666
# 使用单个 Ruby 脚本处理所有逻辑
17-
# 1. 获取 SQLite 数据
67+
# 1. 获取 SQLite 数据 (Loop over DBs)
1868
# 2. 解析 JSON
19-
# 3. 处理路径
69+
# 3. 处理路径 & 去重
2070
# 4. 生成输出
2171
ruby -e '
2272
require "json"
@@ -26,47 +76,57 @@ require "open3"
2676
begin
2777
# 获取参数
2878
query = ARGV[0].to_s.strip.downcase
29-
db_path = ARGV[1]
30-
31-
# 执行 SQLite 查询
32-
sql = "SELECT value FROM ItemTable WHERE key = '\''history.recentlyOpenedPathsList'\''"
33-
stdout, stderr, status = Open3.capture3("sqlite3", db_path, sql)
79+
db_paths = ARGV[1..-1]
3480
35-
if !status.success? || stdout.empty?
36-
puts JSON.generate({items: []})
37-
exit 0
38-
end
39-
40-
# 解析 JSON 数据
41-
data = JSON.parse(stdout)
42-
43-
# 处理项目并生成输出
4481
items = []
45-
46-
data["entries"].each do |entry|
47-
uri = entry["folderUri"] || entry["fileUri"]
48-
next unless uri
49-
50-
# 处理路径
51-
path = URI.decode_www_form_component(uri.sub("file://", ""))
52-
name = File.basename(path)
82+
seen_paths = {}
83+
84+
db_paths.each do |db_path|
85+
next unless File.exist?(db_path)
86+
87+
# 执行 SQLite 查询
88+
sql = "SELECT value FROM ItemTable WHERE key = '\''history.recentlyOpenedPathsList'\''"
89+
stdout, stderr, status = Open3.capture3("sqlite3", db_path, sql)
5390
54-
# 如果有查询词,进行过滤
55-
next if !query.empty? && !name.downcase.include?(query)
91+
next if !status.success? || stdout.empty?
5692
57-
# 添加到结果
58-
items << {
59-
uid: name,
60-
type: "file",
61-
title: name,
62-
subtitle: path,
63-
arg: path,
64-
autocomplete: name,
65-
icon: {
66-
type: "fileicon",
67-
path: path
68-
}
69-
}
93+
begin
94+
# 解析 JSON 数据
95+
data = JSON.parse(stdout)
96+
97+
data["entries"].each do |entry|
98+
uri = entry["folderUri"] || entry["fileUri"]
99+
next unless uri
100+
101+
# 处理路径
102+
path = URI.decode_www_form_component(uri.sub("file://", ""))
103+
104+
# 去重: subtitle is path
105+
next if seen_paths[path]
106+
seen_paths[path] = true
107+
108+
name = File.basename(path)
109+
110+
# 如果有查询词,进行过滤
111+
next if !query.empty? && !name.downcase.include?(query)
112+
113+
# 添加到结果
114+
items << {
115+
uid: name,
116+
type: "file",
117+
title: name,
118+
subtitle: path,
119+
arg: path,
120+
autocomplete: name,
121+
icon: {
122+
type: "fileicon",
123+
path: path
124+
}
125+
}
126+
end
127+
rescue => e
128+
# ignore parse errors for a specific db
129+
end
70130
end
71131
72132
# 输出结果
@@ -75,4 +135,4 @@ rescue => e
75135
# 出错时返回空结果
76136
puts JSON.generate({items: []})
77137
end
78-
' "$query" "$STATE_DB"
138+
' "$query" "${dbs[@]}"

0 commit comments

Comments
 (0)