IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Launch RStudio, Positron, and other Data Science apps from your Finder Toolbar

    R | Dr Tom Palmer发表于 2024-12-23 00:00:00
    love 0
    [This article was first published on R | Dr Tom Palmer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Introduction

    Have you ever wanted to be able to quickly open a Data Science app, say RStudio Desktop or Positron in the current Finder window at the click of a button? We’ll see how to achieve this by creating Automator apps. Here’s a screenshot of what we’ll end up with.

    Screenshot of a Finder window with app icons in its toolbar.

    Creating the Automator apps

    Open Automator and select New then Application. The in the top left search bar enter applescript and drag and drop that element onto the main window.

    Screenshot of the Automator app.

    We then need to enter the relevant AppleScript code for launching each app in the current Finder window. Currently, I use WezTerm for my terminal emulator, Zed as my main text editor, RStudio Desktop for most of my R/R Markdown/Quarto coding, Visual Studio Code for other text editing tasks, and I have been starting to try out Positron. My AppleScript code for each app is as follows.

    RStudio Desktop

    on run {input, parameters}
      tell application "Finder"
        set myWin to window 1
        set thePath to (quoted form of POSIX path of (target of myWin as alias))
        do shell script "/Applications/RStudio.app/Contents/MacOS/RStudio " & thePath
      end tell
    end run

    WezTerm

    on run {input, parameters}
      tell application "Finder"
        set myWin to window 1
        set thePath to (quoted form of POSIX path of (target of myWin as alias))
        do shell script "/opt/homebrew/bin/wezterm-gui start --cwd " & thePath
      end tell
    end run

    Zed

    on run {input, parameters}
      tell application "Finder"
        set myWin to window 1
        set thePath to (quoted form of POSIX path of (target of myWin as alias))
        do shell script "/usr/local/bin/zed -n " & thePath
      end tell
    end run

    R launched in a WezTerm terminal session

    on run {input, parameters}
      tell application "Finder"
        set myWin to window 1
        set thePath to (quoted form of POSIX path of (target of myWin as alias))
        do shell script "/opt/homebrew/bin/wezterm-gui start --cwd " & thePath & " -- /usr/local/bin/R"
      end tell
    end run

    Visual Studio Code and Positron

    First enable the ability to launch these apps with code and positron from a Terminal in each app, see here and here.

    This script is more involved because we first check for any .code-workspace files and open one if found. My AppleScript coding is not very proficient, so there may more efficient approaches to coding this.

    on run {input, parameters}
      tell application "Finder"
        try
          set currentFolder to (folder of window 1) as alias
          set workspaceFiles to (every file of currentFolder whose name extension is "code-workspace")
          if (count of workspaceFiles) = 0 then 
            set folderPath to POSIX path of currentFolder
            do shell script "/usr/local/bin/positron -n " & quoted form of folderPath
          else if (count of workspaceFiles) = 1 then 
            set workspaceFile to item 1 of workspaceFiles
            set workspacePath to POSIX path of (workspaceFile as alias)
            do shell script "/usr/local/bin/positron -n " & quoted form of workspacePath
          else if (count of workspaceFiles) > 1 then 
            display dialog "Multiple code-workspace files found in directory."  
          end if
          on error
              display dialog "No Finder window is open."
          end try
      end tell
    end run

    (For your Visual Studio Code app simply replace positron with code.)

    Saving and adding icons

    After adding the AppleScript code save each app. I call mine, e.g., RStudio-openhere.app.

    Next it is helpful to give each app the relevant icon. To do this in Finder bring up the Info boxes for the original app and your -openhere version by selecting each app and pressing Cmd+I. Next drag the large icon from the original app onto the small icon of your -openhere app.

    Screenshot of copying the Positron icon onto our openhere app.

    Adding the apps to the Finder toolbar

    Finally, we need to place shortcuts of these apps onto the Finder toolbar. To do this first hold down Cmd then drag the app from Finder onto the toolbar to the location you would like. On release the app should now be in the toolbar. (And to remove an icon from the toolbar, again hold Cmd then drag it off the toolbar.)

    Screenshot of moving our Positron-openhere app onto the Finder toolbar.

    Summary

    I have shown how to create Automator apps to open RStudio Desktop, Positron, and several other Data Science apps from the current Finder window.

    To leave a comment for the author, please follow the link and comment on their blog: R | Dr Tom Palmer.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Continue reading: Launch RStudio, Positron, and other Data Science apps from your Finder Toolbar


沪ICP备19023445号-2号
友情链接