fork-dev / Tracker

Bug and issue tracker for Fork for Mac
494 stars 12 forks source link

[Feature Request] Implement AppleScript / JXA support. #2104

Open vgorloff opened 2 months ago

vgorloff commented 2 months ago

At the moment we can use "fork" command line tool to automate Fork.app (from outside):

usage: fork [<options>] <command> [<args>]

These are common Fork commands:
    fork open       open current repository in Fork
    fork status     open current repository in Fork and show the Commit view
    fork log        open current repository in Fork and show the revision list view
    fork log [-- path]  open current repository in Fork and show file history
    ...

Also we can use "Custom Commands" feature to extend Fork.app functionality:

# 1) Pushes to remote.
# 2) Creates Pull Request on GitHub from current branch into selected branch. Last commit name will be used as PR title.
git push --set-upstream
pr.forBranch -r "$path" -t "${ref}"


But, there are few cases not yet covered – when we need to know details about Fork.app current/internal state and automate it from outside:

Say, I have a command line script which opens/switches full stack environment for the project I am working on. This automation script/flow does the following:

  1. Closes existing instances of VSCode.
  2. Closes all windows/tabs in Fork.app.
  3. Closes iTerm.app tabs.
  4. Closes tabs in a Chrome/Firefox/Safari browsers.

And then:

  1. Opens backend, frontend projects in VSCode. Moves VSCode to certain screen.
  2. Opens in Fork.app needed Git repos.
  3. Opens needed iTerm.app tabs with predefined Profile and Startup command.
  4. Opens needed tabs in a Chrome/Firefox/Safari browsers.
  5. Logs In into Time tracking system.
  6. Opens Bug tracking web app in Opera.app browser.
  7. Opens Slack/Mail applications.
  8. etc.

Currently Fork.app is not supporting AppleScript / JXA automation. Thus, such things as shown below not achievable:

Example JXA file

// File: playground.jxa

console.log("Attempt via App itself:\n")

const app = Application("Fork");
console.log(app.name()); // Fork
console.log(app.version()); // 2.41.2
console.log(app.windows.length); // 0 ;( Not showing currently opened 3 repositories.
console.log(app.documents.length); // 0 ;( Not showing currently opened 3 repositories.

console.log("\nAttempt via System Events:\n")

const appSysEvents = Application("System Events");
const proc = appSysEvents.processes["Fork"];
const numOfWindows = proc.windows.length;
console.log(`Found ${numOfWindows} windows.`);
for (let index = 0; index < numOfWindows; index++) {
  const window = proc.windows[index];
  console.log(`Window name at index ${index}: "${window.name()}".`);
  // window.close() // ;( Not working
}

Sample run: osascript -l JavaScript '/.../playground.jxa'

Output:

Attempt via App itself:

Fork
2.41.2
0
0

Attempt via System Events:

Found 1 windows.
Window name at index 0: "my-repo".

From above example you can see that windows or tabs are not reachable from outside.

Would be nice to get minimal AppleScript / JXA support to have a possibility to open, close, iterate windows or tabs, as well as to get Git repo path of the window or tab.

Thank you in advance!


Here is few resources about Cocoa Scripting and how to implement it: