DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Implementing Infinite Scroll in jOOQ
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services

Trending

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  1. DZone
  2. Coding
  3. Languages
  4. Using Alfred to Quickly Open Workspaces

Using Alfred to Quickly Open Workspaces

In this case, we want to use Alfred as a way to quickly and interactively choose a VS Code workspace to open.

By 
Nico Stewart user avatar
Nico Stewart
·
Feb. 26, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
2.5K Views

Join the DZone community and get the full member experience.

Join For Free

I use VSCode for almost all of my development, enabling me to save folders and their custom settings to workspace files.

Alfred is a macOS program that serves as a launcher, clipboard manager, finder, and much more. In this case, I want to use Alfred as a way to quickly and interactively choose a VSCode workspace to open.

Required Structure

Alfred’s Script Filters allow us to search a dynamic list for some item and then do something with that item. The documentation then links to the Script Filter JSON format. We’re going to create a script (binary) that outputs all discovered workspaces in the required format.

Code

We can use basic commands to discover all workspaces we want to include:

JSON
 
find ~/code -type file -maxdepth 1 -name '*.code-workspace'


This gives us all workspace files in the ~/code directory. From there, we can easily create the JSON object required by Alfred.

The equivalent command from go is:

Go
 
filepath.Glob(fmt.Sprintf("%s/*%s", filepath.Join(home, codeDir), workspaceFileType))


Where home is our home directory, and codeDir is the folder in our home directory that contains our workspaces. Now that I’ve found all our workspace files, I just need to create JSON objects from them that Alfred will consume.

We’ll define a basic struct to represent that object:

JSON
 
type alfredList struct {
    Items []alfredItem `json:"items"` // All the items to choose from
}
type alfredItem struct {
    UID          string `json:"uid"`          // ID, just the basename
    Type         string `json:"type"`         // Type of item, always 'file'
    Title        string `json:"title"`        // Title to display - friendly name
    Subtitle     string `json:"subtitle"`     // Subtitle to display - short filepath
    File         string `json:"arg"`          // Filepath to open if the item is selected
    Autocomplete string `json:"autocomplete"` // String to use for autocomplete
}


We can definitely get fancier with our objects, but for now, we’ll keep it simple. Our function will create an alfredItem given the file path to a workspace file:

JSON
 
// newAlfredItem creates an alfredItem given the path of a workspace file
func newAlfredItem(workspaceFile string) alfredItem {
    // Remove leading path and file extension
    baseName := strings.Replace(filepath.Base(workspaceFile), workspaceFileType, "", 1)

    return alfredItem{
        UID:          baseName,
        Type:         "file",
        Title:        baseName,
        Subtitle:     filepath.Join("~", codeDir, baseName), // Use '~' instead of $HOME
        File:         workspaceFile,
        Autocomplete: strings.ReplaceAll(baseName, "-", " ") // Split dash-separated words
  }
}


So we want to feed all discovered workflow files into our newAlfredItem method and then print those in the required format. With basic error handling, we end up with this:

JSON
 
matches, err := filepath.Glob(fmt.Sprintf("%s/*%s", filepath.Join(home, codeDir), workspaceFileType))
if err != nil {
  log.Fatal(err)
}

list := alfredList{}
for _, m := range matches {
  list.Items = append(list.Items, newAlfredItem(m))
}

output, err := json.Marshal(list)
if err != nil {
  log.Fatal(err)
}
fmt.Println(string(output))


Using From Alfred

Create a new blank workflow. In the new workflow, add a Script Filter by selecting Inputs -> Script Filter. We need to give it some keywords that will bring up the list when we type it. I used ‘vc’ because nothing else was using it.

To get the result of our program into the list of items, we’ll just compile our Go program and run the binary from our workflow.

Keyword vc

When we run the workflow by entering the keyword vc, we see a list of the discovered workspaces:

Results

By hitting Enter on a single workspace, we’ll open it in VS Code.

Improvements

Unless we’re adding a lot of workspaces each day, we’ll be fine getting our list from a static-ish JSON file. In the past, I’ve used Lingon to create a job that periodically rediscovers each workspace file and saves them to a JSON file.

JSON Workspace Filter (software) workflow

Published at DZone with permission of Nico Stewart. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • Implementing Infinite Scroll in jOOQ
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator
  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!