apple script 打开常用软件

try
tell application “System Events”
(**)
–xxx代表要自己替换的地方,代码只是示例,需要自己修改一下(文件或文件夹名称等),报错的也可以直接删掉以方便运行。
#设定用户名
set userName to name of current user

	--启动偏好设置
	tell application "System Preferences"
		activate
		delay 2 #等待2秒
		set current pane to pane "com.apple.preference.network" --设置面板到网络
	end tell
	--delay 2 #点击前等待
	--click button "网络" of scroll area 1 of window "系统偏好设置" of application process "System Preferences" of application "System Events"
	delay 2 #给点击响应
	
	--判断Wi-Fi是否已经打开
	--"Wi-Fi, 关闭" -- "Wi-Fi, 已连接"
	set wifiIsOpen to false
	set allRows to every row of table 1 of scroll area 1 of window "网络" of application process "System Preferences" of application "System Events"
	set rowCount to count of allRows
	set x to 1
	repeat until (x = (rowCount + 1))
		-- statement 
		-- statement  
		set thisRow to item x of allRows
		set wifiStatus to value of static text of thisRow
		set wifiStatusStr to wifiStatus as string
		if wifiStatusStr contains "Wi-Fi" then --判断是否Wi-Fi行
			if wifiStatusStr contains "已连接" then --判断是否已经打开
				set wifiIsOpen to true
			end if
		end if
		set x to (x + 1)
	end repeat
	
	if wifiIsOpen then --判断是否已经打开
		--"已连接"
		delay 2
	else
		--"未连接"
		--(*
		--模拟点击打开Wi-Fi
		select row 3 of table 1 of scroll area 1 of window "网络" of application process "System Preferences" of application "System Events"
		delay 2 #给点击响应
		click button "打开Wi-Fi" of group 1 of window "网络" of application process "System Preferences" of application "System Events"
		delay 8 #等待网络自动连接上
		--*)
	end if
	
	--关闭偏好设置
	tell application "System Preferences"
		quit
	end tell
	
	--打开应用程序
	--打开QQ
	tell application "QQ"
		--activate
	end tell
	--打开微信
	--tell application "WeChat"
	--activate
	--end tell
	--打开SourceTree
	tell application "SourceTree"
		activate
	end tell
	--打开钉钉
	tell application "DingTalk"
		activate
	end tell
	
	--打开文件、工程等
	tell application "Finder"
		activate
		--打开Xcode工程
		open document file "xxx.xcworkspace" of folder "xxx" of folder userName of folder "Users" of startup disk
		--打开文稿
		open document file "xxx.pages" of folder "Documents" of folder "com~apple~Pages" of folder "Mobile Documents" of folder "Library" of folder userName of folder "Users" of startup disk --iCloud的Pages文件夹中的文件
		open document file "xxx.pages" of folder "Documents" of folder "com~apple~Pages" of folder "Mobile Documents" of folder "Library" of folder userName of folder "Users" of startup disk
		--打开Txt文件
		open document file "xxx.txt" of folder "xxx" of folder "xxx" of folder "Documents" of folder userName of folder "Users" of startup disk
	end tell
	
	--打开网页
	do shell script "open 'https://www.apple.com.cn'" 
	do shell script "open 'https://blog.csdn.net/Mr17Liu'"
	--打开网页
	tell application "Safari"
		activate
		--新建tab页,传入URL
		tell front window
			--get every tab
			--get URL of tab 1
			make new tab with properties {URL:"https://www.apple.com.cn"}
		end tell
	end tell
	
	--打开终端执行命令
	tell application "Terminal"
		activate
		--查看开机记录
		do script "last | grep reboot"
	end tell
	
	--延时等待文件打开
	delay 25
	
	--最小化Pages窗口
	tell application "Pages"
		set miniaturized of every window to true --最小化所有窗口
	end tell
	
	--告诉Xcode运行
	tell application "Xcode"
		--等待Xcode工程load完成
		repeat 120 times
			if loaded of front workspace document is true then
				delay 1.5
				tell front workspace document
					run #运行
					--stop #停止
				end tell
				exit repeat
			end if
			delay 0.5
		end repeat
		if loaded of front workspace document is false then
			error "Xcode workspace did not finish loading within timeout."
		end if
	end tell
	
	--点击微信的登录按钮
	--click button "登录" of window "登录" of application process "WeChat" of application "System Events"
	
	--点击QQ登录窗口的扫码按钮
	--click button 1 of window "Window" of application process "QQ" of application "System Events"
	
	--关闭脚本编辑器
	tell application "Script Editor"
		quit
	end tell
	
end tell

on error errMsg
display dialog "Error: " & errMsg
end try

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AppleScript 简明基础教程 iDoraemon Nathan 编著 目录 第一章 AppleScript入门! 第一节 什么是AppleScript! 第二节 AppleScript的工作机制! 第三节 AppleScript的用途和它带来的好处! 第四节 和AppleScript有关的程序和设置! 第五节 Automator和AppleScript! 第二章 快速上手AppleScript编辑器! 第一节 挖掘实用的功能! 第二节 脚本的存储格式! 第三节 支持AppleScript的应用程序! 第四节 AppleScript的录制功能! 应用实例1:建立100个子文件夹! 第三章 AppleScrip语言初步! 第一节 对象、属性和命令! 第二节 标识符和关键字! 第三节 数据类型! 第四节 强制数据类型转换! 第五节 运算符! 第六节 提取对象中的元素! 第七节 添加注释和括号! 第八节 代码缩写! 第四章 读懂AppleScript字典! 第一节 打开特定应用程序的AppleScript字典! 第二节 读懂AppleScript字典! 第五章 变量和属性! ! Nathan编著 3  第一节 变量的概念! 第二节 全局变量和局部变量! 第三节 数据共享机制! 第四节 属性! 第五节 预定义变量! 第六章 流程控制语句! 第一节 Tell语句! 第二节 条件语句If! 第三节 循环语句! 第四节 Considering/Ignoring语句(用于文本比较)! 第七章 基本用户交互! 第一节 简单对话框和输入框! 第二节 警告对话框! 第三节 列表选择对话框! 第四节 文件选择对话框! 第五节 其他用户交互! 第八章 错误处理! 第一节 基本的Try语句! 第二节 带有错误处理的Try语句! 第三节 AppleScript中的错误(Error)! 第四节 超时(Timeout)! 第九章 文件操作! 第一节 Alias类型! 第二节 相对路径和POSIX路径! 第三节 文件读取! 第四节 文件写入! Nathan编著 4 第十章 事件处理器! 第一节 基本的事件处理器! 第二节 带参数的事件处理器! 第三节 返回值! 第四节 run和open事件处理器! 第五节 保持打开的脚本应用程序! 第六节 文件夹操作! 第十一章 脚本对象! 第一节 me关键字! 第二节 编写和使用基本的script对象! 第三节 载入和调用外部script对象! 第四节 修改外部script对象中的属性变量! 附录一:AppleScript保留关键字! 附录二:预定义的错误代码和错误信息! AppleScript错误:! Mac OS系统错误! 后记!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值