Skip to content

Commit

Permalink
Add support for AppleScript
Browse files Browse the repository at this point in the history
  • Loading branch information
vince-hz authored and yichengchen committed Mar 20, 2022
1 parent 87041b5 commit 5d3e9c6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ClashX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176A62355FE680093DD7B /* NetworkChangeNotifier.swift */; };
49D176A9235614340093DD7B /* ProxyGroupSpeedTestMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176A8235614340093DD7B /* ProxyGroupSpeedTestMenuItem.swift */; };
49D176AB23575BB20093DD7B /* ProxyGroupMenuItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49D176AA23575BB20093DD7B /* ProxyGroupMenuItemView.swift */; };
8A2BBEA727A03ACB0081EBEF /* ProxySetting.sdef in Resources */ = {isa = PBXBuildFile; fileRef = 8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */; };
8ACD21BB27A04C7800BC4632 /* ProxySettingCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */; };
8ACD21BD27A04ED500BC4632 /* ProxyModeChangeCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */; };
9E0E2F1481CB327D3753969D /* libPods-ClashX.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CAE7981A9F34B5E210C549CB /* libPods-ClashX.a */; };
F910AA24240134AF00116E95 /* ProxyGroupMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = F910AA23240134AF00116E95 /* ProxyGroupMenu.swift */; };
F915A4622366ADEF004840BE /* ClashConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = F915A4612366ADEF004840BE /* ClashConnection.swift */; };
Expand Down Expand Up @@ -187,6 +190,9 @@
49D176A8235614340093DD7B /* ProxyGroupSpeedTestMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyGroupSpeedTestMenuItem.swift; sourceTree = "<group>"; };
49D176AA23575BB20093DD7B /* ProxyGroupMenuItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyGroupMenuItemView.swift; sourceTree = "<group>"; };
5217C006C5A22A1CEA24BFC1 /* Pods-ClashX.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClashX.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ClashX/Pods-ClashX.debug.xcconfig"; sourceTree = "<group>"; };
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>"; };
A1485BCE642059532D01B8BA /* Pods-ClashX.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ClashX.release.xcconfig"; path = "Pods/Target Support Files/Pods-ClashX/Pods-ClashX.release.xcconfig"; sourceTree = "<group>"; };
CAE7981A9F34B5E210C549CB /* libPods-ClashX.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ClashX.a"; sourceTree = BUILT_PRODUCTS_DIR; };
F910AA23240134AF00116E95 /* ProxyGroupMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProxyGroupMenu.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -417,6 +423,7 @@
49CF3B1F20CD7463001EBF94 /* ClashX */ = {
isa = PBXGroup;
children = (
8A2BBEA527A03A920081EBEF /* AppleScript */,
49722FDD211ED2A900650A41 /* Vendor */,
4913C82021157CEB00F6B87C /* Macro */,
4997732220D251A60009B136 /* Basic */,
Expand Down Expand Up @@ -456,6 +463,16 @@
name = Pods;
sourceTree = "<group>";
};
8A2BBEA527A03A920081EBEF /* AppleScript */ = {
isa = PBXGroup;
children = (
8A2BBEA627A03ACB0081EBEF /* ProxySetting.sdef */,
8ACD21BA27A04C7800BC4632 /* ProxySettingCommand.swift */,
8ACD21BC27A04ED500BC4632 /* ProxyModeChangeCommand.swift */,
);
path = AppleScript;
sourceTree = "<group>";
};
CF1AC9FACC36FCE7663C5583 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -588,6 +605,7 @@
49761DA721C9497000AE13EF /* dashboard in Resources */,
4929F677258CD89B00A435F6 /* Country.mmdb.gz in Resources */,
4981C88B216BAE4A008CC14A /* Localizable.strings in Resources */,
8A2BBEA727A03ACB0081EBEF /* ProxySetting.sdef in Resources */,
F9FAB31E262BE04800DE02A6 /* Images.xcassets in Resources */,
495340B020DE5F7200B0D3FF /* StatusItemView.xib in Resources */,
49CF3B2820CD7465001EBF94 /* Main.storyboard in Resources */,
Expand Down Expand Up @@ -697,6 +715,7 @@
49722FEF211F338B00650A41 /* FileEvent.swift in Sources */,
49D176A72355FE680093DD7B /* NetworkChangeNotifier.swift in Sources */,
4913C82321157D0200F6B87C /* Notification.swift in Sources */,
8ACD21BD27A04ED500BC4632 /* ProxyModeChangeCommand.swift in Sources */,
49228457270AADE20027A4B6 /* RemoteConfigUpdateIntervalSettingView.swift in Sources */,
F9203A26236342820020D57D /* AppDelegate+..swift in Sources */,
499A485C22ED793C00F6C675 /* NSView+Nib.swift in Sources */,
Expand All @@ -707,6 +726,7 @@
F9E754D0239CC21F00CEE7CC /* WebPortalManager.swift in Sources */,
495BFB8821919B9800C8779D /* RemoteConfigManager.swift in Sources */,
4982F51F2344A216008804B0 /* Cgo+Convert.swift in Sources */,
8ACD21BB27A04C7800BC4632 /* ProxySettingCommand.swift in Sources */,
49722FF1211F338B00650A41 /* Witness.swift in Sources */,
49722FF0211F338B00650A41 /* EventStream.swift in Sources */,
499A486522EEA3FD00F6C675 /* Array+Safe.swift in Sources */,
Expand Down
38 changes: 38 additions & 0 deletions ClashX/AppleScript/ProxyModeChangeCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ProxyModeChangeCommand.swift
// ClashX
//
// Created by Vince-hz on 2022/1/25.
// Copyright © 2022 west2online. All rights reserved.
//

import Foundation
import AppKit

@objc class ProxyModeChangeCommand: NSScriptCommand {
override func performDefaultImplementation() -> Any? {
guard let directParameter = directParameter as? String,
let mode = ClashProxyMode(rawValue: directParameter)
else {
scriptErrorNumber = -1
scriptErrorString = "please enter a valid parameter. rule, global or direct"
return nil
}
guard let delegate = NSApplication.shared.delegate as? AppDelegate else {
scriptErrorNumber = -2
scriptErrorString = "can't get application, try again later"
return nil
}
let menuItem: NSMenuItem
switch mode {
case .rule:
menuItem = delegate.proxyModeRuleMenuItem
case .global:
menuItem = delegate.proxyModeGlobalMenuItem
case .direct:
menuItem = delegate.proxyModeDirectMenuItem
}
delegate.actionSwitchProxyMode(menuItem)
return nil
}
}
15 changes: 15 additions & 0 deletions ClashX/AppleScript/ProxySetting.sdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude" title="ClashX Terminonoly">
<suite name="Proxy Suite" code="????" description="Proxy setting for AppleScript.">
<command name="toggleProxy" code="clashtog" description="Toggle the system proxy.">
<cocoa class="ClashX.ProxySettingCommand"/>
</command>
<command name="proxyMode" code="clashmod" description="Update proxy mode.">
<cocoa class="ClashX.ProxyModeChangeCommand"/>
<direct-parameter description="The proxy mode to set. direct, global, or rule">
<type type="text"/>
</direct-parameter>
</command>
</suite>
</dictionary>
22 changes: 22 additions & 0 deletions ClashX/AppleScript/ProxySettingCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ProxySettingCommand.swift
// ClashXX
//
// Created by Vince-hz on 2022/1/25.
// Copyright © 2022 west2online. All rights reserved.
//

import Foundation
import AppKit

@objc class ProxySettingCommand: 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.actionSetSystemProxy(self)
return nil
}
}
4 changes: 4 additions & 0 deletions ClashX/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSAppleScriptEnabled</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2021年 yichengchen. All rights reserved.</string>
<key>NSMainStoryboardFile</key>
Expand All @@ -109,6 +111,8 @@
<string>Any</string>
</dict>
</dict>
<key>OSAScriptingDefinition</key>
<string>ProxySetting.sdef</string>
<key>SMAuthorizedClients</key>
<array>
<string>identifier "com.west2online.ClashX" and anchor apple generic and certificate leaf[subject.CN] = "Mac Developer: chen yicheng (96U846XGYH)" and certificate 1[field.1.2.840.113635.100.6.2.1] /* exists */</string>
Expand Down

0 comments on commit 5d3e9c6

Please sign in to comment.