Objective C 项目中的 Applescript

发布于 2024-12-07 14:35:04 字数 5299 浏览 0 评论 0原文

我正在尝试在我的 mac 应用程序上通过 applescript 运行此 shell 脚本命令。它在 applescript 编辑器中工作正常,但是当我在 xcode 中运行它时,如下所示,它不起作用。我做错了吗?

NSString *asString = [[NSString alloc] initWithFormat:@"property MACaddr : \"gg:gg:gg:gg:gg:gg\"\n property WAN_IP_address : \"255.255.255.255\"\n property port_number : \"9\"\n "
                    "on run\n set command to \"/usr/bin/php -r \" & quoted form of (\"$mac = \" & quoted form of MACaddr & \"; $porttemp = \" & quoted form of port_number & \";$ip = \" & quoted form of WAN_IP_address & \"; \" & \"" 
                                        "$mac_bytes = explode(\\\":\\\", $mac); "
                                          " $mac_addr = \\\"\\\"; "
                                          " for ($i=0; $i<6; $i++) "
                                          " $mac_addr .= chr(hexdec($mac_bytes[$i]));" 
                                          " $packet = \\\"\\\"; "
                                          " for ($i=0; $i<6; $i++)   "
                                          " $packet .= chr(255); "
                                           "for ($i=0; $i<16; $i++)  "
                                           "$packet .= $mac_addr;" 
                                          " $port = $porttemp; "
                                           "$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);" 
                                           "socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);" 
                                           "socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);" 
                                           "socket_close($sock);\") "
                                           "do shell script command \n"
                                            "end run"   ];
NSLog(@"the applescript %@", asString);
NSAppleScript *asScript = [[NSAppleScript alloc] initWithSource:asString];
[asScript executeAndReturnError:nil];
[asString release];
[asScript release];

这是在我的 Applescript 编辑器中运行良好的确切 applescript。我已经用正确的反斜杠编辑了上面的部分,并且它与我的 applescirpt 的工作原理相同。但是,它在 xcode 中仍然不起作用,并且魔术包没有被发送。 (使用wireshark来监控这一点。)任何想法出了什么问题?我什至添加了运行部分。

property MACaddr : "gg:gg:gg:g4:g5:gg"
property WAN_IP_address : "255.255.255.255"
property port_number : "9"

on run
    set command to "/usr/bin/php -r " & quoted form of ("$mac = " & quoted form of     MACaddr & "; $porttemp = " & quoted form of port_number & ";$ip = " & quoted form of WAN_IP_address & "; " & " 


$mac_bytes = explode(\":\", $mac); 
$mac_addr = \"\"; 
for ($i=0; $i<6; $i++) 
$mac_addr .= chr(hexdec($mac_bytes[$i])); 
$packet = \"\"; 
for ($i=0; $i<6; $i++)  /*6x 0xFF*/ 
$packet .= chr(255); 
for ($i=0; $i<16; $i++) /*16x MAC address*/ 
$packet .= $mac_addr; 

$port = $porttemp; 
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE); 
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); 
socket_close($sock); 
")
    do shell script command
end run

不管怎样,我终于让它工作了。不知道它到底是什么,但这里的代码是为那些想要通过 PHP 在 Objective C 环境中通过 Applescript 作为 shell 脚本运行 WOL(局域网唤醒)魔术包的 php 脚本的人提供的。是的。这是经过编辑和工作的:

NSString *asString = [[NSString alloc] initWithFormat:@"property MACaddr : \"gg:gg:gg:gg:gg:gg\"\n property WAN_IP_address : \"255.255.255.255\"\n property port_number : \"9\"\n "
                    "on run\n set command to \"/usr/bin/php -r \" & quoted form of (\"$mac = \" & quoted form of MACaddr & \"; $porttemp = \" & quoted form of port_number & \";$ip = \" & quoted form of WAN_IP_address & \"; \" & \"\n" 
                                        "$mac_bytes = explode(\\\":\\\", $mac);\n "
                                          " $mac_addr = \\\"\\\";\n "
                                          " for ($i=0; $i<6; $i++) "
                                          " $mac_addr .= chr(hexdec($mac_bytes[$i]));\n" 
                                          " $packet = \\\"\\\";\n "
                                          " for ($i=0; $i<6; $i++)\n     "
                                          " $packet .= chr(255);\n "
                                           "for ($i=0; $i<16; $i++)\n    "
                                           "$packet .= $mac_addr;\n" 
                                          " $port = $porttemp;\n "
                                           "$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);\n" 
                                           "socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);\n" 
                                           "socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);\n" 
                                           "socket_close($sock);\")\n "
                                           "do shell script command \n"
                                            "end run"   ];
NSLog(@"the applescript %@", asString);
NSString *script2 = [asString stringByReplacingOccurrencesOfString:@"gg:gg:gg:gg:gg:gg" withString:AirportMAC];
NSAppleScript *asScript = [[NSAppleScript alloc] initWithSource:script2];
[asScript executeAndReturnError:nil];
[asString release];
[asScript release];

非常感谢大家的帮助!

I am trying to run this shell script command through applescript on my mac application. It works fine in the applescript editor but when I run it in xcode as shown below, it does not work. Am I doing it wrong?

