"Open Recent VS Code Project" example script not working anymore #1416
-
|
I've been using the "Open Recent VS Code Project" script and been really liking it, but it does not seem to work anymore. I tried completely uninstalling kit and reinstalling it (+ adding the script again), but I still get the same error. Error: Full log: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
|
@benschlegel Can you try editing the script with something like this to handle the error? Mine doesn't error, so I think it's a recent project on your system that's that being read in correctly. // Name: Recent VS Code Project
// Cache: true
// Group: Favorite
// Keyword: vs
import "@johnlindquist/kit"
import { URL, fileURLToPath } from "url"
// /Users/johnlindquist/Library/Application Support/Code/User/globalStorage/state.vscdb
let filename = home("Library", "Application Support", "Code", "User", "globalStorage", "state.vscdb")
// windows path not tested, just guessing
if (isWin) filename = home("AppData", "Roaming", "Code", "User", "globalStorage", "state.vscdb")
let { default: sqlite3 } = await import("sqlite3")
let { open } = await import("sqlite")
const db = await open({
filename,
driver: sqlite3.Database,
})
let key = `history.recentlyOpenedPathsList`
let table = `ItemTable`
let result = await db.get(`SELECT * FROM ${table} WHERE key = '${key}'`)
let recentPaths = JSON.parse(result.value)
let recentFilePaths = []
for (const entry of recentPaths.entries) {
if (entry?.folderUri && entry.folderUri.startsWith("file://")) {
try {
const path = fileURLToPath(new URL(entry.folderUri))
recentFilePaths.push(path)
} catch (error) {
console.error(`Failed to parse ${entry.folderUri}. Error: ${error}`)
}
}
}
let recentPath = await arg("Open a recent path", recentFilePaths)
hide()
await exec(`code ${recentPath}`) |
Beta Was this translation helpful? Give feedback.
-
|
@johnlindquist I was playing around with this a bit more and got it working for all cases that i could find that previously caused crashes (including launching remote ssh folders/virtual desktops). Should I publish my script as a separate new "v2" community script? My findings are that there were 3 unsupported types of projects:
I handled all those cases now. After digging through some docs, i found out you can launch remote connections like Running the script now looks like this: Non-standard/non-local projects use the label provided by vscode itself for clarity (e.g. The full source code for my new script: // Name: Open Recent VS Code Project
// Shortcode code
// Shortcut: cmd shift o
import "@johnlindquist/kit"
import { Action } from "@johnlindquist/kit"
import { URL, fileURLToPath } from "url"
/**
* Wether to include projects that are a single file in recent projects (defaults to false)
*/
const includeSingleFiles = false
// /Users/johnlindquist/Library/Application Support/Code/User/globalStorage/state.vscdb
let filename = home("Library", "Application Support", "Code", "User", "globalStorage", "state.vscdb")
// windows path not tested, just guessing
if (isWin) filename = home("AppData", "Roaming", "Code", "User", "globalStorage", "state.vscdb")
// @ts-ignore
let { default: sqlite3 } = await import("sqlite3")
let { open } = await import("sqlite")
const db = await open({
filename,
driver: sqlite3.Database,
})
let key = `history.recentlyOpenedPathsList`
let table = `ItemTable`
let result = await db.get(`SELECT * FROM ${table} WHERE key = '${key}'`)
let recentPaths = JSON.parse(result.value)
// Collect all "recent project" data and parse it for display in kit (special cases for vscode-remote, virtual desktop and single files)
let recentFilePaths: Action[] = []
for (const entry of recentPaths.entries) {
// Check if entry is a folder
if (entry.folderUri) {
if (entry.folderUri.startsWith("file://")) {
// Default case (recent project is a regular folder)
const path = fileURLToPath(new URL(entry.folderUri))
const label = entry.label ?? getLabelFromPath(path);
recentFilePaths.push({name: label, description: path, value: path})
} else if ((entry.folderUri.startsWith("vscode-remote://") || entry.folderUri.startsWith("vscode-vfs://"))) {
// Project is a folder but on remote (ssh or github virtual desktop)
const label = entry.label ?? entry.folderUri;
// vscode remote session expects to be launched like `code --folder-uri=vscode-remote://<path>` (same for vscode-vfs)
const value = `--folder-uri=${entry.folderUri}`
recentFilePaths.push({name: label, description: entry.folderUri, value: value})
}
} else {
// This branch only occurs if recent project is a single file instead of entire folder (ignored by default)
// To also add single files to recent projects, go to line 9 and change `includeSingleFiles` from false to true
if (includeSingleFiles) {
try {
const path = fileURLToPath(new URL(entry.fileUri))
const label = getLabelFromPath(path)
recentFilePaths.push({name: label, description: path, value: path})
} catch (error) {
// In case recent project is different non supported type
console.error(`Failed to parse ${entry.folderUri}. Error: ${error}`)
}
}
}
}
let recentPath = await arg("Open recent project", recentFilePaths)
hide()
await exec(`code ${recentPath}`)
function getLabelFromPath(path: string) {
return path.substring(path.lastIndexOf("/") + 1);
} |
Beta Was this translation helpful? Give feedback.

@benschlegel Can you try editing the script with something like this to handle the error? Mine doesn't error, so I think it's a recent project on your system that's that being read in correctly.