From 03960ec50f1b60672f91e5d089e86cdd317dd92d Mon Sep 17 00:00:00 2001 From: magicdawn Date: Thu, 21 Nov 2024 22:16:43 +0800 Subject: [PATCH] feat(applescript): add updateConfig command --- ClashX.xcodeproj/project.pbxproj | 4 ++++ ClashX/AppleScript/ProxySetting.sdef | 3 +++ ClashX/AppleScript/README.md | 12 +++++++++++ ClashX/AppleScript/UpdateConfigCommand.swift | 22 ++++++++++++++++++++ Shortcuts.md | 13 ++++++++---- 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 ClashX/AppleScript/README.md create mode 100644 ClashX/AppleScript/UpdateConfigCommand.swift diff --git a/ClashX.xcodeproj/project.pbxproj b/ClashX.xcodeproj/project.pbxproj index 388e55f50..00f51c02e 100644 --- a/ClashX.xcodeproj/project.pbxproj +++ b/ClashX.xcodeproj/project.pbxproj @@ -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 */ @@ -361,6 +362,7 @@ 8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = ProxySetting.sdef; sourceTree = ""; }; 8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxySettingCommand.swift; sourceTree = ""; }; 8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyModeChangeCommand.swift; sourceTree = ""; }; + 8B032B8C2CEF5CB900296514 /* UpdateConfigCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateConfigCommand.swift; sourceTree = ""; }; F910AA23240134AF00116E95 /* ProxyGroupMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyGroupMenu.swift; sourceTree = ""; }; F915A4612366ADEF004840BE /* ClashConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashConnection.swift; sourceTree = ""; }; F9203A25236342820020D57D /* AppDelegate+..swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppDelegate+..swift"; sourceTree = ""; }; @@ -827,6 +829,7 @@ 8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */, 8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */, 8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */, + 8B032B8C2CEF5CB900296514 /* UpdateConfigCommand.swift */, ); path = AppleScript; sourceTree = ""; @@ -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 */, diff --git a/ClashX/AppleScript/ProxySetting.sdef b/ClashX/AppleScript/ProxySetting.sdef index 8cf3ec80d..4862657b9 100644 --- a/ClashX/AppleScript/ProxySetting.sdef +++ b/ClashX/AppleScript/ProxySetting.sdef @@ -14,5 +14,8 @@ + + + diff --git a/ClashX/AppleScript/README.md b/ClashX/AppleScript/README.md new file mode 100644 index 000000000..7a83b01a5 --- /dev/null +++ b/ClashX/AppleScript/README.md @@ -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 diff --git a/ClashX/AppleScript/UpdateConfigCommand.swift b/ClashX/AppleScript/UpdateConfigCommand.swift new file mode 100644 index 000000000..6b4df7800 --- /dev/null +++ b/ClashX/AppleScript/UpdateConfigCommand.swift @@ -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 + } +} diff --git a/Shortcuts.md b/Shortcuts.md index faa8ac50a..703d49123 100644 --- a/Shortcuts.md +++ b/Shortcuts.md @@ -7,6 +7,7 @@ ClashX Meta目前仅支持以下功能的AppleScript 1. 打开(关闭)系统代理 2. 切换出站模式 3. 打开(关闭)Tun模式 +4. 更新配置 ## 通过 Automator 创建全局快捷键 @@ -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** @@ -44,8 +45,12 @@ ClashX Meta目前仅支持以下功能的AppleScript `tell application "ClashX Meta" to TunMode` +更新配置 + +`tell application "ClashX Meta" to updateConfig` + ## 已知缺陷 1. 通过 Automator 创建全局快捷键的方式无法直接在桌面使用快捷键,你需要进入任意程序中才能启动快捷键 - -2. 在任何程序中第一次启用该快捷键都要点击一次确认授权才能启动快捷键 \ No newline at end of file + +2. 在任何程序中第一次启用该快捷键都要点击一次确认授权才能启动快捷键