NSString *asString = [[NSString alloc] initWithFormat:@"property MACaddr : \"gg:gg:gg:gg:gg:gg\"\n property WAN_IP_address : \"255.255.255.255\"\n property port_number : \"9\"\n "
                    "on run\n set command to \"/usr/bin/php -r \" & quoted form of (\"$mac = \" & quoted form of MACaddr & \"; $porttemp = \" & quoted form of port_number & \";$ip = \" & quoted form of WAN_IP_address & \"; \" & \"" 
                                        "$mac_bytes = explode(\\\":\\\", $mac); "
                                          " $mac_addr = \\\"\\\"; "
                                          " for ($i=0; $i<6; $i++) "
                                          " $mac_addr .= chr(hexdec($mac_bytes[$i]));" 
                                          " $packet = \\\"\\\"; "
                                          " for ($i=0; $i<6; $i++)   "
                                          " $packet .= chr(255); "
                                           "for ($i=0; $i<16; $i++)  "
                                           "$packet .= $mac_addr;" 
                                          " $port = $porttemp; "
                                           "$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);" 
                                           "socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);" 
                                           "socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);" 
                                           "socket_close($sock);\") "
                                           "do shell script command \n"
                                            "end run"   ];
NSLog(@"the applescript %@", asString);
NSAppleScript *asScript = [[NSAppleScript alloc] initWithSource:asString];
[asScript executeAndReturnError:nil];
[asString release];
[asScript release];

Here is the exact applescript that runs fine in my Applescript editor. I have edited the above part with the correct backslashes and all and its the same as my applescirpt which works. However, it still does not work in xcode and the magic packet is not being sent. (using wireshark to monitor this.) any ideas whats wrong? I even added the on run part.

property MACaddr : "gg:gg:gg:g4:g5:gg"
property WAN_IP_address : "255.255.255.255"
property port_number : "9"

on run
    set command to "/usr/bin/php -r " & quoted form of ("$mac = " & quoted form of     MACaddr & "; $porttemp = " & quoted form of port_number & ";$ip = " & quoted form of WAN_IP_address & "; " & " 


$mac_bytes = explode(\":\", $mac); 
$mac_addr = \"\"; 
for ($i=0; $i<6; $i++) 
$mac_addr .= chr(hexdec($mac_bytes[$i])); 
$packet = \"\"; 
for ($i=0; $i<6; $i++)  /*6x 0xFF*/ 
$packet .= chr(255); 
for ($i=0; $i<16; $i++) /*16x MAC address*/ 
$packet .= $mac_addr; 

$port = $porttemp; 
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE); 
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); 
socket_close($sock); 
")
    do shell script command
end run

anyways, I have got it working FINALLY. no idea what exactly it was but here is the code for someone who wants to run a php script for WOL(wake on lan) magic packet through PHP as a shell script by applescript in a objective c environment. yup. here is the editied and working one:

NSString *asString = [[NSString alloc] initWithFormat:@"property MACaddr : \"gg:gg:gg:gg:gg:gg\"\n property WAN_IP_address : \"255.255.255.255\"\n property port_number : \"9\"\n "
                    "on run\n set command to \"/usr/bin/php -r \" & quoted form of (\"$mac = \" & quoted form of MACaddr & \"; $porttemp = \" & quoted form of port_number & \";$ip = \" & quoted form of WAN_IP_address & \"; \" & \"\n" 
                                        "$mac_bytes = explode(\\\":\\\", $mac);\n "
                                          " $mac_addr = \\\"\\\";\n "
                                          " for ($i=0; $i<6; $i++) "
                                          " $mac_addr .= chr(hexdec($mac_bytes[$i]));\n" 
                                          " $packet = \\\"\\\";\n "
                                          " for ($i=0; $i<6; $i++)\n     "
                                          " $packet .= chr(255);\n "
                                           "for ($i=0; $i<16; $i++)\n    "
                                           "$packet .= $mac_addr;\n" 
                                          " $port = $porttemp;\n "
                                           "$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);\n" 
                                           "socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);\n" 
                                           "socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);\n" 
                                           "socket_close($sock);\")\n "
                                           "do shell script command \n"
                                            "end run"   ];
NSLog(@"the applescript %@", asString);
NSString *script2 = [asString stringByReplacingOccurrencesOfString:@"gg:gg:gg:gg:gg:gg" withString:AirportMAC];
NSAppleScript *asScript = [[NSAppleScript alloc] initWithSource:script2];
[asScript executeAndReturnError:nil];
[asString release];
[asScript release];

Thank you all so much for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-12-14 14:35:04

永远不要这样做:

NSLog([asScript stringValue]);

总是这样做:

NSLog("%@",[asScript stringValue]);

将未知格式字符串传递给 NSLog() 是通往崩溃箱的快速路径。

Never do this:

NSLog([asScript stringValue]);

Always do this:

NSLog("%@",[asScript stringValue]);

Passing unknown format strings to NSLog() is a quick path to the crash bin.

爱你不解释 2024-12-14 14:35:04

绝对是反斜杠字符的问题。 NSLog 你的 asString 变量并尝试在编辑器中运行打印的代码,你会看到错误。

如果字符串中有引号,则需要三个反斜杠!

"$mac_bytes = explode(\":\", $mac); "

应该是

"$mac_bytes = explode(\\\":\\\", $mac); "

,其他地方也是如此。

Definitely an issue with the backslashed characters. NSLog your asString variable and try running that printed code in the editor and you'll see the error.

Where you have quotes within the string, you'll need triple backslashes!

"$mac_bytes = explode(\":\", $mac); "

should be

"$mac_bytes = explode(\\\":\\\", $mac); "

and in other places as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文