-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathTKRemoteControlController.m
More file actions
88 lines (79 loc) · 4.04 KB
/
TKRemoteControlController.m
File metadata and controls
88 lines (79 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// TKRemoteControlController.m
// WeChatPlugin
//
// Created by TK on 2017/8/8.
// Copyright © 2017年 tk. All rights reserved.
//
#import "TKRemoteControlController.h"
#import "TKWeChatPluginConfig.h"
#import "TKRemoteControlModel.h"
#import "WeChatPlugin.h"
// 执行 AppleScript
static NSString * const kRemoteControlAppleScript = @"osascript /Applications/WeChat.app/Contents/MacOS/WeChatPlugin.framework/Resources/TKRemoteControlScript.scpt";
@implementation TKRemoteControlController
+ (void)executeRemoteControlCommandWithMsg:(NSString *)msg {
NSArray *remoteControlModels = [TKWeChatPluginConfig sharedConfig].remoteControlModels;
[remoteControlModels enumerateObjectsUsingBlock:^(NSArray *subModels, NSUInteger index, BOOL * _Nonnull stop) {
[subModels enumerateObjectsUsingBlock:^(TKRemoteControlModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
if (model.enable && ![model.keyword isEqualToString:@""] && ([msg isEqualToString:model.keyword] || [msg isEqualToString:model.command])) {
if ([model.function isEqualToString:@"屏幕保护"] || [model.function isEqualToString:@"锁屏"]) {
// 屏幕保护 & 锁屏 通过 Shell 命令来执行即可
[self executeShellCommand:model.executeCommand];
} else {
// 拼接相关参数,执行 AppleScript
NSString *command = [NSString stringWithFormat:@"%@ %@",kRemoteControlAppleScript, model.executeCommand];
[self executeShellCommand:command];
// bug: 有些程序在第一次时会无法关闭,需要再次关闭
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if ([model.function isEqualToString:@"退出所有程序"]) {
NSString *command = [NSString stringWithFormat:@"%@ %@",kRemoteControlAppleScript, model.executeCommand];
[self executeShellCommand:command];
}
});
}
if (![msg hasPrefix:@"#remote."]) {
NSString *currentUserName = [objc_getClass("CUtility") GetCurrentUserName];
NSString *callBack = [NSString stringWithFormat:@"小助手收到一条指令:%@",model.function];
MessageService *service = [[objc_getClass("MMServiceCenter") defaultCenter] getService:objc_getClass("MessageService")];
[service SendTextMessage:currentUserName toUsrName:currentUserName msgText:callBack atUserList:nil];
}
}
}];
}];
}
/**
通过 NSTask 执行 Shell 命令
@param cmd Terminal命令
*/
+ (void)executeShellCommand:(NSString *)cmd {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
[task setArguments:@[@"-c", cmd]];
[task launch];
}
+ (NSString *)remoteControlCommandsString {
NSMutableString *replyContent = [NSMutableString stringWithFormat:@"远程控制指令:\n(功能-指令-是否开启)\n\n"];
NSArray *remoteControlModels = [TKWeChatPluginConfig sharedConfig].remoteControlModels;
[remoteControlModels enumerateObjectsUsingBlock:^(NSArray *subModels, NSUInteger index, BOOL * _Nonnull stop) {
switch (index) {
case 0:
[replyContent appendString:@"macbook控制:\n"];
break;
case 1:
[replyContent appendString:@"app控制:\n"];
break;
case 2:
[replyContent appendString:@"网易云音乐控制:\n"];
break;
default:
break;
}
[subModels enumerateObjectsUsingBlock:^(TKRemoteControlModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
[replyContent appendFormat:@"%@-%@-%@\n", model.function, model.keyword, model.enable ? @"开启":@"关闭"];
}];
[replyContent appendString:@"\n"];
}];
return replyContent;
}
@end