Question
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?
Answer - 1
\U201c and \u201d both represent quotes (left/right respectively). Typing quotes on a keyboard So it makes me believe that you aren’t escaping the quote " correctly. Play around with the format of the string and the characters you’re escaping in it. – erran Jun 13 '12 at 16:03
Well i have translated the unicodes but i can’t really figure out what goes wrong. I have tried playing with the apple script but it does not work - the weird part is that it works on some macs. – Rasmus Styrk Jun 13 '12 at 16:23
Even doing simple applescripts like “active this app” produces the same error. – Rasmus Styrk Jun 13 '12 at 16:24
link Looks like you need escape quotes multiple times. – erran Jun 13 '12 at 16:28
The multi escape are for the AppleScript command “do shell script” only. – jackjr300 Jun 13 '12 at 21:24
Answer - 2
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:@"\\\""];
this answer is applicable too when addressing quote issues in json. the unicodes were indeed red herrings. – Brandt Solovij May 18 '15 at 22:03
The ‘red herring’ in my case was a missing \n before ‘end tell’ – Keith John Hutchison Feb 2 '16 at 5:28