Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed May 7, 2024
1 parent 03a2411 commit e473c21
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 32 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Wox.Plugin.Template.Nodejs
# Wox.Plugin.Arc

Plugin template for nodejs plugin
A Wox plugin to search tabs of arc browser (only support macos).

# How to Install

Run `wpm install arc` in Wox.
Binary file modified images/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "replace_me_with_name",
"name": "wox.plugin.arc",
"version": "0.0.1",
"scripts": {
"build": "pnpm clean && ncc build src/index.ts -o dist && babel dist --out-dir dist && cp -r images dist && cp -r plugin.json dist",
"package": "pnpm build && cd dist && zip -r ../replace_me_with_name.wox *.js *.json images",
"package": "pnpm build && cd dist && zip -r ../wox.plugin.arc.wox *.js *.json images",
"dev": "nodemon --watch src --watch images --watch plugin.json --ext json,ts,js,mjs,png --exec pnpm run build",
"clean": "node -e \"var { rmdirSync, existsSync } = require('fs'), path = require('path'); ['./dist'].forEach(fPath => {if (existsSync(path.join(__dirname, fPath))) rmdirSync(path.join(__dirname, fPath), { recursive: true })}); process.exit(0);\"",
"clean:all": "pnpm run clean && (rm -r ./node_modules || true)",
Expand All @@ -24,6 +24,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@wox-launcher/wox-plugin": "^0.0.57"
"@wox-launcher/wox-plugin": "^0.0.63",
"run-applescript": "^7.0.0"
}
}
17 changes: 8 additions & 9 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"Id": "[Id]",
"Id": "84cb8c8f-1156-489d-a490-6d04fbc85d52",
"TriggerKeywords": [
"[Trigger Keyword]"
"*",
"arc"
],
"Name": "[Name]",
"Description": "[Description]",
"Author": "[Author Name]",
"Name": "Arc",
"Description": "Search arc tabs and spaces",
"Author": "Wox-team",
"Version": "0.0.1",
"MinWoxVersion": "2.0.0",
"Runtime": "[Runtime]",
"Website": "[Website]",
"Runtime": "nodejs",
"Website": "https://github.com/Wox-launcher/Wox.Plugin.Arc",
"Entry": "index.js",
"Icon": "relative:images/app.png",
"SupportedOS": [
"windows",
"linux",
"darwin"
]
}
16 changes: 12 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

226 changes: 226 additions & 0 deletions src/arc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Source: https://github.com/raycast/extensions/tree/94a85235fc605b1ad5eb75ff13bb3fad742b4b07/extensions/arc/

import { runAppleScript } from "run-applescript"
import { Space, Tab } from "./types.js"

export async function getTabs() {
const response = await runAppleScript(`
on escape_value(this_text)
set AppleScript's text item delimiters to the "\\""
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the "\\\\\\""
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
set _output to ""
tell application "Arc"
set _window_index to 1
set _tab_index to 1
repeat with _tab in tabs of first window
set _title to my escape_value(get title of _tab)
set _url to get URL of _tab
set _location to get location of _tab
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"url\\": \\"" & _url & "\\", \\"windowId\\": " & _window_index & ", \\"tabId\\": " & _tab_index & " , \\"location\\": \\"" & _location & "\\" }")
if _tab_index < (count tabs of first window) then
set _output to (_output & ",\\n")
else
set _output to (_output & "\\n")
end if
set _tab_index to _tab_index + 1
end repeat
end tell
return "[\\n" & _output & "\\n]"
`)

return response ? (JSON.parse(response) as Tab[]) : undefined
}

export async function findTab(url: string) {
const response = await runAppleScript(`
on escape_value(this_text)
set AppleScript's text item delimiters to the "\\""
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the "\\\\\\""
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
set _output to ""
tell application "Arc"
set _window_index to 1
repeat with _window in windows
set _tab_index to 1
repeat with _tab in tabs of _window
set _url to get URL of _tab
if _url is equal "${url}" then
set _title to my escape_value(get title of _tab)
set _location to get location of _tab
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"url\\": \\"" & _url & "\\", \\"windowId\\": " & _window_index & ", \\"tabId\\": " & _tab_index & " , \\"location\\": \\"" & _location & "\\" }")
return _output
end if
set _tab_index to _tab_index + 1
end repeat
set _window_index to _window_index + 1
end repeat
end tell
return _output
`)

return response ? (JSON.parse(response) as Tab) : undefined
}

