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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:43:55+00:00 2026-06-03T00:43:55+00:00

Is it possible to predefine constant or variable for an AppleScript in cocoa application?

  • 0

Is it possible to predefine constant or variable for an AppleScript in cocoa application?
in other words is the function “addConstantToAppleScript” (used in the following code) definable?

addConstantToAppleScript("myText", "Hello!");
char *src = "display dialog myText";
NSString *scriptSource = [NSString stringWithCString:src]; 
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:scriptSource];
NSDictionary *scriptError = [[NSDictionary alloc] init];
[appleScript executeAndReturnError:scriptError];

thanks.

  • 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-03T00:43:56+00:00Added an answer on June 3, 2026 at 12:43 am

    If you want to prepend an NSDictionary of key/value pairs to the beginning of an NSString containing AppleScript you could use something like the following function. Personally I would do this as a category on NSString but you have asked for a function.

    NSString *addConstantsToAppleScript(NSString *script, NSDictionary *constants) {
        NSMutableString *constantsScript = [NSMutableString string];
    
        for(NSString *name in constants) {
            [constantsScript appendFormat:@"set %@ to \"%@\"\n", name, [constants objectForKey:name]];
        }   
    
        return [NSString stringWithFormat:@"%@%@", constantsScript, script];
    }
    

    This function converts the key/value pairs to AppleScript statements of the form set <key> to "<value>". These statements are then added to the front of the supplied script string. The resulting script string is then returned.

    You would use the above function as follows:

    // Create a dictionary with two entries:
    //     myText = Hello\rWorld!
    //     Foo    = Bar
    NSDictionary *constants = [[NSDictionary alloc ] initWithObjectsAndKeys:@"Hello\rWorld!", @"myText", @"Bar", @"Foo", nil];
    
    // The AppleScript to have the constants prepended to   
    NSString *script = @"tell application \"Finder\" to display dialog myText";
    
    // Add the constants to the beginning of the script 
    NSString *sourceScript = addConstantsToAppleScript(script, constants);
    
    // sourceScript now equals
    //     set Foo to "Bar"
    //     set myText to "Hello\rWorld!"
    //     tell application "Finder" to display dialog myText
    
    NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:sourceScript];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

kan anyone tell me if/how its possible to predefine some default values when creating
Is it possible to used ggplot to color points based on predefinded standard color
I was wondering if it is possible to drive Flash application programatically to provide
Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
I want to predefine the TEXT-INPUT field - attribute VALUE. Is it possible to
Is it possible to rename a predefined function in JavaScript as we do with
Is it possible to use an enumeration in the following circumstance: Let’s say you
Possible Duplicate: Is there a convenient function in objective-c / coca-touch to find a
Possible Duplicate: How to get pixel data from a UIImage (Cocoa Touch) or CGImage
Is it possible to redefine a constant in php which was defined by the

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.