(图文基本无关)

如果单纯说简单方便,其使用AppleScript更好,特别是现在有了JS的加入,比如:

(*

This Apple script will resize any program window to an exact size and the window is then moved to the center of your screen.
Specify the program name, height and width below and run the script. Written by Amit Agarwal on December 10, 2013 *) set theApp to "Google Chrome"
set appHeight to 1080
set appWidth to 1920 tell application "Finder"
set screenResolution to bounds of window of desktop
end tell set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution tell application theApp
activate
reopen
set yAxis to (screenHeight - appHeight) / 2 as integer
set xAxis to (screenWidth - appWidth) / 2 as integer
set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
end tell

觉得增加脚本会让你的工程比较繁琐的话,还可以把脚本写入到object-c用对象调用的方法完成,比如:

NSApplescript * as = [[NSApplescript alloc] initWithSource:@"tell application \"TheApplication\"\nclose every window\nend tell"];
NSDictionary * errInfo;
NSAppleEventDescriptor * res = [as executeAndReturnError:&err];
if( !res ){
// An error occurred. Inspect errInfo and perform necessary actions
} [as release];

但是如果真的开发一个产品,使用纯的c/object-c还是更规范、可控一些,因此上面这个两个脚本的方法不算在内。闲话不说,直接贴代码:

#include <stdio.h>

#import <Foundation/Foundation.h>
#import <CoreFoundation/CoreFoundation.h>
#import <ApplicationServices/ApplicationServices.h>
#import <CoreGraphics/CoreGraphics.h> typedef int CGSConnection;
extern CGSConnection _CGSDefaultConnection(void);
extern CGError CGSCopyWindowProperty(const CGSConnection cid, NSInteger wid, CFStringRef key, CFStringRef *output); void getTitleList1(){
CFStringRef titleValue;
CGSConnection connection = CGSDefaultConnectionForThread();
NSInteger windowCount, *windows;
char cTitle[256] = {0}; NSCountWindows(&windowCount);
windows = (NSInteger*) malloc(windowCount * sizeof(NSInteger));
if (windows) {
NSWindowList(windowCount, windows);
for (int i = 0; i < windowCount; ++i)
{
CGSCopyWindowProperty(connection, windows[i], CFSTR("kCGSWindowTitle"), &titleValue);
if(!titleValue) //Not every window has a title
continue; //CFStringGetCString(titleValue,cTitle,127,kCFStringEncodingMacRoman);
CFStringGetCString(titleValue,cTitle,256,kCFStringEncodingUTF8);
printf("title: %s\n",cTitle);
}
free(windows);
}
} void getTitleList2(){
@autoreleasepool {
// Get all the windows
CFArrayRef windowListAll = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSArray* arr = CFBridgingRelease(windowListAll);
NSUInteger count = [arr count]; //CFArrayGetCount(arr);
// Loop through the windows
for (NSMutableDictionary* entry in arr){
if (entry == nil){
break;
}
NSLog(@"enter:%@",entry);
NSString *wndName=[entry objectForKey:(id)kCGWindowName];
NSInteger wndNumber=[[entry objectForKey:(id)kCGWindowNumber] intValue];
NSLog(@"wndName:%@ number:%ld",wndName,wndNumber);
if (![wndName isEqualToString: @"~/test.txt"]){
//不是自己想要的窗口继续下一个循环
continue;
}
//下面这个方法是手册中最先查到的,但仅对属于自己app的窗口有效,其它app的窗口无效,所以不能采用
//NSWindow * wind=[NSApp windowWithWindowNumber: wndNumber];
//NSLog(@"wnd:%@",wind);
CGRect bounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:@"kCGWindowBounds"], &bounds);
NSLog(@"bounds: %@",NSStringFromRect(bounds));
//根据pid获取窗口所属的app
pid_t pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
AXUIElementRef appRef = AXUIElementCreateApplication(pid);
NSLog(@"Ref = %@",appRef);
//获取app所有的窗口
CFArrayRef windowList;
AXUIElementCopyAttributeValue(appRef, kAXWindowsAttribute, (CFTypeRef *)&windowList);
//NSLog(@"WindowList = %@", windowList);
CFRelease(appRef);
if (!windowList){
//NSLog(@"windowList is nil");
continue;
}
for (int i=0;i<CFArrayGetCount(windowList);i++){
//遍历app所有窗口,查找跟全局遍历所获得窗口的实体
AXUIElementRef windowRef = (AXUIElementRef) CFArrayGetValueAtIndex( windowList, i);
NSLog(@"windowRef:%@",windowRef);
CGWindowID application_window_id = 0;
_AXUIElementGetWindow(windowRef, &application_window_id);
if (application_window_id == wndNumber){
//找到
NSLog(@"Found a wnd that number is:%u",application_window_id);
//根据需要来操作窗口的位置,仅用作示例,这里可以修改成其它操作
CFTypeRef position;
CGPoint newPoint;
newPoint.x = 0;
newPoint.y = 0;
NSLog(@"Create new position");
position = (CFTypeRef)(AXValueCreate(kAXValueCGPointType, (const void *)&newPoint));
//setting new position
AXUIElementSetAttributeValue(windowRef, kAXPositionAttribute, position);
}
CFRelease(windowRef);
}
NSLog(@"end a loop ----------------------------");
} //for windowListAll
} //autorelease
} int main(int argc, char **argv){
getTitleList1(); //第一种方法,重点在遍历
getTitleList2(); //第二种方法,重点在获取窗口后可以进一步控制 return 0;
}

