Mac 自动化自动操作工作流程(AppleScript)完成日常软件文件网页一键打开

每天上班都要打开相同的软件、文件、网页,很烦?
每天下班都要关闭相同的软件、文件、网页,很烦?
试试Mac系统自带的脚本AppleScript,一键上班,一键下班。
1、打开脚本编辑器(AppleScript)
点击启动台 -> 其他 -> 脚本编辑器。或直接在浏览器中输入applescript://,然后按回车键。
2、打开脚本编辑器后,点击新建文稿,输入脚本内容。
3、脚本示例如下,里面主要的功能是获取用户名、启动偏好设置打开Wi-Fi、打开常用应用程序、打开常用文件、打开常用网页以及后续其它等。

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

4、按Command+s组合键进行保存,选择保存位置和文件格式,格式选择[脚本]。
5、然后就是点击运行。如图:
Mac 自动化自动操作工作流程(AppleScript)完成日常软件文件网页一键打开_第1张图片

你可能感兴趣的:(AppleScript,Mac脚本编辑器,AppleScript,Mac自动化)