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 9121715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:55:30+00:00 2026-06-17T05:55:30+00:00

I am migrating some code that executes AppleScripts from NSAppleScript to NSUserAppleScriptTask so that

  • 0

I am migrating some code that executes AppleScripts from NSAppleScript to NSUserAppleScriptTask so that I can sandbox my app. The problem I am having can be best demonstrated as follows:

The AppleScript “test.scpt” is simply

on run
  display dialog "Hello World" buttons {"OK"} default button "OK"
end run

If I execute this 10 times in a row with NSAppleScript as below, the script is executed 10 times, with each execution waiting for the previous execution to complete.

NSURL *script = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"scpt"]];

for (int i=0; i<10; i++) {

    NSDictionary *error = nil;
    NSAppleScript *task = [[NSAppleScript alloc] initWithContentsOfURL:script error:nil];
    [task executeAndReturnError:&error];
    if (error!=nil) {
        NSLog(@"AppleScript error: %@", error);
    }
    [task release];
}

However using NSUserAppleScriptTask it appears the tasks are executed concurrently. The concurrent executions is a “problem” as it seems that if a previous script has a dialog open, the next script to be executed errors. This can be demonstrated as below:

NSURL *script = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"scpt"]];


for (int i=0; i<10; i++) {

    NSError *error;
    NSUserAppleScriptTask *task = [[NSUserAppleScriptTask alloc] initWithURL:script error:&error];
    [task executeWithCompletionHandler:^(NSError *error) {
        if (error){
            NSLog(@"Script execution failed with error: %@", [error localizedDescription]);
        }
    }];

   [task release];

}

This generates the following error for 9 out of the 10 executions:

execution error: "Hello World" doesn’t understand the «event sysodlog» message. (-1708)

I thought the correct solution would be to queue each operation using gcd or NSOperationQueue, but I’ve not managed to construct a queue that waits for the completion block of the NSUserAppleScriptTask to be executed before it starts the next task.

Can anyone suggest a solution that would give me the same behaviour as the NSAppleScript method gives me?

  • 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-06-17T05:55:31+00:00Added an answer on June 17, 2026 at 5:55 am

    You could run the scripts from a dispatch queue (or operation queue) and use an NSConditionLock to wait for each script to complete.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:0];
    
        NSURL *script = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"scpt"]];
        for (int i=0; i<10; i++) {
            NSError *error;
            NSUserAppleScriptTask *task = [[NSUserAppleScriptTask alloc] initWithURL:script error:&error];
            [task executeWithCompletionHandler:^(NSError *error) {
                if (error){
                    NSLog(@"Script execution failed with error: %@", [error localizedDescription]);
                }
                [lock lock];
                [lock unlockWithCondition:1];
            }];
            [task release];
    
            //This will wait until the completion handler of the script task has run:
            [lock lockWhenCondition:1];
            [lock unlockWithCondition:0];
        }
        [lock release];
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code that I'm migrating from Fortran to C++, and
I am migrating some code from the older azure table storage client to the
I'm migrating some code from the ASPX view engine to Razor and I've run
I am migrating some code from Scala 2.10-M5 to Scala 2.10-M7 . I have
We are migrating some code to use Entity Framework and have a query that
We're converting our Rails 2.3 app to 3.0.11, and were having a problem migrating
I'm currently involved in a project that is migrating some old VB6 code to
I am migrating some spreadsheets from Excel 2003/WinXP to Excel 2010/Win7. Some spreadsheets use
We are having some serious issues migrating Visual FoxPro to SQL Sever.. I don't
I'm migrating some old code to jquery: xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState ==

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.