export async function selectTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to select
end tell
activate
end tell
`)
}

export async function closeTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to close
end tell
end tell
`)
}

export async function reloadTab({ windowId, tabId }: Pick<Tab, "windowId" | "tabId">) {
await runAppleScript(`
tell application "Arc"
tell window (${windowId} as number)
tell tab (${tabId} as number) to reload
end tell
end tell
`)
}

export async function makeNewTab(url: string) {
await runAppleScript(`
tell application "Arc"
tell front window
make new tab with properties {URL:"${url}"}
end tell
activate
end tell
`)
}

export type MakeNewWindowOptions = {
incognito?: boolean;
url?: string;
};

export async function makeNewWindow(options: MakeNewWindowOptions = {}): Promise<void> {
await runAppleScript(`
tell application "Arc"
make new window with properties {incognito:${options.incognito ?? false}}
activate
${
options.url
? `tell front window to make new tab with properties {URL:"${options.url}"}`
: ""
}
end tell
`)
}

export async function makeNewLittleArcWindow(url: string) {
await runAppleScript(`
tell application "Arc"
make new tab with properties {URL:"${url}"}
activate
end tell
`)
}

export async function makeNewTabWithinSpace(url: string, space: Space) {
await runAppleScript(`
tell application "Arc"
tell front window
tell space ${space.id}
make new tab with properties {URL:"${url}"}
end tell
end tell
activate
end tell
`)
}

export async function selectSpace(id: Space["id"]) {
await runAppleScript(`
launch app "Arc"
delay "1"
tell application "Arc"
tell front window
tell space ${id} to focus
end tell
end tell
`)
}

export async function getSpaces() {
const response = await runAppleScript(`
set _output to ""
tell application "Arc"
set _space_index to 1
repeat with _space in spaces of front window
set _title to get title of _space
set _output to (_output & "{ \\"title\\": \\"" & _title & "\\", \\"id\\": " & _space_index & " }")
if _space_index < (count spaces of front window) then
set _output to (_output & ",\\n")
else
set _output to (_output & "\\n")
end if
set _space_index to _space_index + 1
end repeat
end tell
return "[\\n" & _output & "\\n]"
`)

return response ? (JSON.parse(response) as Space[]) : undefined
}

export async function getVersion() {
const response = await runAppleScript(`
tell application "Arc"
return version
end tell
`)

return response
}
41 changes: 27 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import type { Plugin, PluginInitContext, PublicAPI, Query, Result } from "@wox-launcher/wox-plugin"
import type { Context, Plugin, PluginInitParams, PublicAPI, Query, Result } from "@wox-launcher/wox-plugin"
import { getTabs, selectTab } from "./arc"
import { Tab } from "./types"

let api: PublicAPI
let tabs: Tab[]

export const plugin: Plugin = {
init: async (context: PluginInitContext) => {
api = context.API
await api.Log("Info", "Init finished")
init: async (ctx: Context, initParams: PluginInitParams) => {
api = initParams.API

setInterval(async () => {
const openTabs = await getTabs()
if (openTabs == undefined) {
return
}

tabs = openTabs
}, 2000)
},

query: async (query: Query): Promise<Result[]> => {
return [
{
Title: "Hello World",
SubTitle: "This is a subtitle",
query: async (ctx: Context, query: Query): Promise<Result[]> => {
if (tabs == undefined || tabs.length == 0) {
return []
}

return tabs.filter(o => o.title.toLowerCase().includes(query.Search.toLowerCase()) || o.url.toLowerCase().includes(query.Search.toLowerCase())).map(tab => {
return {
Title: tab.title,
SubTitle: tab.url,
Icon: {
ImageType: "relative",
ImageData: "images/app.png"
Expand All @@ -21,14 +36,12 @@ export const plugin: Plugin = {
{
Name: "Open",
Action: async () => {
await api.ChangeQuery({
QueryType: "input",
QueryText: "Hello World!"
})
await selectTab(tab)
await api.Log(ctx, "Info", `Opening tab: ${tab.title}`)
}
}
]
}
]
})
}
}
Loading

0 comments on commit e473c21

Please sign in to comment.