开发者

Applescript in Objective C project

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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