我的新玩具-AppleScript(三)

在纠结这一篇到底要不要发,不过既然是一个系列的,还是发出来吧。
本篇有很多案例是从别的地址copy的,下面会有说明。如果作者有异议请与我联系。
参考地址http://blog.csdn.net/yang3wei/article/details/7964226

1.使用AppleScript中的对话框

这种对话框很难符合现在用户的审美要求,不过特别方便。如果你不打算大规模推广你的脚本,而且不是处女座的话,也能够忍受了吧。

我的新玩具-AppleScript(三)_第1张图片

最简单的样式

ps:扁平化才是王道;看多了现代简约明快的作品后,明显比过去厚重古老的风格更符合时代精神。不过说不定哪天新一代复古潮流兴起,谁知道呢?!
使用弹出框有一些要注意的地方:
*1 它可以有多个按钮的;
*2 它是有返回值的,返回值是你最终操作的字符串;
*3 它是可以增加输入框的,而且比你想的简单多了。
setdialogString to"Input a number here"
set returnedString to display dialog dialogString default answer ""
看见了吗,你用一个set语句即可获得对话框的返回值;至于输入框,你加一个default answer ""搞定,你可以在双引号内显示预输入的内容。
set dialogString to "Input a number here"

set returnedString to display dialog dialogString default answer ""

set returnedNumber to the text returned of returnedString

try

set returnedNumber to returnedNumber as number

set calNumber to returnedNumber * 100

 display dialog calNumber

on error the error_message number the error_number

display dialog "Error:" & the error_number & " Details:" & the error_message

end try

beep
最后一个beep就让你的电脑叫一下。

2.使用mac的邮件系统

很显然,你要发邮件的话,至少要考虑下面这些变量:收件人,标题,内容,附件等等。看到下面的代码,你差不多应该明白了。
--Variables

set recipientName to " 小红"

set recipientAddress to "[email protected]"

set theSubject to "AppleScript Automated Email"

set theContent to "This email was created and sent using AppleScript!"

--Mail Tell Block

tell application "Mail"

--Create the message

set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

--Set a recipient

tell theMessage

make new to recipient with properties {name:recipientName, address:recipientAddress}

--Send the Message

send

end tell

end tell
如果你想快速回复确认的内容的话,这个脚本或许有用;当然,我认为它最大的用处应该是骚扰别人。

3.让浏览器打开网页

每天上班的时候,我肯定会用到浏览器。休息的时候会逛一下CSDN,现在还有一些不错的搜索引擎可以玩:快搜可以直接Google哦;中国搜索刚刚上线,有一些不错的内容,支持国产;必应的页面好看,光放着也是不错的嘛。
这里浏览器用的是Chrome:
set urlMyBlog to "http://blog.csdn.net/u011238629"

set urlKuaiso to "http://so.chongbuluo.com/"

set urlChinaSearch to "http://www.chinaso.com"

set urlBiying to "https://cn.bing.com"

--使用Chrome浏览器

tell application "Google Chrome"

--新建一个chrome窗口

set window1 to make new window

tell window1

--当前标签页加载必应,就是不用百度哈哈

set currTab to active tab of window1

set URL of currTab to urlBiying

--打开csdn博客,搜索

make new tab with properties {URL:urlMyBlog}

make new tab with properties {URL:urlChinaSearch}

make new tab with properties {URL:urlKuaiso}

--将焦点由最后一个打开的标签页还给首个标签页

set active tab index of window1 to 1

end tell

end tell

4.让你的电脑说话

这个技能可以用来配音的。
tell current application

say "How are you?" using "Zarvox"

say "Fine,thank you." using "Victoria"

say "Ha Ha"

--嘟嘟响一声

beep

end tell

5.清理废纸篓

tell application "Finder"

empty the trash

beep

open the startup disk

end tell

就写到这里吧,上面都是网上很容易搜索到的东西。至于我的目标之一是用AppleScript控制Xcode做一些事,如果有收获再分享吧。
今天的代码排版可能好一些,学习了一下Markdown的编辑方式:http://www.jianshu.com/p/q81RER

你可能感兴趣的:(开发相关)