This code shows how to send a keystroke event for instance Control-Command-1 and Control-Command-0 to Finder with AppleScript. This is equivalent to pressing Finder->View->Arrange By->Name and Finder->View->Arrange By->None
// @Yoolla: rearrangement with AppleScript NSString *source0 = [NSString stringWithFormat:@"\ tell application \"System Events\"\n\ tell process \"Finder\"\n\ keystroke \"1\" using {control down, command down}\n\ end tell\n\ end tell\n"]; NSString *source1 = [NSString stringWithFormat:@"\ tell application \"System Events\"\n\ tell process \"Finder\"\n\ keystroke \"0\" using {control down, command down}\n\ end tell\n\ end tell\n"]; NSAppleScript *run0 = [[NSAppleScript alloc] initWithSource:source0]; NSAppleScript *run1 = [[NSAppleScript alloc] initWithSource:source1]; NSDictionary *error0; __block NSDictionary *error1; [run0 executeAndReturnError:&error0]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [run1 executeAndReturnError:&error1]; }); NSLog(@"error0 for %@ = %@", source0, error0); NSLog(@"error1 for %@ = %@", source1, error1);
That’s it!