gpt4 book ai didi

objective-c - 在 Objective-C 中自动点击 iTunes 鼠标

转载 作者:行者123 更新时间:2023-12-03 17:25:27 25 4
gpt4 key购买 nike

我正在尝试从 Objective-C 自动化在 iTunes 中单击鼠标左键。我正在执行以下操作。

  1. 首先我正在收听 iTunes 事件

    [[NSDistributedNotificationCenter defaultCenter] addObserver:self
    selector:@selector(allDistributedNotifications:)
    name:nil
    object:nil];
  2. 当调用 allDistributedNotifications 时,我会执行以下操作:

    - (void) allDistributedNotifications:(NSNotification *)note {
    NSString *object = [note object];
    NSString *name = [note name];
    NSDictionary *userInfo = [note userInfo];
    NSLog(@"object: %@ name: %@ userInfo: %@",object, name, userInfo);

    if([object isEqualToString:@"com.apple.iTunes.dialog"]&& [userInfo objectForKey:@"Showing Dialog"] == 0){
    NSLog(@"*** ended iTunes Dialogue");
    }

    if([name isEqualToString:@"com.apple.iTunes.sourceSaved"]){
    NSLog(@"*** iTunes saved to file");
    currentURLIndex +=1;
    [self loadWithData:[itmsURLs objectAtIndex:currentURLIndex] fromBot:YES];
    }
    }
  3. LoadWithData 看起来像这样

    -(void) loadWithData:(NSURL*) url fromBot:(BOOL)aBot
    {
    BOOL success;

    success = [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:url]
    withAppBundleIdentifier:@"com.apple.itunes"
    options:NSWorkspaceLaunchDefault
    additionalEventParamDescriptor:nil
    launchIdentifiers:nil];
    if(success){
    [numAppsDownloaded setStringValue:[NSString stringWithFormat: @"%lu",currentURLIndex+1]];
    }
    if(success && aBot){
    [self performSelector:@selector(clickDownload) withObject:nil afterDelay:0.5];
    }
    }
  4. 点击下载依次如下所示

    -(void) clickDownload
    {
    NSPoint mouseLoc;

    mouseLoc = [NSEvent mouseLocation]; //get current mouse position
    CGPoint point = CGPointMake(mouseLoc.x, mouseLoc.y);

    CGEventRef theEvent;
    CGEventType type;

    CGMouseButton button = kCGMouseButtonLeft;

    type = kCGEventLeftMouseDown; // kCGEventLeftMouseDown = NX_LMOUSEDOWN,
    theEvent = CGEventCreateMouseEvent(NULL,type, point, button);

    NSEvent* downEvent = [NSEvent eventWithCGEvent:theEvent];
    [self forwardEvent:downEvent];

    [NSThread sleepForTimeInterval:0.2];

    type = kCGEventLeftMouseUp;
    theEvent = CGEventCreateMouseEvent(NULL,type, point, button);
    NSEvent* upEvent = [NSEvent eventWithCGEvent:theEvent];
    [self forwardEvent:upEvent];
    }

    5.最后,forwardEvent 看起来像这样

    - (void)forwardEvent: (NSEvent *)event
    {
    NSLog(@"event: %@",event);

    pid_t PID;
    NSInteger WID;

    // get the iTunes Window ID

    NSArray* windows = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

    NSEnumerator* windowEnumerator = [windows objectEnumerator];

    while( (window = [windowEnumerator nextObject] ) )
    {
    if([[(NSDictionary*) window objectForKey:@"kCGWindowName"] isEqualToString:@"iTunes"])
    WID = (NSInteger)[(NSDictionary*) window objectForKey:@"kCGWindowNumber"];
    }

    ProcessSerialNumber psn;
    CGEventRef CGEvent;
    NSEvent *customEvent;

    NSPoint mouseLoc = [NSEvent mouseLocation]; //get current mouse position
    NSPoint clickpoint = CGPointMake(mouseLoc.x, mouseLoc.y);

    customEvent = [NSEvent mouseEventWithType: [event type]
    location: clickpoint
    modifierFlags: [event modifierFlags] | NSCommandKeyMask
    timestamp: [event timestamp]
    windowNumber: WID
    context: nil
    eventNumber: 0
    clickCount: 1
    pressure: 0];

    CGEvent = [customEvent CGEvent];

    // get the iTunes PID

    NSRunningApplication* app;

    NSArray* runningApps = [[NSWorkspace sharedWorkspace] runningApplications];

    NSEnumerator* appEnumerator = [runningApps objectEnumerator];

    while ((app = [appEnumerator nextObject]))
    {
    if ([[app bundleIdentifier] isEqualToString:@"com.apple.iTunes"])

    PID = [app processIdentifier];
    }
    NSLog(@"found iTunes: %d %@",(int)PID,WID);

    NSAssert(GetProcessForPID(PID, &psn) == noErr, @"GetProcessForPID failed!");

    CGEventPostToPSN(&psn, CGEvent);
    }

问题是我看不到正在执行的鼠标单击。

最佳答案

您正在使用 NSCommandKeyMask 发送鼠标左键事件。对于右键单击等效项(即 Control 单击),您需要使用 NSControlKeyMask。或者,您可以只使用鼠标右键事件,而不使用修饰符掩码。

关于objective-c - 在 Objective-C 中自动点击 iTunes 鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com