重点的内容直接看注释,其中的第二种方法可控性要好很多,不过程序也复杂一些。大概流程是先遍历所有屏幕的窗口->然后根据窗口获取该窗口所属的应用->再次获取应用所属的所有窗口->在这些窗口中找到自己想要的->控制,第二步的确做的会有大量重复遍历,不过从提供的api上看,目前只有这个办法才能够控制窗口。代码中有大量的日志信息,正式使用的话调试完成可以删掉。

这段代码使用object-c和c混编,后缀为.m,因为只是测试代码,没有建立xcode项目,是在命令行编译的,编译方法可能有的人不太熟悉,也贴出来:

clang -fobjc-arc  -o wndjob wndjob.m -framework CoreGraphics -framework AppKit

参考链接:

https://www.labnol.org/software/resize-mac-windows-to-specific-size/28345/

https://stackoverflow.com/questions/8898430/close-all-windows-of-another-app-using-accessibility-api

https://stackoverflow.com/questions/21069066/move-other-windows-on-mac-os-x-using-accessibility-api

两种方法操作其它mac应用的窗口的更多相关文章

  1. Linux 下操作GPIO(两种方法,驱动和mmap)(转载)

    目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...

  2. Linux 下操作gpio(两种方法,驱动和mmap)

    目前我所知道的在linux下操作GPIO有两种方法: 1.  编写驱动,这当然要熟悉linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据 ...

  3. 安卓ListView操作的两种方法

    举例做一个微信的中间部分(好友消息等信息通知) 第一种:BaseAdapter() package com.example.wx; import java.util.ArrayList;import ...

  4. Android开发12——Andorid中操作数据库的insert的两种方法以及nullColumnHack

    一.发现问题 先看两种方法插入数据 public void save(Person p){ SQLiteDatabase db = dbHelper.getWritableDatabase(); db ...

  5. spark-sql将Rdd转换为DataFrame进行操作的两种方法

    SparkConf sparkConf = new SparkConf() .setMaster("local").setAppName("ClzMap"); ...

  6. 代码操作Word时,目录自动更新的两种方法

    最近的项目中有一个功能点为:根据分析数据库并生成报告.不过不是大数据.数据挖掘之类,报告的内容.组织方式都是事先固定下来的.实现的方式为,在普通word文档中插入书签制成模板,然后程序使用OpenXM ...

  7. MySQL命令执行sql文件的两种方法

    MySQL命令执行sql文件的两种方法 摘要:和其他数据库一样,MySQL也提供了命令执行sql脚本文件,方便地进行数据库.表以及数据等各种操作.下面笔者讲解MySQL执行sql文件命令的两种方法,希 ...

  8. C# 调用WCF服务的两种方法

    项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...

  9. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

随机推荐

  1. 在VUE-CLI 3下的第一个Element-ui项目(菜鸟专用)

    vue-cli3.0使用及配置 (https://www.cnblogs.com/xzqyun/p/10779891.html  ) 以上是  vue-cli3.0使用及配置   这里我们来引用基于v ...

  2. angular之指令

    指令(Directive) 1.指令概念介绍 --  AngularJS有一套完整的.可扩展的.用来帮助web应用开发的指令集. --  在DOM编译期间和HTML关联着的指令会被检测到,并且 会被执 ...

  3. angularjs和ajax的结合使用 (三)

    转眼九月份了,忙忙碌碌 发现今年还没开过张,写一篇吧. 15年在空闲时就倒腾过angularjs那玩意儿 ,觉得还是挺好的,李金龙那厚厚的一本书,只不过没有系统化应用.最主要的是原来有一个东西没有用到 ...

  4. git创建本地分支以及推送本地分之至远程分支

    Git分支策略 实际开发中,应当按照以下几个基本原则进行管理: 首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能再上边干活. 那在哪干活呢?干活都在dev分支上,也就是说,de ...

  5. CF666B. World Tour

    CF666B. World Tour 题意: 给定一张边权为 1 的有向图,求四个不同点 A, B, C, D 使得 dis(A, B) + dis(B, C) + dis(C, D) 取最大值,di ...

  6. [pyMongo]insert_many的Bulkwrite实现机制

    在SQL中,insert many的操作可能会出现插入数据量过大的问题. 印象中MySQL Driver对insert语句的buffer有一个大小限制.超过这个限制的数据可能会被丢弃? -- > ...

  7. 图论之最短路径floyd算法

    Floyd算法是图论中经典的多源最短路径算法,即求任意两点之间的最短路径. 它可采用动态规划思想,因为它满足最优子结构性质,即最短路径序列的子序列也是最短路径. 举例说明最优子结构性质,上图中1号到5 ...

  8. 使用ant编译zookeeper源码

    1. 安装Ant Ant下载地址:http://ant.apache.org/bindownload.cgi 解压即可. 注意如果不配置环境变量的话需要使用绝对路径,我配置了. 开始我ant跑错了 U ...

  9. 2018面向对象程序设计(java)课程学习进度条

    周次 (阅读/编写)代码行数 发布博文量/评论他人博文数量 课余学习时间 学习收获的最大程序阅读或编程任务 1 30-50 1/0 5 九九乘法表 2 60-80 1/0 6 实验一,实验二 3 12 ...

  10. 自主学习python文本进度条及π的计算

    经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...