Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript from Mac App says "Expected end of line but found \U201c\"\U201d."

I am trying to perform a copy/paste for my to the the last active app, here's my code:

NSString *appleScriptSource = [NSString stringWithFormat:@"\ntell application \"%@\" to activate\ntell application \"System Events\" to tell process \"%@\"\nkeystroke \"v\" using command down\nend tell", [lastApp localizedName], [lastApp localizedName]];

NSDictionary *error;
NSAppleScript *aScript = [[NSAppleScript alloc] initWithSource:appleScriptSource];
NSAppleEventDescriptor *aDescriptor = [aScript executeAndReturnError:&error];

The problem is that on some computers it works just fine, but on others it fails. My error output from error that is returned by executeAndReturnError is:

2012-06-13 17:43:19.875 Mini Translator[1206:303] (null) (error: {
    NSAppleScriptErrorBriefMessage = "Expected end of line but found \U201c\"\U201d.";
    NSAppleScriptErrorMessage = "Expected end of line but found \U201c\"\U201d.";
    NSAppleScriptErrorNumber = "-2741";
    NSAppleScriptErrorRange = "NSRange: {95, 1}";
})

I can't seem to figure out what it means or why it happens.

We tried copying the generated apple-script code into the Apple Script editor, and here it works just fine.

My App is sandboxed - i have added the bundle identifiers for the key "com.apple.security.temporary-exception.apple-events" for the apps i want to support.

Any suggestions?

like image 279
Rasmus Styrk Avatar asked Jun 13 '12 15:06

Rasmus Styrk


1 Answers

I am guessing that the \u201c and \u201d are red herrings and just represent smart quotes around a double quote in the error message produced by apple script, and your issue lies with the localized name of the last application you are formatting into the script. I am not sure why you might see this on one machine and not another.

For example if the name was 'Some " App' then the double quotes would end up mismatched as it would end up injected into the middle of a double quoted string. You might want to try and replace any double quotes in the name with '\"' which will escape them.

e.g.

NSString *esc = [[lastApp localizedName] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
like image 160
tyranid Avatar answered Oct 22 '22 13:10

tyranid