Reputation: 91
This is the code which i am using:
NSDictionary *errorInfo=nil;
NSString *source=@"tell application \"Mail\"\nget name of mailbox of every account\nend tell";
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *aDescriptor=[[NSAppleEventDescriptor alloc]init];
aDescriptor=[run executeAndReturnError:&errorInfo];
[aDescriptor coerceToDescriptorType:'utxt'];
NSLog(@"result:%@",[aDescriptor stringValue]);
Output which i got: result:(null)
Please help me anyone on this.Thanks in advance:)
Upvotes: 3
Views: 1823
Reputation: 6040
Swift version, tested in 2022:
func run(appleScript: String) {
var error: NSDictionary? = nil
if let scriptObject = NSAppleScript(source: appleScript) {
let output = scriptObject.executeAndReturnError(&error)
// Print All Values
let numberOfItems = output.numberOfItems
print("numberOfItems: \(numberOfItems)")
for i in 0..<numberOfItems {
let innerDescriptor = output.atIndex(i)
print("\(i): " + (innerDescriptor?.stringValue ?? "nil"))
}
// Catch Error
if let error = error {
print("Error: '\(error)'")
}
} else {
print("Error: Unable to init NSAppleScript")
}
}
Upvotes: 0
Reputation: 28242
IIRC that will return a list descriptor filled with list descriptors. You need to iterate over them and pull out the info you want. You're also initializing a descriptor and then immediately overwriting its pointer. Do something like (untested):
NSDictionary *errorInfo = nil;
NSString *source = @"tell application \"Mail\"\nget name of mailbox of every account\nend tell";
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *aDescriptor = [run executeAndReturnError:&errorInfo];
NSInteger num = [aDescriptor numberOfItems];
// Apple event descriptor list indexes are one-based!
for (NSInteger idx = 1; idx <= num; ++idx) {
NSAppleEventDescriptor *innerListDescriptor = [aDescriptor descriptorAtIndex:idx];
NSInteger innerNum = [innerListDescriptor numberOfItems];
for (NSInteger innerIdx = 1; innerIdx <= innerNum; ++innerIdx) {
NSString *str = [[innerListDescriptor descriptorAtIndex:innerIdx] stringValue];
// Do something with str here
}
}
Upvotes: 3