Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(applescript): add updateConfig command #114

Open
wants to merge 1 commit into
base: meta-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
01F336402AD10D0B0048AF77 /* com.metacubex.ClashX.ProxyConfigHelper in Copy Files */ = {isa = PBXBuildFile; fileRef = F9A7C0692306E874007163C7 /* com.metacubex.ClashX.ProxyConfigHelper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
01FBC6312B9C2B0800810BFF /* ClashProcess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FBC62F2B9C2B0800810BFF /* ClashProcess.swift */; };
491E6203258A424D00313AEF /* CommonUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 491E61FD258A424500313AEF /* CommonUtils.m */; };
8B032B8D2CEF5CB900296514 /* UpdateConfigCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B032B8C2CEF5CB900296514 /* UpdateConfigCommand.swift */; };
F935B2FA23083EE6009E4D33 /* ProxySettingTool.m in Sources */ = {isa = PBXBuildFile; fileRef = F935B2F923083EE6009E4D33 /* ProxySettingTool.m */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -361,6 +362,7 @@
8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = ProxySetting.sdef; sourceTree = "<group>"; };
8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxySettingCommand.swift; sourceTree = "<group>"; };
8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyModeChangeCommand.swift; sourceTree = "<group>"; };
8B032B8C2CEF5CB900296514 /* UpdateConfigCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateConfigCommand.swift; sourceTree = "<group>"; };
F910AA23240134AF00116E95 /* ProxyGroupMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyGroupMenu.swift; sourceTree = "<group>"; };
F915A4612366ADEF004840BE /* ClashConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConnection.swift; sourceTree = "<group>"; };
F9203A25236342820020D57D /* AppDelegate+..swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+..swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -827,6 +829,7 @@
8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */,
8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */,
8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */,
8B032B8C2CEF5CB900296514 /* UpdateConfigCommand.swift */,
);
path = AppleScript;
sourceTree = "<group>";
Expand Down Expand Up @@ -1130,6 +1133,7 @@
01F336192AD10D0B0048AF77 /* UpdateConfigAction.swift in Sources */,
01F3361A2AD10D0B0048AF77 /* NSTableView+Reload.swift in Sources */,
01F3361B2AD10D0B0048AF77 /* ClashProvider.swift in Sources */,
8B032B8D2CEF5CB900296514 /* UpdateConfigCommand.swift in Sources */,
01F3361C2AD10D0B0048AF77 /* SettingTabViewController.swift in Sources */,
01F3361D2AD10D0B0048AF77 /* ClashRule.swift in Sources */,
01F3361E2AD10D0B0048AF77 /* ClashRuleProvider.swift in Sources */,
Expand Down
3 changes: 3 additions & 0 deletions ClashX/AppleScript/ProxySetting.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
<type type="text"/>
</direct-parameter>
</command>
<command name="updateConfig" code="clashupc" description="Update config.">
<cocoa class="ClashX_Meta.UpdateConfigCommand"/>
</command>
</suite>
</dictionary>
12 changes: 12 additions & 0 deletions ClashX/AppleScript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# NOTE

## `code`

code 长度必须是 4 或 8, 否则 `syntax error: 应用程序的词典已损坏。 (-2705)`

## reference

- Apple 的 Scripting Interface Guidelines: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_creating_sdef/SAppsCreateSdef.html
- AppleScript Language Guide: https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html
- NSAppleEventDescriptor 的文档:https://developer.apple.com/documentation/foundation/nsappleeventdescriptor
- Apple Event Programming Guide:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/AppleEvents/AppleEvents.html
22 changes: 22 additions & 0 deletions ClashX/AppleScript/UpdateConfigCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// UpdateConfigCommand.swift
// ClashX Meta
//
// Created by magicdawn on 2024/11/21.
// Copyright © 2024 west2online. All rights reserved.
//

import Foundation
import AppKit

@objc class UpdateConfigCommand: NSScriptCommand {
override func performDefaultImplementation() -> Any? {
guard let delegate = NSApplication.shared.delegate as? AppDelegate else {
scriptErrorNumber = -2
scriptErrorString = "can't get application, try again later"
return nil
}
delegate.actionUpdateConfig(self)
return nil
}
}
13 changes: 9 additions & 4 deletions Shortcuts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ClashX Meta目前仅支持以下功能的AppleScript
1. 打开(关闭)系统代理
2. 切换出站模式
3. 打开(关闭)Tun模式
4. 更新配置

## 通过 Automator 创建全局快捷键

Expand All @@ -16,9 +17,9 @@ ClashX Meta目前仅支持以下功能的AppleScript

[Alfred-Workflow-for-ClashX-Meta](https://github.com/hbsgithub/Alfred-Workflow-for-ClashX-Meta)

## 可用的 AppleScript
## 可用的 AppleScript

你可以在这里选择你需要的 AppleScript 代码,以此创建你需要的快捷键。
你可以在这里选择你需要的 AppleScript 代码,以此创建你需要的快捷键。

**以下示例代码为ClashX Meta程序。如果你正在用ClashX或ClashX Pro,那么请将ClashX Meta替换为 ClashX或ClashX Pro**

Expand All @@ -44,8 +45,12 @@ ClashX Meta目前仅支持以下功能的AppleScript

`tell application "ClashX Meta" to TunMode`

更新配置

`tell application "ClashX Meta" to updateConfig`

## 已知缺陷

1. 通过 Automator 创建全局快捷键的方式无法直接在桌面使用快捷键,你需要进入任意程序中才能启动快捷键
2. 在任何程序中第一次启用该快捷键都要点击一次确认授权才能启动快捷键

2. 在任何程序中第一次启用该快捷键都要点击一次确认授权才能启动快捷键