Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6628025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:04:58+00:00 2026-05-25T22:04:58+00:00

I am trying to run this shell script command through applescript on my mac

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:04:59+00:00Added an answer on May 25, 2026 at 10:04 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run this dreadfully simple command in Bash java -cp nasa-top-secret.jar
I'm trying to run this script: import re, os def build_pool(cwd): global xtn_pool, file_pool
I am trying to run this SQL from ASP.NET 2005 but am getting an
i just installed python i am trying to run this script: import csv reader
I'm trying to run the following line of script in bash on Mac OS
I am trying to run a shell script from within a .vimrc file (three
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
I'm trying to run this for loop; for (int col= 0; grid[0].length; col++) However
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
I am getting this error when trying to run internet information services on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.